简短回答:
使用StyledText而不是Label - 所有条件都相同。
详细解答:
关于这个“功能”,GTK中存在许多错误(120256是一个很好的切入点),其中大多数都标记为彼此重复,其中大多数都追溯到gtk + 2.4 / 2.6,其中一些标记为固定。然而,在2011年gtk2 + 2.22版本(针对Maverick Market),漏洞似乎非常有弹性。
阅读了这个错误报告之后,我在整个SWT源代码树中找到了wordwrap关键字,看起来Steve Northover男孩们使用StyledText Control比使用Label Control更好地解决了这个问题。
我重用了Favonius测试用例(很多Favonius ;-),这显然是好兆头。
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class RightAlign2
{
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
shell.setLayout(layout);
shell.setText("Alignment Test");
String[] labelTexts = new String[]{"label 1:","Another label:","Another label with wrapped text:"};
for (String labelText : labelTexts)
{
StyledText fieldText = new StyledText(shell, SWT.RIGHT |SWT.WRAP | SWT.BORDER );
fieldText.setText(labelText);
GridData labelGridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
labelGridData.widthHint = 100;
fieldText.setLayoutData(labelGridData);
Text textField = new Text(shell, SWT.BORDER);
GridData textGridData = new GridData(GridData.FILL_HORIZONTAL);
textGridData.horizontalAlignment = GridData.HORIZONTAL_ALIGN_BEGINNING;
textGridData.widthHint = 248;
textField.setLayoutData(textGridData);
}
shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
}