在我的情况下,我有两个SashForm的孩子,但问题适用于所有复合材料.
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 );
希望它有帮助.