2014-02-19 36 views
0

我在一行中創建了幾個對象。如何刪除對象之間的空間?

我想控制對象之間的空間

我該怎麼做? 我是否需要更改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; 

}

回答

3

使用RowLayout#spacing = 0刪除項目之間的間隔。下面是一些示例代碼(我用SWT.BORDERLabel s到使間距更明顯):

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