2010-05-06 31 views
11

在我的情况下,我有两个SashForm的孩子,但该问题适用于所有Composite s。更改SWT复合材料儿童的顺序

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

回答

13

当您创建child1,检查的child2已经被实例化。如果是,则意味着child1是在右边,因为它已经被后来创建的,所以你必须要做到这一点:

child1.moveAbove(child2); 

希望它能帮助。

+0

它的确如此。我错过了'moveAbove'的存在,因为我正在查看'Composite'的方法。 – 2010-05-06 12:23:03

+1

感谢此信息:)要注意的一件好事是您可以使用null将其移动到顶部。 – 2015-09-29 06:50:41