问题是我无法设置标签的高度。对于长文本,我的标签应该有2行的高度,而短文本只有1行。有一个代码示例:
public MyComposite(Composite parent) {
super(parent, SWT.NONE);
setLayout(new GridLayout(1, false));
description = new Label(this, SWT.WRAP);
description.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
description.setText("Looooooooooooooooooooooooooooooooooooooo" +
"oooooooooooooooooooooooooooooooooooooooooooooooooooooo" +
"ooooooooooooooooooooooooooooooooooooong text");
}
要在GridLayout中启用换行文本,必须将GridData(grabExcessHorizontalSpace
)的第三个参数设置为true
。还要确保父组合的大小由其layoutData定义,否则它将由Label展开。以下代码段包装了标签文本:
Shell shell = new Shell( parentShell );
shell.setSize( 400, 300 );
shell.setLayout( new GridLayout(1, false) );
Label description = new Label(shell, SWT.WRAP);
description.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false));
description.setText("Looooooooooooooooooooooooooooooooooooooo" +
"oooooooooooooooooooooooooooooooooooooooooooooooooooooo" +
"ooooooooooooooooooooooooooooooooooooong text");
shell.open();
还要注意,换行是作为单词换行来实现的,一个很长的非空白字符序列不会被分割成行。