如何在SWT中使水平扩展的控件紧挨着固定宽度的控件
我想完成以下任务:
其中一个固定宽度控件与填充的水平扩展控件相邻可用空间。
我已经能够使用 GridLayout
来垂直地完成此操作:
使用以下代码:
parent.setLayout(new GridLayout());
Button button1 = new Button(parent, SWT.PUSH);
button1.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false));
button1.setText("First");
Button button2 = new Button(parent, SWT.PUSH);
button2.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
button2.setText("Second");
有没有办法在不明确指定列数的情况下进行水平版本?其他 GUI 工具包(例如 Android、QT 和 GTK)确实有助于实现这一目标。
目前我的横向解决方案是这样的:
parent.setLayout(new GridLayout(2, false));
Button button1 = new Button(parent, SWT.PUSH);
button1.setLayoutData(new GridData(GridData.BEGINNING, GridData.FILL, false, true));
button1.setText("First");
Button button2 = new Button(parent, SWT.PUSH);
button2.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
button2.setText("Second");
I would like to accomplish the following:
Where one fixed-width control is adjacent to a horizontally-expanding control that fills the available space.
I am already able to use GridLayout
to accomplish this vertically:
by using the following code:
parent.setLayout(new GridLayout());
Button button1 = new Button(parent, SWT.PUSH);
button1.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false));
button1.setText("First");
Button button2 = new Button(parent, SWT.PUSH);
button2.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
button2.setText("Second");
Is there a way to do the horizontal version without having to explicitly specify the number of columns? Other GUI toolkits (e.g. Android, QT, and GTK) do facilitate such a thing.
Currently my horizontal solution is this:
parent.setLayout(new GridLayout(2, false));
Button button1 = new Button(parent, SWT.PUSH);
button1.setLayoutData(new GridData(GridData.BEGINNING, GridData.FILL, false, true));
button1.setText("First");
Button button2 = new Button(parent, SWT.PUSH);
button2.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
button2.setText("Second");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须指定 GridLayout 中的列数。我不明白为什么这应该是一个问题(而且你所拥有的似乎有效)。您可以根据需要计算列数,具体取决于您拥有的小部件数量。
这篇 SWT 布局文章 相当不错。如果您想控制第一个对象的大小,请在
GridData
中指定widthHint
。You have to specify the number of columns in
GridLayout
. I don't understand why that should be a problem (and what you have seems to work). You can compute the number of columns as necessary depending on how many widgets you have.This SWT Layout article is quite good. If you want to control the size of your first object, specify a
widthHint
in theGridData
.