总的Composite使用的是FormLayout,其中上面一个子Composite使用的是GridLayout,三个button所在的Composite命名为buttonComp. 要想设置下面的text在buttonComp的左边,是不可行的。
Composite comp = new Composite(parent, SWT.BORDER);
comp.setLayout(new FormLayout());
Composite aboveComp = createAboveComposite(comp);
Text textBelow = new Text(comp, SWT.BORDER);
textBelow.setText("Below");
textBelow.setToolTipText("Below tool tip");
FormData aboveCompFormData = new FormData();
aboveCompFormData.top = new FormAttachment(0);
aboveCompFormData.left = new FormAttachment(0);
aboveCompFormData.right = new FormAttachment(100);
aboveComp.setLayoutData(aboveCompFormData);
FormData textFormData = new FormData();
textFormData.top = new FormAttachment(aboveComp);
textFormData.left = new FormAttachment(0);
textFormData.right = new FormAttachment(buttonComp);
textBelow.setLayoutData(textFormData);
实际运行的效果是下面的Text不见了。这是因为在FormData.getRightAttachment方法中
FormAttachment getRightAttachment (Control control, int spacing, boolean flushCache) {
if (cacheRight != null) return cacheRight;
if (isVisited) return cacheRight = new FormAttachment (0, getWidth (control, flushCache));
if (right == null) {
if (left == null) return cacheRight = new FormAttachment (0, getWidth (control, flushCache));
return cacheRight = getLeftAttachment (control, spacing, flushCache).plus (getWidth (control, flushCache));
}
Control rightControl = right.control;
if (rightControl != null) {
if (rightControl.isDisposed ()) {
right.control = rightControl = null;
} else {
if (rightControl.getParent () != control.getParent ()) {//must be same parent
rightControl = null;
}
}
}
计算出来的numerator为0
如果上面的Composite是遗留的类,不能更改。想要实现下面的Text在buttonComp的左边,可以在下面的Composite中建相同的buttonComp, setVisible(false).