java - 手动界定的复合 Material 中的 SWT 布局

标签 java swt

我有以下代码:

public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);

    Composite parent = new Composite(shell, SWT.NONE);
    parent.setBounds(20, 20, 400, 300);//since shell doesn't have a layout
    parent.setLayout(new FillLayout(SWT.HORIZONTAL|SWT.VERTICAL));

    Composite child = new Composite(parent, SWT.BORDER);
    child.setLayout(new GridLayout(2, true));

    Label l = new Label(child, SWT.NONE);
    l.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
    l.setText("BLOB-BLOB-BLOB!");

    Button button = new Button(child, SWT.PUSH);
    button.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
    button.setText("TEEEEXTTTT");


    shell.setSize(500, 500);
    shell.open();

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

因此,我认为我的父复合 Material 的边界为 (20, 20, 400, 300),因为 shell 没有布局,而子复合 Material 填充父复合 Material ,因为它有 FillLayout。 但我只得到了正确的 parent 。我必须为 shell 设置布局,或者为每个控件设置每个边界,尽管已经设置了布局。为什么布局在这种情况下不起作用?

最佳答案

如果在打开 shell 之前强制对其进行布局,则一切都会按预期显示:

shell.setSize( 500, 500 );
shell.layout( true, true ); // force layout
shell.open();

我不确定为什么需要 layout() 调用。但我通常也不使用绝对位置...

作为旁注:FillLayout 采用任一 SWT.HORIZONTAL SWT.VERTICAL 。指定两者没有意义。

关于java - 手动界定的复合 Material 中的 SWT 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27179013/

相关文章:

java - 使用 SWT 浏览器小部件时如何从 Java 设置 Mozilla 首选项?

java - Java 的摘要与外部实用程序的不同结果

java - 我无法在我的程序中使用 PrinterWriter,有人可以帮我解决这个问题吗?

java 数组创建复杂性

java - SWT - 创建一个数字网格,我希望能够使用键盘进行切换

java - 非模态 JFace 对话框?

java - 在表格单元格中查找单击按钮的行索引

java - SWT.KeyUp 触发过于频繁

java - java中打印时 'long'类型的数字改变了它的值

java - 如何在初始化期间声明 spring bean