如何使用 swing 中的 GridLayout 使我的列具有不同的大小?
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想要这种效果,那么您需要使用 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.