我正在为复合容器使用 FormLayout。 当我添加两个子项 label 和 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);
最佳答案
formData4ClientArea.right = new FormAttachment(label,-5);
此时,label
为null
。它没有实例化。所以基本上你将 clientArea 附加到任何东西上。如果你想将clientArea
附加到label
,ofc你需要先实例化label
,然后是clientArea
但是,另一方面,为什么顺序对您很重要?
关于Java、SWT、FormLayout - 为什么我添加子项的顺序很重要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12049890/