2012-08-05 79 views
1

如何从Java中的formAttachment读取值?如何从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

+0

BTW:你想读哪些值? FormAttachment的'numerator'和'offset'值或'Text'中包含的文本? – Baz 2012-08-05 15:11:23

+0

分子和偏移 – Yrais 2012-08-05 16:54:39

回答

1

FormAttachment s的用来定位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); 

Alternativelyyou可以使用构造器new FormAttachment(control, offset, alignment)修复控制relativ的边缘到另一个控制的边缘:

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); 

有拉尔夫一个很好的Eclipse RCP手册Ebert here。不幸的是它是用德语。但是,您可以在第56-57页上找到解释上述示例的图像。

+0

并非如此。 我的意思是如何读取分子和偏移量。 看图片。 为什么“one.bottom”的值是100 ?. 完整的代码在http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/DemonstratesaSash3.htm – Yrais 2012-08-05 17:07:14

+0

@Yrais如果你只是想了解为什么他们在这个例子中使用'100',我可以帮不了你。不过,我会建议使用'SashForm',它更容易使用:http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/SashFormExample.htm – Baz 2012-08-05 17:09:26

+0

并非如此。 看图片。 dat.top = new FormAttachment(0.0)。 “0”,将其分配给图片的大小?如果(0.0)是(x,y)或者x和y位置,则为 。我能够了解。 但是这个(分子,偏移量)。 – Yrais 2012-08-05 17:44:44