Table 和工具栏在同一个 shell

Table and toolbar on same shell

我需要帮助如何将工具栏和 Table 放在同一个 shell SWT window.

我遇到的问题是,如果我把它们都放在表格上,它们每个都覆盖了 shell window:

的一半
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.CoolBar;
import org.eclipse.swt.widgets.ToolItem;


public class MainLayout {

    protected Shell shell;
    private Table table;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        try {
            MainLayout window = new MainLayout();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    /**
     * Create contents of the window.
     */
    protected void createContents() {
        shell = new Shell();
        shell.setSize(450, 300);
        shell.setText("SWT Application");
        shell.setLayout(new FillLayout(SWT.HORIZONTAL));

        Menu menu = new Menu(shell, SWT.BAR);
        shell.setMenuBar(menu);

        MenuItem menuItem = new MenuItem(menu, SWT.CASCADE);
        menuItem.setText("New SubMenu");

        Menu menu_2 = new Menu(menuItem);
        menuItem.setMenu(menu_2);

        table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
        table.setHeaderVisible(true);
        table.setLinesVisible(true);

        //ToolBar toolBar = new ToolBar(shell, SWT.FLAT | SWT.RIGHT);
        ToolBar toolBar = new ToolBar(shell, SWT.HORIZONTAL);

        ToolItem item = new ToolItem(toolBar, SWT.PUSH);
        item.setText("Button One");



    }
}

你能帮我解决这个问题吗?

改为使用 GridLayout。您还需要设置 GridData 以自定义小部件布局。

示例代码:

protected void createContents() {
    shell = new Shell();
    shell.setSize(450, 300);
    shell.setText("SWT Application");
    shell.setLayout(new GridLayout(1,false)); // change

    Menu menu = new Menu(shell, SWT.BAR);
    shell.setMenuBar(menu);

    MenuItem menuItem = new MenuItem(menu, SWT.CASCADE);
    menuItem.setText("New SubMenu");

    Menu menu_2 = new Menu(menuItem);
    menuItem.setMenu(menu_2);

    table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION );
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));  // change

    //ToolBar toolBar = new ToolBar(shell, SWT.FLAT | SWT.RIGHT);
    ToolBar toolBar = new ToolBar(shell, SWT.HORIZONTAL);

    ToolItem item = new ToolItem(toolBar, SWT.PUSH);
    item.setText("Button One");
}

参考文献: