FormAttachment (Control control)的参数必须是属于同一个composite

总的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).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值