对齐按钮中的 SWT 问题
SWT issues in Aligning a button
我写了一个小程序,它有 2 个列表视图和一个用于 select 和取消 select selected 组合的按钮。问题是我无法将按钮对齐到列的中心。
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setLayout(new GridLayout(3, true));
GridData data = new GridData(GridData.FILL_BOTH);
// Creating Label
new Label(shell, SWT.NONE).setText("This is a plain Text");
new Label(shell, SWT.NONE).setText("");
new Label(shell, SWT.NONE).setText("This is a plain Text");
// Create a single-selection list
List single = new List(shell, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL);
single.setLayoutData(data);
// Add the items, one by one
for (int i = 0, n = ITEMS.length; i < n; i++) {
single.add(ITEMS[i]);
}
single.setSelection(0);
//////////////////////////////////////////////////////
// Button button1=new Button(shell, SWT.ARROW | SWT.RIGHT);
Group first = new Group(shell, SWT.CENTER);
first.setLayout(new RowLayout(SWT.VERTICAL));
Button button1 = new Button(first, SWT.NONE);
// GridData bdata = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
Button button3 = new Button(first, SWT.PUSH);
// button3.setLayoutData(bdata);
button1.setText("Select ");
button3.setText("UnSelect");
//////////////////////////////////////////////
// Create a single-selection list
List single2 = new List(shell, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL);
single2.setLayoutData(data);
single2.add("");
single2.setSelection(0);
shell.open();
System.out.println(single.getItem(single.getSelectionIndex()));
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
实际输出:
预期输出:
不要尝试在多个控件上重复使用 GridData
,这会导致问题,因为布局信息存储在数据中。您必须为每个控件使用新的 GridData
。所以:
List single = new List(shell, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL);
single.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
List single2 = new List(shell, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL);
single2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
要将按钮集 GridData
置于 Group
的中心:
Group first = new Group(shell, SWT.NONE);
first.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
此 GridData
请求水平和垂直居中。
要使按钮宽度相同,请对组使用 GridLayout
并使用水平填充和抓取 space:
first.setLayout(new GridLayout());
Button button1 = new Button(first, SWT.NONE);
button1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
Button button3 = new Button(first, SWT.PUSH);
button3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
请注意,不再推荐使用 GridData.FILL_BOTH
和类似常量。
我写了一个小程序,它有 2 个列表视图和一个用于 select 和取消 select selected 组合的按钮。问题是我无法将按钮对齐到列的中心。
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setLayout(new GridLayout(3, true));
GridData data = new GridData(GridData.FILL_BOTH);
// Creating Label
new Label(shell, SWT.NONE).setText("This is a plain Text");
new Label(shell, SWT.NONE).setText("");
new Label(shell, SWT.NONE).setText("This is a plain Text");
// Create a single-selection list
List single = new List(shell, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL);
single.setLayoutData(data);
// Add the items, one by one
for (int i = 0, n = ITEMS.length; i < n; i++) {
single.add(ITEMS[i]);
}
single.setSelection(0);
//////////////////////////////////////////////////////
// Button button1=new Button(shell, SWT.ARROW | SWT.RIGHT);
Group first = new Group(shell, SWT.CENTER);
first.setLayout(new RowLayout(SWT.VERTICAL));
Button button1 = new Button(first, SWT.NONE);
// GridData bdata = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
Button button3 = new Button(first, SWT.PUSH);
// button3.setLayoutData(bdata);
button1.setText("Select ");
button3.setText("UnSelect");
//////////////////////////////////////////////
// Create a single-selection list
List single2 = new List(shell, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL);
single2.setLayoutData(data);
single2.add("");
single2.setSelection(0);
shell.open();
System.out.println(single.getItem(single.getSelectionIndex()));
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
实际输出:
预期输出:
不要尝试在多个控件上重复使用 GridData
,这会导致问题,因为布局信息存储在数据中。您必须为每个控件使用新的 GridData
。所以:
List single = new List(shell, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL);
single.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
List single2 = new List(shell, SWT.BORDER | SWT.SINGLE | SWT.V_SCROLL);
single2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
要将按钮集 GridData
置于 Group
的中心:
Group first = new Group(shell, SWT.NONE);
first.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, true));
此 GridData
请求水平和垂直居中。
要使按钮宽度相同,请对组使用 GridLayout
并使用水平填充和抓取 space:
first.setLayout(new GridLayout());
Button button1 = new Button(first, SWT.NONE);
button1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
Button button3 = new Button(first, SWT.PUSH);
button3.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
请注意,不再推荐使用 GridData.FILL_BOTH
和类似常量。