1

我正在为复合容器使用 FormLayout。当我添加两个子标签clientArea时,其中 clientArea 取决于标签 - 只有在我首先添加标签时才会出现 clientArea。

在容器上调用 layout() 后,添加子项无济于事 - clientArea 不显示。

如何将子级添加到 FormLayout 控制的容器中,独立于它们彼此的依赖关系?

MyLabel label;
Composite clientArea;   

public MyContainer(Composite parent, int style) {
    super(parent,style);

    //choose the container Layout
    FormLayout layout = new FormLayout();
    this.setLayout(layout);


    clientArea = new Composite(this, SWT.NONE);
    FormData formData4ClientArea = new FormData();
    formData4ClientArea.left = new FormAttachment(0,0);
    formData4ClientArea.top = new FormAttachment(0,5);
    formData4ClientArea.right = new FormAttachment(label,-5);
    formData4ClientArea.bottom = new FormAttachment(100,-5);
    //set the Formdata
    clientArea.setLayoutData(formData4ClientArea);
    clientArea.setBackground(getDisplay().getSystemColor(SWT.COLOR_GREEN));     


    //create the label
    label = new MyLabel(this, SWT.NONE);
    FormData formData4Label = new FormData();
    formData4Label.top = new FormAttachment(0,5);
    formData4Label.right = new FormAttachment(100,-5);
    formData4Label.bottom = new FormAttachment(100,-5);
    //set the FormData
    label.setLayoutData(formData4Label);
4

2 回答 2

5

formData4ClientArea.right = new FormAttachment(label,-5);此时,labelnull。它没有被实例化。因此,基本上您将 clientArea 附加到任何内容。如果你想clientArea附加到label,你需要先实例化label,然后clientArea

但是,另一方面,为什么顺序对你很重要?

于 2012-08-21T07:20:31.680 回答
1

事实上,实例化组件的顺序并不重要。

您不能做的是在创建对象之前引用它。正如 Plygnome 所说,问题在于当您创建 时FormAttachmentLabel就是null

在我们的项目中,我们首先创建所有组件,然后创建它们的所有布局数据对象。

于 2012-08-21T11:41:27.740 回答