在不添加FormLayout的情况下组合GridLayout和FillLayout?

问题描述 投票:1回答:1

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

例如,我的应用程序中有一个使用GridLayout的屏幕。我想添加两个垂直(即FillLayout)布局; GridLayout左侧的一个布局和GridLayout右侧的另一个布局。不幸的是,我无法弄清楚如何获得我想要的整体布局(即在底部看到ascii艺术)。

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

can I combine SWT GridLayout and FillLayout

是否有示例代码允许我在不使用FormLayout的情况下将两个FillLayouts添加到网格布局?应该如下所示(忽略最右边的FillLayout中粘贴得很糟糕的ascii)。

+-------------+      +-------------------------------------------+          +--------------+
|             |      |                                           |      |                      |
|             |      |                                           |      |                  |
|             |      |                                           |      |                      |
|             |      |                                           |      |                      |
|  Fill Layout|      |     Grid Layout                           |      |      Fill Layout |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
+-------------+      +-------------------------------------------+          +--------------+
java layout swt
1个回答
3
投票

尝试以下代码,如果您看起来与此类似,请告诉我。

我使用Absolute Layout作为壳,我在我的应用程序中创建了3个复合材料,并将Fill Layout应用于2个复合材料和Grid Layout用于1个复合材料。请参阅以下代码

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");

    }
}

输出类似如下:

Sample SWT application window

© www.soinside.com 2019 - 2024. All rights reserved.