多列

與上面的單列示例類似,如果我們改為使用 GridLayout(int, boolean) 建構函式,我們可以建立一個包含多列的佈局。

在這種情況下,我們建立兩列,每列都具有相同的寬度。

public class MultiColumnGridLayoutExample {

    private final Display display;
    private final Shell shell;

    public MultiColumnGridLayoutExample() {
        display = new Display();
        shell = new Shell(display);

        // Create the layout and apply to the Shell
        shell.setLayout(new GridLayout(2, true));

        // Add the child controls to the Shell - in this case, in two columns
        final Button buttonA = new Button(shell, SWT.PUSH);
        buttonA.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        buttonA.setText("Button A");
        
        final Button buttonB = new Button(shell, SWT.PUSH);
        buttonB.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        buttonB.setText("Button B");
        
        final Button buttonC = new Button(shell, SWT.PUSH);
        buttonC.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        buttonC.setText("Button C");
        
        final Button buttonD = new Button(shell, SWT.PUSH);
        buttonD.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        buttonD.setText("Button D");
    }

    public void run() {
        shell.pack();
        shell.open();

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

    public static void main(final String... args) {
        new MultiColumnGridLayoutExample().run();
    }

}

結果是:

在此處輸入影象描述