设计特定的布局

时间:2013-12-10 14:10:13

标签: java layout swt

我需要创建一个看起来像图片中的布局,但我迷失了。

enter image description here

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

让我给你一个起点:

public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));
    shell.setText("StackOverflow");

    Composite contentHolder = new Composite(shell, SWT.NONE);
    contentHolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    contentHolder.setLayout(new GridLayout(1, false));

    createMenuBar(shell);
    createContent(contentHolder);

    shell.pack();
    shell.setSize(600, 400);
    shell.open();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }

    display.dispose();
}

private static void createContent(Composite parent)
{
    Button button = new Button(parent, SWT.NONE);
    button.setText("connect");
    button.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));

    Canvas canvas = new Canvas(parent, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
    canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
}

private static void createMenuBar(final Shell shell)
{
    Menu menuBar = new Menu(shell, SWT.BAR);
    MenuItem fileItem = new MenuItem(menuBar, SWT.CASCADE);
    fileItem.setText("File");
    MenuItem helpItem = new MenuItem(menuBar, SWT.CASCADE);
    helpItem.setText("Action");

    Menu fileMenu = new Menu(menuBar);
    fileItem.setMenu(fileMenu);
    MenuItem exitItem = new MenuItem(fileMenu, SWT.NONE);
    exitItem.setText("Exit");

    Menu aboutMenu = new Menu(menuBar);
    helpItem.setMenu(aboutMenu);
    MenuItem aboutItem = new MenuItem(aboutMenu, SWT.NONE);
    aboutItem.setText("Some action");

    shell.setMenuBar(menuBar);
}

看起来像这样:

enter image description here

如果您确实需要左侧的垂直滚动条,请查看this