【发布时间】:2021-08-30 19:33:39
【问题描述】:
将所有小部件放在一个容器中时,网格布局可以正常对齐。
但是由于某种原因,我需要将一些小部件放入子容器中,例如下面的组合。对齐方式不同。
那么问题来了:有没有对gridlayout没有影响的容器?
Ps:我知道可以使用空白作为解决方法,但是...
代码:
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
GridLayoutFactory.fillDefaults().numColumns(2).margins(8, 3).applyTo(shell);
new Label(shell, 0).setText("Label1");
new Text(shell, 0);
new Label(shell, 0).setText("A long Label");
new Text(shell, 0);
Composite composite = new Composite(shell, 0);
GridDataFactory.fillDefaults().span(2, 1).applyTo(composite);
GridLayoutFactory.fillDefaults().numColumns(2).applyTo(composite);
new Label(composite, 0).setText("Label22");
new Text(composite, 0);
new Label(composite, 0).setText("Label223");
new Text(composite, 0);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
【问题讨论】:
-
不,没有这样的容器。如果您希望列对齐,您真的必须坚持使用单个复合材料。使用空格不是一种解决方法 - 空格的宽度会因字体而异,并且无法使其在不同的平台上工作。
-
是的,所以我不想使用空格。
-
您可以使用 GridData 宽度提示(
GridDataFactory中的hint方法)为所有标签指定固定宽度,但处理提示的值有点混乱。
标签: swt grid-layout