如何在SWT中使水平扩展的控件紧挨着固定宽度的控件

发布于 2024-12-23 09:08:33 字数 1124 浏览 0 评论 0原文

我想完成以下任务:

水平扩展

其中一个固定宽度控件与填充的水平扩展控件相邻可用空间。

我已经能够使用 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:

horizontal expansion

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:

vertical expansion

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

李不 2024-12-30 09:08:33

您必须指定 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 the GridData.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文