我在外壳中使用网格布局。我想控制小部件的位置。如您所见,底部按钮必须位于第 4 行。我正在使用 windowbuilder 插件,它可以将它们放置在所需的位置。
问题是我在源代码中看不到放置指令。我怎样才能以编程方式放置它们。这是窗口生成器生成的源代码。
package dgsj.Chapter04.examples.ch4;
import org.eclipse.swt.widgets.*;
public class GridLayout2x2 {
private static GridData data_1;
private static GridData data_2;
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setMinimumSize(new Point(123, 30));
shell.setSize(126, 123);
GridLayout layout = new GridLayout();
layout.numColumns = 3;
shell.setLayout(layout);
GridData data = new GridData(GridData.FILL_BOTH);
data.grabExcessHorizontalSpace = false;
data.widthHint = 200;
data.grabExcessVerticalSpace = false;
Button one = new Button(shell, SWT.PUSH);
one.setText("one");
one.setLayoutData(data);
data = new GridData(GridData.FILL_BOTH);
Button two = new Button(shell, SWT.PUSH);
two.setText("two");
two.setLayoutData(data);
new Label(shell, SWT.NONE);
new Label(shell, SWT.NONE);
data_2 = new GridData(GridData.FILL_BOTH);
Button four = new Button(shell, SWT.PUSH);
four.setText("four");
four.setLayoutData(data_2);
new Label(shell, SWT.NONE);
new Label(shell, SWT.NONE);
new Label(shell, SWT.NONE);
new Label(shell, SWT.NONE);
new Label(shell, SWT.NONE);
Button btnNewButton = new Button(shell, SWT.NONE);
btnNewButton.setText("New Button");
data_1 = new GridData(GridData.FILL_BOTH);
data_1.grabExcessVerticalSpace = false;
data_1.grabExcessHorizontalSpace = false;
Button three = new Button(shell, SWT.PUSH);
three.setText("three");
three.setLayoutData(data_1);
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}