我有以下代码:
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/