Java 如何在 Swing 中使用 GridLayout 使我的列的大小不同?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6471502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:14:28  来源:igfitidea点击:

How can I make my columns different sizes using GridLayout in swing?

javaswinggrid-layout

提问by Grammin

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

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

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.

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

Thanks

谢谢

采纳答案by Feisty Mango

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

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

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

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

Have fun with that one =P

玩得开心=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.

您可以通过混合使用 FlowLayout 和 GridLayout 来解决此问题,以获得类似的效果。但是,随着您的布局复杂性变得更大,此解决方案将变得非常乏味和混乱。