通常的方法是使用GridData.这GridData告诉组件如何在其父节点内表现,例如如何在父节点上传播.
通过使用:
component.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));
Run Code Online (Sandbox Code Playgroud)
告诉组件水平占用尽可能多的空间,但只能垂直占用必要的空间.
这是一个小例子,它应该按照你期望的方式运行:
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));
}
}
Run Code Online (Sandbox Code Playgroud)
这是启动后的样子:

调整大小后:

作为旁注:如果您还没有阅读过,我建议您阅读Eclipse关于Layouts的本教程.每个SWT开发人员都应该阅读它.
| 归档时间: |
|
| 查看次数: |
9102 次 |
| 最近记录: |