使两个 Jpanel 在 JFrame 中不对称展开
我有一个 JFrame,里面有两个 JPanel。一个设置在西边,另一个设置在东边,带有 BorderLayout。问题是,它只显示两个 10 像素宽度、100% JFrame 高度的条带: 替代文本 http://img641.imageshack.us/img641/3298/imagewg.png< /a>
我想要做的是设置每个面板的大小,最终结果是西边的 jpanel 是 jframe 宽度的 80%,其余的 20% 是东边的。是否可以?我应该使用其他布局吗?
多谢。
I have a JFrame with two JPanels inside. One is set on west, other on east with BorderLayout. The thing is, it just shows two 10 pixel width, 100% JFrame height strips:
alt text http://img641.imageshack.us/img641/3298/imagewg.png
What i want to do is to setsize each panel having as end result that the jpanel on the west be 80% of the jframe width, the remaining 20% to the one on the east. Is it possible? Should I use another layout?
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这取决于您的具体要求。
使用分割窗格时,您可以将其禁用,这将阻止调整大小。
您还可以使用以下方法隐藏分隔线:
但是,这不会为您提供精确的 80/20 分割。我创建了一个宽度为 400 的分割窗格。正确的组件从 312 开始,而应该是 320。所以这取决于你想要什么。
如果你想要真正的 80/20 分割,那么相对布局是专门为此编写的。
It depends on your exact requirement.
When using a split pane you can make it disabled which will prevent resizing.
You can also hide the divider using:
However, this doesn't give you an exact 80/20 split. I created a splitpane of width 400. The right component started at 312 when is should be 320. So it depends what you want.
If you want a real 80/20 split then the Relative Layout was specifically written for this.
我建议尝试 GridBagLayout,您可以设置每个组件的水平空间的“权重”或优先级。
您也可以使用 BoxLayout (以及X 轴),然后将预先确定尺寸的组件添加到容器中。布局更容易使用。
I would recommend trying GridBagLayout, you can set the "weight" or priority of horizontal space per component.
You could also just go with a BoxLayout (along the X axis) and just add the pre-sized components to your container. The layout is a bit easier to work with.
您是否尝试过
JSplitPane
,特别是使用setDividerLocation(doubleproportionalLocation)
方法?这允许您设置封闭组件的宽度或高度的百分比。Have you tried a
JSplitPane
, specifically using thesetDividerLocation(double proportionalLocation)
method? This allows you to set a percentage of the enclosing component's width or height.我刚刚使用了 MIG Layout 和 presto。
I just used MIG Layout and presto.
这里有三个选项:
1 - 保留边框布局并在两个 JPanel 上设置首选大小。这适用于固定大小的窗口。
2 - 使用GridBagLayout:
3 - 使用著名的SpringLayout。这个布局管理器确实很难掌握,但这里是一个入门者:
Three options here:
1 - Keep the border layout and set a prefered size on the two JPanels. This will work with fixed size window.
2 - Use the GridBagLayout:
3 - Use the famous SpringLayout. This layout manager is really hard to master but here is a starter:
我建议您使用 FreeDesign 布局,如下所示:
alt text http://sites.google.com/site/yan Chengcheok/Home/free-design-layout.PNG
Netbeans IDE 让所有这些工作变得有趣而轻松;)
I would suggest that you use FreeDesign layout, which will look like this :
alt text http://sites.google.com/site/yanchengcheok/Home/free-design-layout.PNG
Netbeans IDE makes all this job fun and easy ;)