我正在将我们应用程序中的许多SWT标签转换为文本小部件,因为我们需要使我们的软件更易于访问。屏幕阅读器很难读取标签值,但如果我们使用只读文本小部件,则可以轻松完成。
将gridlayout中的多个标签转换为只读文本后,我们似乎无法将文本垂直居中。下面是一个简单的计算器示例,我们看到了相同的行为。我在GridData和Text上玩过SWT.CENTER,SWT.FILL,但似乎无法使控件垂直居中。
public void initUI() {
GridLayout g1 = new GridLayout(4, true);
g1.horizontalSpacing = 4;
g1.verticalSpacing = 4;
g1.marginBottom = 5;
g1.marginTop = 5;
shell.setLayout(g1);
String[] buttons = {
"Cls", "Bck", "", "Close", "7", "8", "9", "/", "4",
"5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"
};
Text display = new Text(shell, SWT.SINGLE);
GridData gridData = new GridData();
gridData.horizontalSpan = 4;
gridData.horizontalAlignment = GridData.FILL;
display.setLayoutData(gridData);
for (int i = 0; i < buttons.length; i++) {
if (i == 2) {
Label lbl = new Label(shell, SWT.CENTER);
GridData gd = new GridData(SWT.FILL, SWT.FILL, false, false);
lbl.setLayoutData(gd);
} else {
Text btn = new Text(shell,SWT.SINGLE | SWT.READ_ONLY | SWT.BORDER);
btn.setText(buttons[i]);
GridData gd = new GridData(SWT.CENTER, SWT.CENTER, true, true);
gd.widthHint = 50;
gd.heightHint = 30;
btn.setLayoutData(gd);
btn.selectAll();
}
}
}
答案 0 :(得分:2)
我认为你的意思是你希望文本水平居中(它已经垂直居中)。进行此更改后,它将以两种方式居中(我将SWT.CENTER
添加到Text
):
Text btn = new Text(shell, SWT.SINGLE | SWT.READ_ONLY
| SWT.BORDER | SWT.CENTER);
答案 1 :(得分:0)
这是一个适合我的简单方法。
Label label = new Label(composite, SWT.CENTER);
label.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
label.setText("\nText");
hack是为文本后缀新行(\ n)。