作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这是我的代码:
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");
关于java - 如何在swt中使子复合填充父,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28743238/
我是一名优秀的程序员,十分优秀!