2013-03-28 158 views
1

我试图用SWT创建一个简单的显示。到目前为止,我已成功显示来自数据库的信息并使用RowLayout显示它,每行都包含一个GridLayout。它看起来像这样:SWT网格布局宽度

enter image description here

我真正想要的是行延伸到占用窗口的整个宽度。我如何实现这一目标?

感谢您的帮助!

回答

6

通常的做法是使用GridData。这GridData告诉组件如何在它的父代中行为,例如如何在父母之间传播。

使用:

component.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false)); 

你告诉组件水平占据尽可能多的空间可能的,但只有在必要的空间垂直。

下面是应该表现在路上一个小例子,你指望它:

public class StackOverflow 
{ 
    public static void main(String[] args) 
    { 
     Display display = Display.getDefault(); 
     Shell shell = new Shell(display); 

     /* GridLayout for the Shell to make things easier */ 
     shell.setLayout(new GridLayout(1, false)); 

     for(int i = 0; i < 5; i++) 
     { 
      createRow(shell, i); 
     } 

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

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

    private static void createRow(Shell shell, int i) 
    { 
     /* GridLayout for the rows, two columns, equal column width */ 
     Composite row = new Composite(shell, SWT.NONE); 
     row.setLayout(new GridLayout(2, true)); 

     /* Make each row expand horizontally but not vertically */ 
     row.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false)); 

     /* Create the content of the row, expand horizontally as well */ 
     Button first = new Button(row, SWT.PUSH); 
     first.setText("FIRST " + i); 
     first.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false)); 
     Button second = new Button(row, SWT.PUSH); 
     second.setText("SECOND " + i); 
     second.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false)); 
    } 
} 

这就是它看起来像在启动后:

enter image description here

和调整后:

enter image description here


附注:如果您还没有阅读,我建议您阅读Eclipse关于布局的教程this。每个SWT开发者都应该阅读它。

+0

感谢您的帮助。我怎样才能获得相同的功能,但具有不同大小的列? –

+0

我使用嵌套gridlayouts来近似我的目光。再次感谢。 –