繁体   English   中英

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

[英]How to make child composite fill parent in 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();
}

这会产生这样的东西:

在此输入图像描述

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

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

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

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

使用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");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM