SWT网格布局问题

时间:2018-06-14 19:04:21

标签: java layout swt

所以我一直在研究SWT对话框。看起来应该是这样的:

/----------------------------------\
| Label1                           |
| [_Text_box_1__________][Button1] |
|                                  |
| Label2                           |
| |---------------------|[Button2] |
| |  List 1             |[Button3] |
| |---------------------|          |
|                                  |
| Label3                           |
| |---------------------|[Button4] |
| |  List 2             |[Button5] |
| |---------------------|          |
|                                  |
|                 [ Ok ] [ Cancel] |
\----------------------------------/

我已经尝试使用RowLayout和嵌套复合材料来处理TextBox + Buttons但它似乎没有水平填充空间。我也尝试过使用一列和嵌套复合材料的GridLayout以及两列但它似乎无法工作。

有人可以建议一种可能适合我的方法吗?

编辑: 我已经成功地使用固定大小:

    Display display = parent.getDisplay();
    Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL | SWT.TITLE);
    shell.setText("Text");

        RowLayout layout = new RowLayout(SWT.VERTICAL);
        layout.marginBottom = 15;
        layout.marginTop = 15;
        layout.marginLeft = 15;
        layout.marginRight = 15;
        layout.spacing = 5;
        layout.wrap = true;
        layout.fill = true;
        layout.justify = false;
        shell.setLayout(layout);

        Label label1 = new Label(shell, SWT.LEFT);
        label1.setText("Label 1");
        RowData data = new RowData();
        data.width = 400;
        label1.setLayoutData(data);

        Composite comp1 = new Composite(shell, SWT.NONE);
        layout = new RowLayout(SWT.HORIZONTAL);
        layout.fill = true;
        comp1.setLayout(layout);
        Text text1 = new Text(comp1, SWT.SINGLE | SWT.BORDER);
        data = new RowData();
        data.width = 400;
        text1.setLayoutData(data);
        Button button1 = new Button(comp1, SWT.PUSH | SWT.CENTER);
        button1.setText("Button 1");

        new Label(shell, SWT.NONE);

        Label label2 = new Label(shell, SWT.LEFT);
        label2.setText("Label 2");

        Composite comp2 = new Composite(shell, SWT.NONE);
        layout = new RowLayout(SWT.HORIZONTAL);
        comp2.setLayout(layout);

        List list1 = new List(comp2, SWT.BORDER);
        data = new RowData();
        data.width = 400;
        data.height = 150;
        list1.setLayoutData(data);
        Composite comp3 = new Composite(comp2, SWT.NONE);
        comp3.setLayout(new RowLayout(SWT.VERTICAL));
        Button button2 = new Button(comp3, SWT.PUSH | SWT.CENTER);
        button2.setText("Button 2");
        data = new RowData();
        data.width = 70;
        button2.setLayoutData(data);
        Button button3 = new Button(comp3, SWT.PUSH | SWT.CENTER);
        button3.setText("Button 3");
        button3.setLayoutData(data);

        new Label(shell, SWT.NONE);

        Label label3 = new Label(shell, SWT.LEFT);
        label3.setText("Label 3");

        Composite comp4 = new Composite(shell, SWT.NONE);
        layout = new RowLayout(SWT.HORIZONTAL);
        comp4.setLayout(layout);

        List list2 = new List(comp4, SWT.BORDER);
        data = new RowData();
        data.width = 400;
        data.height = 150;
        list2.setLayoutData(data);
        Composite comp5 = new Composite(comp4, SWT.NONE);
        comp5.setLayout(new RowLayout(SWT.VERTICAL));
        Button button4 = new Button(comp5, SWT.PUSH | SWT.CENTER);
        button4.setText("Button 4");
        data = new RowData();
        data.width = 70;
        button4.setLayoutData(data);
        Button button5 = new Button(comp5, SWT.PUSH | SWT.CENTER);
        button5.setText("Button 5");
        button5.setLayoutData(data);

        new Label(shell, SWT.NONE);

        Composite comp6 = new Composite(shell, SWT.RIGHT_TO_LEFT);
        comp6.setLayout(new RowLayout(SWT.HORIZONTAL));
        Button cancelButton = new Button(comp6, SWT.CENTER);
        cancelButton.setText("Cancel");
        data = new RowData();
        data.width = 70;
        cancelButton.setLayoutData(data);
        Button okButton = new Button(comp6, SWT.PUSH | SWT.CENTER);
        okButton.setText("OK");
        okButton.setLayoutData(data);

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

1 个答案:

答案 0 :(得分:2)

您可以在不退回宽度提示的情况下实现此目的(只有在没有其他选项的情况下才能使用):

public static void main(String[] args)
{
    new MyDialog(new Shell(new Display())).open();
}

private static class MyDialog extends Dialog
{
    protected MyDialog(Shell parentShell)
    {
        super(parentShell);
        setShellStyle(getShellStyle() | SWT.RESIZE);
    }

    @Override
    protected void configureShell(Shell newShell)
    {
        super.configureShell(newShell);
        newShell.setText("Stackoverflow");
    }

    @Override
    protected Control createDialogArea(Composite parent)
    {
        Composite comp = (Composite) super.createDialogArea(parent);

        // Part one
        Label one = new Label(comp, SWT.NONE);
        one.setText("Label1");
        new Label(comp, SWT.NONE);
        Text textOne = new Text(comp, SWT.BORDER);
        textOne.setText("Textbox1");
        Button buttonOne = new Button(comp, SWT.PUSH);
        buttonOne.setText("Button1");

        // Part two
        Label two = new Label(comp, SWT.NONE);
        two.setText("Label2");
        new Label(comp, SWT.NONE);
        List listOne = new List(comp, SWT.BORDER);
        listOne.add("List1");
        Composite buttonCompOne = new Composite(comp, SWT.NONE);
        Button buttonTwoOne = new Button(buttonCompOne, SWT.PUSH);
        buttonTwoOne.setText("Button2");
        Button buttonTwoTwo = new Button(buttonCompOne, SWT.PUSH);
        buttonTwoTwo.setText("Button3");

        // Part three
        Label three = new Label(comp, SWT.NONE);
        three.setText("Label3");
        new Label(comp, SWT.NONE);
        List listTwo = new List(comp, SWT.BORDER);
        listTwo.add("List2");
        Composite buttonCompTwo = new Composite(comp, SWT.NONE);
        Button buttonThreeOne = new Button(buttonCompTwo, SWT.PUSH);
        buttonThreeOne.setText("Button4");
        Button buttonThreeTwo = new Button(buttonCompTwo, SWT.PUSH);
        buttonThreeTwo.setText("Button5");

        // Layout stuff
        comp.setLayout(new GridLayout(2, false));
        GridLayout layout = new GridLayout(1, false);
        layout.marginWidth = 0;
        layout.marginHeight = 0;
        buttonCompOne.setLayout(layout);
        layout = new GridLayout(1, false);
        layout.marginWidth = 0;
        layout.marginHeight = 0;
        buttonCompTwo.setLayout(layout);
        textOne.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        buttonOne.setLayoutData(new GridData(SWT.END, SWT.TOP, false, true));
        listOne.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        listTwo.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        buttonCompOne.setLayoutData(new GridData(SWT.END, SWT.TOP, false, true));
        buttonCompTwo.setLayoutData(new GridData(SWT.END, SWT.TOP, false, true));

        return comp;
    }
}

看起来像这样:

小:

enter image description here

enter image description here

相关问题