Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 同一外壳上的表和工具栏_Java_Eclipse_Shell_Toolbar - Fatal编程技术网

Java 同一外壳上的表和工具栏

Java 同一外壳上的表和工具栏,java,eclipse,shell,toolbar,Java,Eclipse,Shell,Toolbar,我需要帮助如何将工具栏和表放在同一个shell SWT窗口上 我遇到的问题是,如果我把它们都放在表格上,它们中的每一个都覆盖了外壳窗口的一半: 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.Menu

我需要帮助如何将工具栏和表放在同一个shell SWT窗口上

我遇到的问题是,如果我把它们都放在表格上,它们中的每一个都覆盖了外壳窗口的一半:

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");



    }
}

你能帮我一下吗?

改用网格布局。您还需要设置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");
}
参考资料:


您希望看到什么?您希望您的布局是怎样的?“普通”窗口,工具栏(带按钮)位于顶部,表格位于其下方(表格应填充shell窗口的其余部分)。