9

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

我的shell使用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();
    }
}
4

2 回答 2

11

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

于 2013-06-19T15:03:01.140 回答
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 ();
    }
}
于 2013-06-20T12:40:05.840 回答