我们如何自动调整SWT中组件的大小?

在我的SWT应用程序中,我在SWT shell中有一些组件。

现在,我如何根据显示窗口的大小自动重新调整此组件的大小。

Display display = new Display(); Shell shell = new Shell(display); Group outerGroup,lowerGroup; Text text; public test1() { GridLayout gridLayout = new GridLayout(); gridLayout.numColumns=1; shell.setLayout(gridLayout); outerGroup = new Group(shell, SWT.NONE); GridData data = new GridData(1000,400); data.verticalSpan = 2; outerGroup.setLayoutData(data); gridLayout = new GridLayout(); gridLayout.numColumns=2; gridLayout.makeColumnsEqualWidth=true; outerGroup.setLayout(gridLayout); ... } 

即,当我减小窗口的大小时,它内部的组件应该根据它出现。

这听起来很可疑,就像你没有使用布局一样。

布局的整个概念让人担心不必要的大小调整。 布局将考虑其所有组件的大小。

我建议阅读关于布局的Eclipse文章

您的代码很容易纠正。 不要设置单个组件的大小,布局将决定它们的大小。 如果您希望窗口具有预定义的大小,请设置shell的大小:

 public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new GridLayout(1, false)); Group outerGroup = new Group(shell, SWT.NONE); // Tell the group to stretch in all directions outerGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); outerGroup.setLayout(new GridLayout(2, true)); outerGroup.setText("Group"); Button left = new Button(outerGroup, SWT.PUSH); left.setText("Left"); // Tell the button to stretch in all directions left.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); Button right = new Button(outerGroup, SWT.PUSH); right.setText("Right"); // Tell the button to stretch in all directions right.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); shell.setSize(1000,400); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } 

在resize之前:

在调整大小之前

resize后:

调整大小后