如何在swt中创建子复合填充父级

时间:2015-02-26 13:03:43

标签: java swt

这是我的代码:

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

    Composite childComposite = new Composite(shell, SWT.BORDER);
    childComposite.setLayout(new GridLayout(2, false));
    Text text1 = new Text(childComposite, SWT.NONE);
    Text text2 = new Text(childComposite, SWT.NONE );

    Label label = new Label(shell, SWT.NONE);
    label.setText("Very loooooooooooooooooooooooong text");

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

这会产生这样的结果:

enter image description here

我的问题是如何让我的子复合材料水平填充父级(至少与下面标签的宽度相同)。 我试过用这样的东西:

Composite childComposite = new Composite(shell, SWT.BORDER | SWT.FILL);

......但这并没有改变任何事情。

此外,当孩子与父母的宽度相同时,我希望文本窗口小部件也填充他们所在的复合体,但宽度不同 - 例如,第一个是20%,第二个是80%。 我应该检查什么可能来实现这个目标?

1 个答案:

答案 0 :(得分:3)

使用GridLayout时,您使用GridData参数来控制setLayoutData方法,以指定网格的填充方式。

你想要这样的东西:

  shell.setLayout(new GridLayout(1, false));

  Composite childComposite = new Composite(shell, SWT.BORDER);

  // Composite fills the grid row
  childComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));

  // Use 5 equal sized columns
  childComposite.setLayout(new GridLayout(5, true));

  // First text fills the first column
  Text text1 = new Text(childComposite, SWT.NONE);
  text1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));

  // Second text fills the next 4 columns
  Text text2 = new Text(childComposite, SWT.NONE);
  text2.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 4, 1));

  Label label = new Label(shell, SWT.NONE);
  label.setText("Very loooooooooooooooooooooooong text");