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

标签 java layout swt

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

例如,我的应用程序中有一个使用 GridLayout 的屏幕。我想添加两个垂直(即 FillLayout)布局;一个布局位于 GridLayout 左侧,另一个布局位于 GridLayout 右侧。不幸的是,我不知道如何获得我想要的整体布局(即见底部的 ascii 艺术)。

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

can I combine SWT GridLayout and FillLayout

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

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

最佳答案

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

我正在为 shell 使用 Absolute Layout,我在我的应用程序中创建了 3 个复合体,并为 2 个复合体应用了 Fill LayoutGrid Layout 对于 1 个复合 Material 。引用下面的代码

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

关于java - 在不添加 FormLayout 的情况下组合 GridLayout 和 FillLayout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50740543/

相关文章:

android - 将 TextView 直接放在 XML 上的 Imageview 之上

java - Canvas 上的 SWT 可编辑文本字段

java - 如何在不使用servlet但使用JAX-RS的情况下将json对象从ajax发布到java类?

java - 在java中构造位序列

java - “镜像”二维数组 [Java]

php - Twig 根据条件扩展模板

java - 如何使用Java中的Scanner读取文件?

java - ListView 与嵌套布局 [优点/缺点]

java - 如何使用SWT浏览器检测互联网连接(或处理服务器不可用)

java - SWT 拖放自定义控件显示在光标旁边