可调整大小的 Swing 布局,其中按钮根据可变尺寸排列
我想使用 Java Swing 进行布局,如下图所示。
(来源:braun-abstatt.de)
左边是一个 JPanel,它是通过 PaintComponent() 绘制的,当窗口大小调整时,图形会自动缩放。 (问题不是关于那个面板的。那个已经完成了。)
现在我需要一些按钮(黑框,在 Photoshop 中添加用于绘图)到前面提到的 JPanel 的右侧。顶部和底部的红色区域的高度(旁边应该只有空白区域)是按照 CONSTANT_FACTOR * getHeight()
的方式计算的。左侧每个隔间旁边应该有一组按钮,排列在相应隔间的中心(参见蓝线)。
包含按钮的 JPanel 了解 CONSTANT_FACTOR 和隔间的数量,因此应该可以将此信息提供给布局管理器。
我最适合使用哪个布局管理器来实现此布局?我读过所有不同的布局管理器,但我不太清楚哪一个或哪一个组合最适合这种情况。
I would like to make a layout using Java Swing which looks like the following drawing.
(source: braun-abstatt.de)
On the left is a JPanel which is drawn through paintComponent()
in a way that the graphics automatically scale when the window is resized. (The question isn't about that panel. That one's already done.)
Now I need some buttons (the black boxes, added in Photoshop for the drawing) to the right of the JPanel mentioned before. The height of the reddish areas at the top and bottom, next to which there should be just empty space, is calculated along the lines of CONSTANT_FACTOR * getHeight()
. Next to each compartment on the left, there should be a group of buttons, lined up to the center of the respective compartment (see the blue lines).
The JPanel containing the buttons knows about the CONSTANT_FACTOR and the number of compartments, so it should be possible to feed this information into a layout manager.
Which layout manager would I best use to achieve this layout? I've read about all the different layout managers, but I can't quite figure out which one or which combination of them best fits in this case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
例如,通过使用不同的 LayoutManager,一个非常简单的和简单的容器,花费不超过15-20分钟:
For example, by use of a different LayoutManager, a very easy and simple container, takes no more than 15-20 minutes:
您应该尝试查看 MigLayout。这是一个超级灵活的布局管理器,也非常简单。
代码看起来像这样:
尝试将 debug,
flowy
添加到构造函数中,以直观地了解正在发生的情况。You should try looking at MigLayout. It's a super flexible LayoutManager that is also very simple.
The code would look something like:
Try adding debug,
flowy
to the constructor to get a visual idea of what is going on.GBC 没有锚点,只有普通的 GridBagConstraints 和首选大小。
具有固定大小的居中 JButton:
GBC without an anchor, just with plain vanilla GridBagConstraints and preferred size.
Centered JButton with fixed size: