简体   繁体   English

如何在 Java/SWT 中将 label 放置在 window 的底部

[英]How do I place a label at the bottom of a window in Java/SWT

I am writing a Java GUI application using SWT, which shows a large table with hundreds of rows.我正在使用 SWT 编写一个 Java GUI 应用程序,它显示了一个包含数百行的大表。 At the bottom of the window, I want to have a fixed label showing some status information.在 window 的底部,我想要一个固定的 label 显示一些状态信息。

My program looks like this:我的程序如下所示:

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();
  }

} }

If I am using a GridLayout , the label is located below the table, but depending on the size of the table and the window size, the label is not visible.如果我使用的是GridLayout ,则 label 位于表格下方,但根据表格的大小和 window 的大小,label 不可见。 If I enlarge the window, the label appears, but does not stay at the bottom of the window but always just below the table.如果我放大 window,label 会出现,但不会停留在 window 的底部,而是始终位于表格下方。 And the table is not scrollable, ie I cannot see the bottom lines of my large table.并且表格不可滚动,即我看不到我的大表格的底线。

If I am using the FillLayout , the table is scrollable, but the label occupies the right half of my window.如果我使用FillLayout ,表格是可滚动的,但 label 占据了我的 window 的右半部分。

Any advise, how I force my label at the bottom of my window and still have table scrollable?有什么建议,我如何强制我的 label 在我的 window 的底部并且仍然有可滚动的表格?

With GridLayout you can do something like:使用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);

Or have the table grab the excess vertical space:或者让桌子抓住多余的垂直空间:

GridData gridData = new GridData(SWT.FILL, SWT.TOP, false, 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, SWT.END, true, false);
statusLine.setLayoutData(gridData);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM