在我的例子中,我有一个 SashForm
的两个 child ,但这个问题适用于所有 Composite
。
class MainWindow {
Sashform sashform;
Tree child1 = null;
Table child2 = null;
MainWindow(Shell shell) {
sashform = new SashForm(shell, SWT.NONE);
}
// Not called from constructor because it needs data not available at that time
void CreateFirstChild() {
...
Tree child1 = new Tree(sashform, SWT.NONE);
}
void CreateSecondChild() {
...
Table child2 = new Table(sashform, SWT.NONE);
}
}
我事先不知道这些方法的调用顺序。如何确保 child1
位于左侧,child2
位于右侧?或者,有没有一种方法可以在创建 sashform
之后更改它们的顺序?
目前我最好的想法是像这样放置占位符:
class MainWindow {
Sashform sashform;
private Composite placeholder1;
private Composite placeholder2;
Tree child1 = null;
Table child2 = null;
MainWindow(Shell shell) {
sashform = new SashForm(shell, SWT.NONE);
placeholder1 = new Composite(sashform, SWT.NONE);
placeholder2 = new Composite(sashform, SWT.NONE);
}
void CreateFirstChild() {
...
Tree child1 = new Tree(placeholder1, SWT.NONE);
}
void CreateSecondChild() {
...
Table child2 = new Table(placeholder2, SWT.NONE);
}
}
最佳答案
创建child1时,检查child2是否已经实例化。如果是,说明child1在右边,因为后面已经创建了,所以要这样做:
child1.moveAbove( child2 );
希望对您有所帮助。
关于java - 更改 SWT 组合子项的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2780694/