Java、SWT、FormLayout—为什么我添加子项的顺序很重要?
我正在为复合容器使用FormLayout。 当我添加两个子项label和clientArea时,clientArea取决于label- clientArea仅在我首先添加标签时显示 在添加子对象后,在容器上调用layout()没有帮助-clientArea不会显示 如何将子级添加到FormLayout控制的容器中,而不依赖于它们之间的依赖关系Java、SWT、FormLayout—为什么我添加子项的顺序很重要?,java,swt,form-layout,Java,Swt,Form Layout,我正在为复合容器使用FormLayout。 当我添加两个子项label和clientArea时,clientArea取决于label- clientArea仅在我首先添加标签时显示 在添加子对象后,在容器上调用layout()没有帮助-clientArea不会显示 如何将子级添加到FormLayout控制的容器中,而不依赖于它们之间的依赖关系 MyLabel label; Composite clientArea; public MyContainer(Composite parent,
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=新格式附件(标签,-5)
此时,标签
为null
。它没有实例化。所以基本上你什么都不做。如果要将clientrea
附加到label
,ofc需要先实例化label
,然后实例化clientrea
但是,另一方面,为什么顺序对您很重要?
formData4ClientArea.right=newformattachment(label,-5)
此时,标签
为null
。它没有实例化。所以基本上你什么都不做。如果要将clientrea
附加到label
,ofc需要先实例化label
,然后实例化clientrea
但是,另一方面,为什么顺序对您很重要?事实上,您实例化组件的顺序并不重要 您不能在对象创建之前引用它。正如Plygnome所述,问题在于当您创建
格式附件
时,标签
为null
在我们的项目中,我们首先创建所有组件,然后创建它们的所有布局数据对象。事实上,实例化组件的顺序并不重要 您不能在对象创建之前引用它。正如Plygnome所述,问题在于当您创建
格式附件
时,标签
为null
在我们的项目中,我们首先创建所有组件,然后创建它们的所有布局数据对象。许多布局(例如GridLayout
说明了添加顺序,就好像它们是布局约束一样。许多布局(例如GridLayout
说明了添加顺序,就好像它们是布局约束一样。