【问题标题】:Combining GridLayout and FillLayout without adding FormLayout?合并GridLayout和FillLayout而不添加FormLayout?
【发布时间】:2018-11-17 07:52:57
【问题描述】:

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 |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
|             |      |                                           |      |                  |
+-------------+      +-------------------------------------------+          +--------------+

【问题讨论】:

  • 所以你想在 SWT 应用程序中填充布局和网格布局,而不添加表单布局。对吗?
  • @SubashJ 正确。

标签: java layout swt


【解决方案1】:

试试下面的代码,如果你看起来类似的话,请告诉我。

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

    }
}

输出将如下所示:

【讨论】:

  • 在这样的事情上使用setBounds 通常不是一个好主意。您应该让布局为您处理大小。
猜你喜欢
  • 1970-01-01
  • 2018-02-23
  • 2012-11-20
  • 2014-03-10
  • 1970-01-01
  • 1970-01-01
  • 2019-03-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多