如何删除对象之间的空间?

时间:2014-02-19 15:54:49

标签: java swt

我在一行中创建了几个对象。

我想控制对象之间的空间

我怎么做? 我是否需要更改RowLayout?

public static void main(String[] args)
 {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout(2, false));

Composite myComposite = new Composite(shell, SWT.NONE);
myComposite.setLayout(new GridLayout(1, false));
myComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

Composite deComposite = new Composite(myComposite, SWT.NONE);
deComposite.setLayout(new RowLayout(SWT.HORIZONTAL));

Label createDetailsde = createDetailsde(deComposite);

shell.pack();
shell.open();

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

}

private static Label createDetailsde(Composite detailsComposite)
   {
     Label linkLabel = new Label(detailsComposite, SWT.NONE);
     linkLabel.setText("test");
     return linkLabel;

}

1 个答案:

答案 0 :(得分:3)

使用RowLayout#spacing = 0删除项目之间的间距。以下是一些示例代码(我使用SWT.BORDER Label来使间距更明显):

public static void main(String[] args)
{
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new GridLayout(2, false));

    Composite myComposite = new Composite(shell, SWT.NONE);
    myComposite.setLayout(new GridLayout(1, false));
    myComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);
    rowLayout.spacing = 0;

    Composite deComposite = new Composite(myComposite, SWT.NONE);
    deComposite.setLayout(rowLayout);

    createDetailsde(deComposite);
    createDetailsde(deComposite);
    createDetailsde(deComposite);
    createDetailsde(deComposite);

    shell.pack();
    shell.open();

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

}

private static Label createDetailsde(Composite detailsComposite)
{
    Label linkLabel = new Label(detailsComposite, SWT.BORDER);
    linkLabel.setText("test");
    return linkLabel;

}

有间距:

enter image description here

没有间距:

enter image description here