如何解决如何在 Java/SWT 中的窗口底部放置标签
我正在使用 SWT 编写一个 Java GUI 应用程序,它显示了一个包含数百行的大表。 在窗口底部,我希望有一个固定标签显示一些状态信息。
我的程序是这样的:
public class Test {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display,SWT.SHELL_TRIM | SWT.V_SCROLL);
FillLayout fillLayout = new FillLayout();
shell.setLayout(fillLayout);
// GridLayout gridLayout = new GridLayout();
// shell.setLayout(gridLayout);
final Table table = new Table(shell,SWT.NONE);
table.setHeaderVisible(true);
final String header[] = {"Feld 1","Feld 2","Feld 3"};
table.setLinesVisible(true);
for (int i = 0; i < 3; i++) {
TableColumn column = new TableColumn(table,SWT.NONE);
column.setText(header[i]);
column.setWidth(100);
}
for (int i = 0; i < 4; i++) {
TableItem item = new TableItem(table,SWT.NONE);
item.setText(new String[] { "a" + i,"b" + i,"c" + i });
}
GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL);
gridData.verticalSpan = 3;
table.setLayoutData(gridData);
Label statusLine = new Label(shell,SWT.NULL);
statusLine.setText("This shall be the status line");
gridData = new GridData(GridData.VERTICAL_ALIGN_END);
gridData.horizontalSpan = 3;
statusLine.setLayoutData(gridData);
shell.setSize(400,100);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
如果我使用 GridLayout,标签位于表格下方,但根据表格的大小和窗口大小,标签不可见。如果我放大窗口,标签会出现,但不会停留在窗口底部,而是始终位于表格下方。并且表格不可滚动,即我看不到大表格的底线。
如果我使用 FillLayout,表格是可滚动的,但标签占据了我窗口的右半部分。
任何建议,我如何在窗口底部强制我的标签并且仍然可以滚动表格?
解决方法
使用 GridLayout
,您可以执行以下操作:
GridLayout gridLayout = new GridLayout();
shell.setLayout(gridLayout);
.... your table code
// To get the table to scroll you have to give a height hint
GridData gridData = new GridData(SWT.FILL,SWT.TOP,false,false);
gridData.heightHint = 100;
table.setLayoutData(gridData);
Label statusLine = new Label(shell,SWT.NULL);
statusLine.setText("This shall be the status line");
// Set the label grid data to grab the remaining space and put the label at the bottom
gridData = new GridData(SWT.FILL,SWT.END,true,true);
statusLine.setLayoutData(gridData);
shell.setSize(400,200);
或者让桌子抓住多余的垂直空间:
GridData gridData = new GridData(SWT.FILL,true);
gridData.heightHint = 100;
table.setLayoutData(gridData);
Label statusLine = new Label(shell,SWT.NULL);
statusLine.setText("This shall be the status line");
gridData = new GridData(SWT.FILL,false);
statusLine.setLayoutData(gridData);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。