在窗口/外壳中自由定位SWT文本

时间:2014-04-22 22:58:58

标签: java position swt

如何在SWT Shell中定位文字?

我的问题是我不知道要使用哪种布局。我已经尝试过Row和Fill-Layout,但是如果我像text.setLocation(x,y)那样做,这将无效。

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

解决了它。

如果您不使用布局,请将所需的元素放在SWT组中,您可以将其放在此组中。

Group group = new Group(parent, SWT.NONE);
Text text = new Text(group, SWT.BORDER);
group.setSize(parent.getSize());
text.setLocation(parent.getSize().x/2, parent.getSize().y/2);

看起来像这样: enter image description here

答案 1 :(得分:0)

可能值得一看FormLayout。会是这样的:

Group group = new Group(parent, SWT.NONE);
group.setLayout(new FormLayout());

Text text = new Text(group, SWT.BORDER);
text.setText("Text1");

FormData fd = new FormData();
fd.top = new FormAttachment(50,0);
fd.left = new FormAttachment(50,0);

text.setLayoutData(fd);

FormAttachment类允许您指定容器宽度的百分比(在这种情况下为50%),以及固定偏移量(0)。