簡體   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