Java SWT复合对准

Java SWT复合对准,java,swt,margin,composite,Java,Swt,Margin,Composite,我想知道对于SWT复合对象,这里的正常/常见做法是什么 我发现,每当我添加一个组合(使用任何UI示例:TextBox或Button)时,在组合中创建的UI都不会与组合的起始边缘对齐。(您可以通过设置合成的背景色来观察这一点) 文本框UI之前的组合中有一些空格/填充。如果以前的UI不是在组合中创建的,那么这会导致我正在创建的GUI表单中出现不对齐 我想知道使它们一致的常见做法是什么?通过设置一些负数填充来向后移动组合,使组合中的UI看起来像是对齐的 下面是示例代码 public static vo

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

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

文本框UI之前的组合中有一些空格/填充。如果以前的UI不是在组合中创建的,那么这会导致我正在创建的GUI表单中出现不对齐

我想知道使它们一致的常见做法是什么?通过设置一些负数填充来向后移动组合,使组合中的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();
    }

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


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

这将修复它:

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();
}
publicstaticvoidmain(字符串[]args){
显示=新显示();
外壳=新外壳(显示);
shell.setLayout(新的GridLayout(1,false));
文本t1=新文本(外壳,SWT.SINGLE | SWT.BORDER);
t1.setText(“测试盒…”);
复合材料c=新复合材料(壳体,SWT.无);
GridLayout=新的GridLayout(2,true);
layout.marginWidth=0;//这将修复它:

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();
}
publicstaticvoidmain(字符串[]args){
显示=新显示();
外壳=新外壳(显示);
shell.setLayout(新的GridLayout(1,false));
文本t1=新文本(外壳,SWT.SINGLE | SWT.BORDER);
t1.setText(“测试盒…”);
复合材料c=新复合材料(壳体,SWT.无);
GridLayout=新的GridLayout(2,true);
layout.marginWidth=0//