0
我想建立一个垂直放置多个复合材料的GUI。我把我的方法放在一个最简单的例子中:儿童复合材料没有出现在布局
public class LayoutTest {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setSize(400, 300);
shell.setLayout(new FillLayout());
Composite listPanel = new ScrolledComposite(shell, SWT.BORDER
| SWT.V_SCROLL);
listPanel.setLayout(new RowLayout(SWT.VERTICAL));
listPanel.setBackground(Display.getCurrent().getSystemColor(
SWT.COLOR_WHITE));
for (int i = 1; i < 5; i++) {
Composite listElement = new Composite(listPanel, SWT.BORDER);
listElement.setLayout(new RowLayout());
Label label = new Label(listElement, SWT.BORDER);
label.setText(String.valueOf(i) + " foo");
label.setSize(label.computeSize(SWT.DEFAULT, SWT.DEFAULT));
listElement.setSize(label.computeSize(shell.getSize().x,
label.getSize().y));
}
shell.layout(true, true);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
你可以看到有些复合材料是在循环中创建的。问题是只有第一个出现在UI中。我预计他们会被排列在彼此之下,因为它的父母有一个垂直的RowLayout
。
我也试过其他方法。一个是一列GridLayout
另一个是FormLayout
,在那里我将每个孩子的顶部附件设置为前一个。两者显示完全相同的结果。
有谁知道我在做什么错?
非常感谢!只是为了理解:'ScrolledComposite'只能有一个孩子,所以我将多个孩子包裹在一个额外的'Composite'中? – 2013-04-04 19:46:58
@André正确。 – Baz 2013-04-04 20:10:18