gpt4 book ai didi

java - 如何在swt中使子复合填充父

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:44:42 24 4
gpt4 key购买 nike

这是我的代码:

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%。我应该检查哪些可能性来实现这一点?

最佳答案

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

关于java - 如何在swt中使子复合填充父,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28743238/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com