如何在网格布局中设置窗口小部件的行和列?

时间:2013-06-19 13:03:59

标签: swt

我在shell中使用网格布局。我想控制小部件的位置。正如您所看到的,底部按钮必须位于第4行。我正在使用windowbuilder插件,它可以将它们放置在所需的位置。

my shell using 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();
    }
}

2 个答案:

答案 0 :(得分:11)

“跳过”GridLayout中某些单元格的唯一方法是用空控件填充它们,这就是WindowBuilder对这些new Label(shell, SWT.NONE);行的处理方式。如果要以编程方式在给定的行/列上放置控件,则必须计算要添加的空Label(或其他不可见控件)所需的数量。

答案 1 :(得分:3)

您最好使用FormLayout,这比GridLayout更灵活。 如果需要,可以获取窗口小部件FormData,更改其设置,然后强制重新布局以在运行时重新定位窗口小部件。

Eclipse的SWT Layout视图可能对您有很大帮助。

以下是一些重现您的布局的代码:

import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class MyLayout {
    public static void main (String [] args) {
        Display display = new Display ();
        Shell shell = new Shell (display);
        FormLayout formLayout = new FormLayout ();
        shell.setLayout (formLayout);

        Button button0 = new Button (shell, SWT.PUSH);
        button0.setText ("button0");
        FormData data = new FormData ();
        data.left = new FormAttachment (0, 0);
        data.right = new FormAttachment (button1, 0, SWT.DEFAULT);
        button0.setLayoutData (data);

        Button button1 = new Button (shell, SWT.PUSH);
        button1.setText ("button1");
        data = new FormData ();
        data.left = new FormAttachment (button3, 0, SWT.LEFT);
        data.right = new FormAttachment (button3, 0, SWT.RIGHT);
        button1.setLayoutData (data);

        Button button2 = new Button (shell, SWT.PUSH);
        button2.setText ("button2");
        data = new FormData ();
        data.left = new FormAttachment (button3, 0, SWT.LEFT);
        data.right = new FormAttachment (button3, 0, SWT.RIGHT);
        data.top = new FormAttachment (button1, 0, SWT.DEFAULT);
        button2.setLayoutData (data);

        Button button3 = new Button (shell, SWT.PUSH);
        button3.setText ("button3");
        data = new FormData ();
        data.right = new FormAttachment (button4, 0, SWT.DEFAULT);
        data.bottom = new FormAttachment (100, 0);
        button3.setLayoutData (data);

        Button button4 = new Button (shell, SWT.PUSH);
        button4.setText ("button4");
        data = new FormData ();
        data.right = new FormAttachment (100, 0);
        data.bottom = new FormAttachment (100, 0);
        button4.setLayoutData (data);

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

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

        display.dispose ();
    }
}