改变SWT综合体儿童的顺序

时间:2023-01-07 23:08:11

In my case I have two children of a SashForm, but the question applies to all Composites.

在我的情况下,我有两个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);
    }    
}

I don't know in advance in what order these methods will be called. How can I make sure that child1 is placed on the left, and child2 on the right? Alternately, is there a way to change their order as children of sashform after they are created?

我事先不知道这些方法的调用顺序。如何确保child1位于左侧,child2位于右侧?或者,有没有办法在创建sashform后改变他们的顺序?

Currently my best idea is to put in placeholders like this:

目前我最好的想法是放置这样的占位符:

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);
    }    
}

1 个解决方案

#1


13  

When you create child1, check if child2 has already been instantiated. If it is, it means child1 is on the right, because it has been created later, so you have to do this:

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

child1.moveAbove( child2 );

Hope it helps.

希望能帮助到你。

#1


13  

When you create child1, check if child2 has already been instantiated. If it is, it means child1 is on the right, because it has been created later, so you have to do this:

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

child1.moveAbove( child2 );

Hope it helps.

希望能帮助到你。