如何使用 swing 中的 GridLayout 使我的列具有不同的大小?

发布于 2024-11-16 23:04:39 字数 655 浏览 5 评论 0原文

我正在使用 GridLayout,我的代码如下:

int changingVar = 1;

JPanel panel = new JPanel(new GridLayout(changingVar, 2));
panel.add(new JButton("BUTTON1"));
panel.add(new JButton("BUTTON2"));

这看起来像:

___________________________________________
| [      BUTTON1     ] [     BUTTON2     ] |
___________________________________________

这是两个大小均匀的列。我想这样做:

___________________________________________
| [          BUTTON1         ] [ BUTTON2 ] |
___________________________________________

其中一列比另一列占用更多的面板空间。我如何使用网格布局来做到这一点?我不反对使用另一种布局,只要我可以有不同数量的两种不同大小的行和列。

谢谢

I'm using a GridLayout and my code is as follows:

int changingVar = 1;

JPanel panel = new JPanel(new GridLayout(changingVar, 2));
panel.add(new JButton("BUTTON1"));
panel.add(new JButton("BUTTON2"));

This looks like:

___________________________________________
| [      BUTTON1     ] [     BUTTON2     ] |
___________________________________________

which is two evenly sized columns. I would like to make it like this:

___________________________________________
| [          BUTTON1         ] [ BUTTON2 ] |
___________________________________________

in which one column takes up more of the panel space then the other. How do I do this with gridlayout? I am not oppose to using another layout as long as I can have a varying amount of rows and columns that are two different sizes.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

错爱 2024-11-23 23:04:39

如果您想要这种效果,那么您需要使用 GridBagLayout。

http://download.oracle.com/javase/tutorial/uiswing/layout /gridbag.html

玩得开心=P

编辑:

您可以通过混合使用 FlowLayout 和 GridLayout 来解决该问题,以获得类似的效果。然而,随着布局复杂性的增加,这个解决方案将变得极其乏味和混乱。

If you want this effect then you need to utilize the GridBagLayout.

http://download.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

Have fun with that one =P

EDIT:

You can work around the problem by employing a mixture of FlowLayout and GridLayout to get a similar effect. However, this solution will become extremely tedious and messy as your layout complexities become bigger.

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