如何使一些GridLayout列在SWT中增长?

时间:2014-01-16 20:17:12

标签: java layout swt

如何使GridLayout的某些列SWT增长?

我将3个控件放入网格布局中,发现它们没有随窗口增长,尽管我设置了FILL样式。

enter image description here

代码:

public class GridLayout01 {


    public static void main(String[] args) {
        GridLayout gridLayout;

        Display display = new Display();
        Shell shell = new Shell(display);

        gridLayout = new GridLayout(3, false);
        shell.setLayout(gridLayout);

        {

            Label promptLabel = new Label(shell, SWT.NONE);
            promptLabel.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false));
            promptLabel.setText("PROMPT:");

            Text commandLineText = new Text(shell, SWT.SINGLE);
            commandLineText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));

            Button commandLineButton = new Button( shell, SWT.PUSH);
            commandLineButton.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false));
            commandLineButton.setText("Say");

        }


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

1 个答案:

答案 0 :(得分:2)

根据您是希望它垂直增长还是水平增长,您需要按如下方式设置GridData

  • 横向填充:new GridData(SWT.FILL, ?, true, ?)

  • 垂直填充:new GridData(?, SWT.FILL, ?, true)

这是Javadoc:

GridData(int horizontalAlignment,
         int verticalAlignment,
         boolean grabExcessHorizontalSpace,
         boolean grabExcessVerticalSpace)
  • horizontalAlignment - 如何将控件水平放置在单元格中,其中之一是:SWT.BEGINNING(或SWT.LEFT),SWT.CENTERSWT.END(或{ {1}})或SWT.RIGHT
  • SWT.FILL - 如何将控件垂直放置在单元格中,其中之一是:verticalAlignment(或SWT.BEGINNING),SWT.TOPSWT.CENTER(或{ {1}})或SWT.END
  • SWT.BOTTOM - 单元格是否足够宽以适应剩余的水平空间
  • SWT.FILL - 单元格是否足够高以适应剩余的垂直空间

还要确保父母占据了孩子们想要占用的所有空间。