二维按钮网格的最佳 Swing 布局?
我正在尝试创建一个 JDialog,就像 Microsoft Word 中的“符号”对话框一样,您可以通过从“插入”菜单中选择“符号...”来获得该对话框。基本上,它是一个 n x m(n 和 m 直到运行时才知道)小按钮网格。我已经有了一个使用 GridLayout 很好地工作的第一个版本。问题是,当您调整对话框大小时(并且要求您应该能够调整它的大小),按钮的大小会发生变化。我需要按钮的大小保持不变。
但我希望包含按钮的网格的尺寸发生变化。例如,如果对话框变宽,但高度保持不变,则行数应减少,而列数应增加。
我想了几种方法来解决这个问题:
- 当调整对话框大小时,创建一个新的 GridLayout 并用按钮重新填充它。我打算尝试一下,看看它看起来如何,但这似乎是一种笨拙的做法。
- 使用其他类型的布局,例如 FlowLayout。我尝试了一下,但它把所有 n x m 按钮放在一排。我不想使用水平滚动条,并且按钮超出了右边缘。不管怎样,它应该是一个二维的按钮网格。
解决这个布局问题的最佳方法是什么?
I'm trying to create a JDialog like the Symbol dialog in Microsoft Word that you get by choosing Symbol... from the Insert menu. Basically, it's an n x m (n and m are not known until runtime) grid of small buttons. I've got a first version of this working nicely using a GridLayout. The problem is that when you resize the dialog (and there is a requirement that you should be able to resize it), the size of the buttons changes. I need the size of the buttons to remain constant.
But I want the dimensions of the grid containing the buttons to change. For example, if the dialog gets wider, but stays the same height, the number of rows should lessen, while the number of columns increases.
I've thought of a couple of ways to fix this:
- When the dialog is resized, create a new GridLayout and repopulate it with the buttons. I'm going to try this and see how it looks, but it seems like a clumsy way of doing it.
- Use some other type of layout such as a FlowLayout. I took a stab at this, but it put all n x m buttons in one row. I do not want to use horizontal scroll-bars and the buttons ran off the right edge. Anyway, it's supposed to be a 2-dimensional grid of buttons.
What is the best way to solve this layout problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 GridLayout 创建一个按钮面板并为其设置固定大小(当然可以在运行时计算)。按钮面板应包含在 BoxLayout 的面板中。
查看 BoxLayout 教程
非常非常 基本示例:
Create a buttons panel with GridLayout and set a fixed size (could be calculated at runtime of course) to it. The buttons panel should be contained in a panel of BoxLayout.
Check out the BoxLayout Tutorial
Very Very basic example:
换行布局可能就是您正在寻找的。
Wrap Layout might be what you are looking for.
我对单列按钮也有类似的问题,并发现
MiGLayout
(第三方,可在 此处)对此来说简单而有效。它有助于制作网格和设置按钮大小,尽管我花了一两天的时间来习惯它的语法。但关键是真正设置按钮大小;
GridLayout
看起来确实是网格布局的最佳选择。我还没有测试过,但我怀疑内置的 setXSize() 方法也能正常工作。GridBagLayout
教程有以下示例您可以通过调整大小/定位来完成一些操作。I had a similar issue with a single column of buttons, and found that
MiGLayout
(third-party, available here) was simple and effective for this. It helped both with making a grid and with setting button sizes, although it took me a day or two to get used to its syntax.But the key is really setting button sizes;
GridLayout
certainly seems like the way to go for a layout that is, well, a grid. I haven't tested, but I suspect that the built-insetXSize()
methods would work just as well. TheGridBagLayout
tutorial has examples of some things you can do with sizing/positioning.FlowLayout 是可行的方法,但您可能会遇到一些配置问题。父组件使用什么布局管理器?
FlowLayout would be the way to go but you might have some configuration problems. What layout manager does the parent component use?