Java SWT提供了四个标准布局类:FillLayoutRowLayoutGridLayoutFormLayout。我知道如何使用每个布局类,但是在组合布局类时会遇到困难。

例如,我的应用程序中有一个使用GridLayout的屏幕。我希望添加两个垂直(即FillLayout)布局; GridLayout左侧的一个布局,GridLayout右侧的另一个布局。不幸的是,我无法弄清楚如何获得所需的整体布局(即,请参见底部的ascii art)。

下面的SO链接提供了示例答案,但是我想避免通过添加FormLayout来过多地更改代码。

can I combine SWT GridLayout and FillLayout

是否有示例代码可让我在不使用FormLayout的情况下将两个FillLayouts添加到Grid Layout?应该看起来像下面的样子(忽略最右边的FillLayout中粘贴错误的ascii)。

+-------------+      +-------------------------------------------+          +--------------+
|             |      |                                           |      |                      |
|             |      |                                           |      |                  |
|             |      |                                           |      |                      |
|             |      |                                           |      |                      |
|  Fill Layout|      |     Grid Layout                           |      |      Fill Layout |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
+-------------+      +-------------------------------------------+          +--------------+

最佳答案

试试下面的代码,让我知道您是否看起来与此相似。

我在外壳上使用Absolute Layout,并在应用程序内部创建了3个复合,并在2个复合中应用了Fill Layout,在1个复合中应用了Grid Layout。请参考下面的代码

public class SampleWindow {

    protected Shell shell;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        try {
            SampleWindow window = new SampleWindow();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    /**
     * Create contents of the window.
     */
    protected void createContents() {
        shell = new Shell();
        shell.setSize(531, 363);
        shell.setText("SWT Application");

        Composite composite = new Composite(shell, SWT.BORDER);
        composite.setBounds(10, 10, 131, 304);
        composite.setLayout(new FillLayout(SWT.HORIZONTAL));

        Label lblNewLabel = new Label(composite, SWT.NONE);
        lblNewLabel.setText("Fill Layout");

        Composite composite_1 = new Composite(shell, SWT.BORDER);
        composite_1.setBounds(159, 10, 199, 304);
        composite_1.setLayout(new GridLayout(3, false));
        Label lblNewLabel_1 = new Label(composite_1, SWT.NONE);
        lblNewLabel_1.setText("Grid Layout");

        Composite composite_2 = new Composite(shell, SWT.BORDER);
        composite_2.setBounds(382, 10, 123, 304);
        composite_2.setLayout(new FillLayout(SWT.HORIZONTAL));

        Label lblNewLabel_2 = new Label(composite_2, SWT.NONE);
        lblNewLabel_2.setText("Fill Layout");

    }
}


输出将类似于以下内容:

java - 合并GridLayout和FillLayout而不添加FormLayout?-LMLPHP

09-13 00:36