【问题标题】:SWT grid layout issueSWT 网格布局问题
【发布时间】:2018-11-24 15:08:49
【问题描述】:

所以我一直在处理 SWT 对话框。它应该看起来像这样:

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

我已经为 TextBox + Buttons 尝试了 RowLayout 和嵌套组合,但它似乎没有水平填充空间。我也尝试过使用一列和嵌套复合材料以及两列的 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();
        }

【问题讨论】:

    标签: java layout swt


    【解决方案1】:

    您可以在不依赖宽​​度提示的情况下实现这一点(只有在没有其他选项时才应该使用):

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

    看起来像这样:

    小:

    大:

    【讨论】:

    • 谢谢,我试试看!有没有办法阻止文本框垂直扩展?
    • @Alternatic textOne.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    • 2019-10-11
    • 2013-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多