如何从java中的formAttachment中读取值?

时间:2012-08-05 14:48:40

标签: java swt

如何从Java中的formAttachment读取值?

这是我的代码:

Text one = new Text(composite, SWT.BORDER);
data = new FormData();
data.top = new FormAttachment(0, 0);
data.bottom = new FormAttachment(100, 0);
data.left = new FormAttachment(0, 0);
data.right = new FormAttachment(sash, 0);
one.setLayoutData(data);

结果:

one is left

1 个答案:

答案 0 :(得分:2)

FormAttachment用于定位Control。您可以使用左侧,顶部,右侧或底部的FormAttachment来修复控件的边缘。所有剩余边缘都会自动计算。 最简单的可能性是相对于周围复合材料边缘的百分比定位。这是一个例子:

FormData formData = new FormData();
// Fix the left edge of the control to 25% of the overall width + 10px offset.
formData.left = new FormAttachment(25, 10);
// Fix the lower edge of the control to 75% of the overall height + 0px offset.
formData.bottom = new FormAttachment(75);
// Tell the control its new position.
control.setLayoutData(formData);

或者,您可以使用构造函数new FormAttachment(control, offset, alignment)将控件的相对边缘固定到另一个控件的边缘:

FormData formData = new FormData();
// Fix left edge 10px to the right of the right edge of otherControl
formData.left = new FormAttachment(otherControl, 10, SWT.RIGHT);
// Fix bottom edge at exactly the same height as the one of otherControl
formData.bottom = new FormAttachment(otherControl, 0, SWT.BOTTOM);
control.setLayoutData(formData);

Ralf Ebert here有一本非常好的Eclipse RCP手册。不幸的是它是德语。但是,您可以在第56-57页找到解释上述示例的图像。