我正在尝试有两个文本框,一个文本框占据SWT中屏幕空间的3/4,另一个文本框占据SWT中屏幕空间的1/4。

我正在使用网格布局,如下所示:

    GridLayout gridLayout = new GridLayout();
    gridLayout.numColumns = 1;
    final Text text0 = new Text (shell, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
    final Text text1 = new Text (shell, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
    shell.setLayout(gridLayout);
    text0.setLayoutData(new GridData(GridData.FILL_BOTH));
    text1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL,200)); //this line needs some help


当前,第一个文本框占据了3/4的空间,但是第二个文本框没有占据整个水平的空间。

谢谢!

最佳答案

如果要谈论水平空间,请使用4列网格,并使第一个文本跨3列:

// 4 equals sized columns
shell.setLayout(new GridLayout(4, true));

final Text text0 = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);

// First text spans 3 columns
text0.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1));

final Text text1 = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);

// Second text is single column
text1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));


垂直划分空间要困难得多,因为将GridData的行跨度字段与“抓取多余空间”字段一起使用不太好。我能想到的最好的方法是使用虚拟Label控件来获取四个等于行:

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

new Label(shell, SWT.LEAD).setLayoutData(new GridData(SWT.BEGINNING, SWT.FILL, false, true));

final Text text0 = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);

// First text spans 3 rows
text0.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 3));

new Label(shell, SWT.LEAD).setLayoutData(new GridData(SWT.BEGINNING, SWT.FILL, false, true));
new Label(shell, SWT.LEAD).setLayoutData(new GridData(SWT.BEGINNING, SWT.FILL, false, true));
new Label(shell, SWT.LEAD).setLayoutData(new GridData(SWT.BEGINNING, SWT.FILL, false, true));

final Text text1 = new Text(shell, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);

// Second text is single row
text1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

10-07 22:36