问题 改变SWT综合体儿童的顺序


就我而言,我有两个孩子 SashForm,但问题适用于所有人 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);
    }    
}

10843
2018-05-06 11:31


起源



答案:


创建child1时,检查child2是否已经实例化。如果是,则表示child1在右侧,因为它已在稍后创建,因此您必须这样做:

child1.moveAbove( child2 );

希望能帮助到你。


13
2018-05-06 12:16



确实如此。我只是错过了存在 moveAbove 因为我正在寻找各种方法 Composite。 - Alexey Romanov
感谢您的信息:)一件好事需要注意的是,您可以使用null将其移动到顶部。 - Jacob L


答案:


创建child1时,检查child2是否已经实例化。如果是,则表示child1在右侧,因为它已在稍后创建,因此您必须这样做:

child1.moveAbove( child2 );

希望能帮助到你。


13
2018-05-06 12:16



确实如此。我只是错过了存在 moveAbove 因为我正在寻找各种方法 Composite。 - Alexey Romanov
感谢您的信息:)一件好事需要注意的是,您可以使用null将其移动到顶部。 - Jacob L