如何使用GridBagConstraints创建布局?
我想像这样布局我的 JPane:
-------
| |
| |
| |
-------
| |
-------
这样,顶部部分比底部部分更大/更高(顶部部分由另一个 JPanel 组成并使用 Graphics 对象来显示图像,而底部部分也由另一个 JPanel 组成但使用 Graphics 对象来绘制一些线条和文本)。
我听说最好的方法是使用 GridBagLayout 和 GridBagConstraints。
我正在尝试找出 GridBagConstraints 的适当属性,但遇到了一些困难。这就是我到目前为止所拥有的...
对于顶部部分,我有:
gridx = 0
gridy = 0
weighty = 1.0; // expand downwards, because the bottom should never expand in the Y direction
fill = GridBagConstraints.BOTH
对于底部部分,我有:
gridx = 0
gridy = 1
fill = GridBagConstraints.HORIZONTAL
anchor = GridBagConstraints.PAGE_END
不幸的是,最终发生的所有事情都是出现一个大的灰色矩形(我的应用程序有白色背景)-没有图像加载,没有线条/文本出现。
我应该怎么办?我应该调整什么?
我读过一些教程,但它看起来真的很令人困惑,我在我的第一个应用程序中让它工作,但现在当我尝试这样做时,它似乎对我不起作用。
I want to layout my JPane like so:
-------
| |
| |
| |
-------
| |
-------
This way, the top section is bigger/taller than the bottom section (the top section consists of another JPanel and uses the Graphics object to display an image, while the bottom section also consists of another JPanel but uses the Graphics object to draw some lines and text).
I've heard that the best way to do this was using the GridBagLayout and GridBagConstraints.
I'm trying to figure out the appropriate properties for the GridBagConstraints, and I'm having some difficulties. This is what I have so far...
For the top part, I have:
gridx = 0
gridy = 0
weighty = 1.0; // expand downwards, because the bottom should never expand in the Y direction
fill = GridBagConstraints.BOTH
For the bottom part, I have:
gridx = 0
gridy = 1
fill = GridBagConstraints.HORIZONTAL
anchor = GridBagConstraints.PAGE_END
Unfortunately, all the ends up happening is a large gray rectangle appears (I have a white background for the application) - no images load, no lines/text appear.
What should I do? What should I adjust?
I've read a few tutorials, but it just seems really confusing, I got it working in my first application, but now when I try to do this it just doesn't seem to work for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说,对于gridbag布局
如果你想要一个组件缩放,你必须给它的缩放方向一个权重,并且你为该方向设置的任何尺寸(宽度/高度)将被布局管理器忽略。
如果您不想要组件比例,则必须定义组件的大小(如果您愿意,您可以在java文档中深入研究这个主题)。对于底部面板,您至少需要为其提供一个首选高度。
这可以满足您的期望
此外,如果您想使用 gridbag 布局,我建议您尝试 painless-gridbag 库 http://code.google.com/p/painless-gridbag/(我是该库的作者)。它不能为您解决这个问题(因为您的问题涉及在 gridbag 布局中管理组件的大小),但它会节省您大量的输入并使您的代码更易于维护
In general, for gridbag layout
if you want a component scale, you must give its scale direction a weight, and any sizes (width/height) you set for that direction will be ignored by the layout manager.
If you don't want a component scale, the component must have its size defined (if you want, you can dig into this topic in documents of java). In your case of the bottom panel, you need to give its, at least, a preferred height.
This can work as your expectation
Further more, if you want to use gridbag layout, I recommend you to try the painless-gridbag library http://code.google.com/p/painless-gridbag/ (I'm the author of that library). It doesn't solve this problem for you (as your problem concerns managing component's size in gridbag layout) but it will save you a lot of typing and make your code easier to maintain
从你的问题中不确定,也许会帮助你 70% 的顶部,30% 的底部
not sure from your question, maybe will help you Top at 70%, Bottom at 30%
如果您可以使用第 3 方库,您可以考虑
FormLayout
以及。它允许您指定每行的高度以及调整大小行为。我没有测试这个,但
可能会成功。有关语法的更多信息,请参阅JGoodies Forms pdf
If you may use 3th party libs, you may consider the
FormLayout
as well. It allows you to specify the height of each row, and the resize behavior.I did not test this but
might do the trick. More information on the syntax can be found in the JGoodies Forms pdf