【问题标题】:SWT Composite alignmentSWT 复合对齐
【发布时间】:2012-10-17 21:15:04
【问题描述】:

我想知道这里对于 SWT 复合对象的正常/常见做法是什么。

我发现,每当我添加一个 Composite(使用任何 UI 示例:TextBox 或 Button)时,在 Composite 中创建的 UI 都不会与 Composite 的起始边缘对齐。 (可以通过设置 Composite 的背景颜色来观察)

在 TextBox UI 之前的 Composite 中有一些空间/填充。如果之前的 UI 不是在 Composite 中创建的,这会导致我正在创建的 GUI 表单出现错位。

我想知道使它们保持一致的常见做法是什么?通过设置一些负填充来将 Composite 向后移动,以使其中的 UI 看起来像是对齐的?

下面的示例代码!

public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        GridLayout layout = new GridLayout();
        layout.numColumns = 1;
        layout.makeColumnsEqualWidth = false;
        shell.setLayout(layout);

        Text t1 = new Text(shell, SWT.SINGLE | SWT.BORDER);
        t1.setText("Test box...");

        Composite c = new Composite(shell, SWT.NONE);
        // c.setBackground(new Color(shell.getDisplay(), 255,0,0));
        layout = new GridLayout();
        layout.numColumns = 2;
        layout.makeColumnsEqualWidth = true;
        c.setLayout(layout);

        Text t2 = new Text(c, SWT.SINGLE | SWT.BORDER);
        t2.setText("Test box within Composite... not aligned to the first textbox");

        Button b = new Button(c, SWT.PUSH);
        b.setText("Button 1");

        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

【问题讨论】:

    标签: java swt margin composite


    【解决方案1】:

    这将解决它:

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(1, false));
    
        Text t1 = new Text(shell, SWT.SINGLE | SWT.BORDER);
        t1.setText("Test box...");
    
        Composite c = new Composite(shell, SWT.NONE);
        GridLayout layout = new GridLayout(2, true);
    
        layout.marginWidth = 0; // <-- HERE
    
        c.setLayout(layout);
    
        Text t2 = new Text(c, SWT.SINGLE | SWT.BORDER);
        t2.setText("Test box within Composite... not aligned to the first textbox");
    
        Button b = new Button(c, SWT.PUSH);
        b.setText("Button 1");
    
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
    

    如果将GridLayout 设置为0,只需设置marginWidth

    提示一下:可以在GridLayout的构造函数中设置列数和等宽的东西。

    【讨论】:

      【解决方案2】:

      GridLayout 具有与之关联的默认边距。我建议你阅读这篇文章

      http://www.eclipse.org/articles/article.php?file=Article-Understanding-Layouts/index.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-11-27
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-06
        • 2012-11-10
        相关资源
        最近更新 更多