2012-08-05 78 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