GridBagLayout:均匀分布的单元格
是否可以使用 GridBagLayout 管理器完全模拟 GridLayout 的行为?
基本上,我有一个 8x8 网格,其中每个单元格应具有相同的宽度和高度。 GridLayout 自动执行此操作。但我想在网格中添加另一行和一列,其大小与其他行和列不同。该行/列应占据所有可能剩余的空间(因为可用大小无法平均分配到 8 个单元格中)。这是否可能,或者我是否必须再次使用不同的布局管理器?
编辑
这是我想要实现的简单图形,简化为仅 4 个单元格:
彩色单元格是我添加到实际网格(灰色)中的单元格,该网格具有相同高度和宽度的单元格x
。所以网格的高度和宽度是4*x
。我现在希望附加单元格具有必要的宽度/高度(minimumSize)加上完整尺寸的其余可用宽度/高度。
如果整个面板的大小发生变化,灰色网格单元应再次占据尽可能多的空间。
Is it possible to completely emulate the behavior of a GridLayout with the GridBagLayout manager?
Basically, I have a 8x8 grid in which each cell should have the same width and height. The GridLayout automatically did this. But I want to add another row and column to the grid which size is not the same as the other ones. That row/column should take up all the remaining space that might be left over (because the available size couldn't be equally distributed into 8 cells). Is that even possible, or do I – again – have to use a different layout manager?
edit
Here is a simple graphic of what I want to achieve, simplified to just 4 cells:
The colored cells are the ones I added to the actual grid (gray) which has cells with the same height and width x
. So the grid's height and width is 4*x
. I now want the additional cells to have the necessary width/height (minimumSize) plus the rest of the available width/height from the full size.
If the whole panel's size is changed, the gray grid cells should again take up as much as space as possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将固定单元格的
GridBagConstraints
的weightx
和weighty
设置为0
和fill
至无
。对于浮动单元格,将fill
设置为BOTH
,对于仅水平扩展的浮动单元格,将weightx
设置为1
对于垂直扩展的,将weighty
设置为1
。单元格只有在有内容时才会扩展,因此您需要用一些东西填充它。我选择了 JLabel 并为固定单元格中的标签设置了固定尺寸。调整大小时,您需要重新计算尺寸并调用
invalidate()
来重新计算布局。以下是
w
xh
网格的示例:set
weightx
andweighty
ofGridBagConstraints
of the fixed cells to0
and thefill
toNONE
. For the floating cells setfill
toBOTH
, for the floating cells that should expand only horizontally setweightx
to1
and for the vertically expanding ones setweighty
to1
.The cells only expand if they have any content, so you need to fill it with something. I chose
JLabel
s and set fixed dimensions for the labels in the fixed cells. On resize you need to recalculate the dimensions and callinvalidate()
to recalculate the layout.Here is an example for a
w
xh
grid:如果您不一定依赖于 GridBagLayout,则应该查看 MigLayout 库。您可以按照以下方式做一些事情:
If you're not tied to the GridBagLayout necessarily, you should look into the MigLayout library. You can do something along the lines of:
所有值均在 GridBagConstraints 中设置。
如果您希望固定 8x8 网格,则可以设置
ipadx
和ipady
值。然后将新行/列的
weightx
和weighty
值设置为1.0
并将fill
设置为完整
。您的额外行/列将随着剩余的空间而扩展。
如果你想让8x8的网格也扩展,那就更复杂了。除了
weightx
和weighty
值之外,您还可以调整ipadx
和ipady
值。或者为 8x8 网格创建一个面板并在其中使用 GridLayout。在面板和附加行/列上使用GridBagLayout
。All values are set in the
GridBagConstraints
.If you want your 8x8 grid fixed, you can set the
ipadx
andipady
values.Then set the
weightx
andweighty
values of your new row/column to1.0
and setfill
toFULL
.Your extra row/column will expand with the space that is left over.
If you want the 8x8 grid to expand as well, that is more complicated. You can adjust the
ipadx
andipady
values in addition to theweightx
andweighty
values. Or make a panel for the 8x8 grid and use aGridLayout
in there. Use yourGridBagLayout
on the panel and the additional row/column.您不在 JTable 中执行此操作是否有原因?看起来就是它的用途。使用适当的单元格渲染器,您可以将任何您想要的内容放入单元格中。
http://www.java2s.com/Code/Java/Swing-Components /TableRowHeaderExample.htm
Is there a reason you're not doing this in a JTable? Seems like just the sort of thing it's made for. With the appropriate cell renderer you can put anything you want in the cells.
http://www.java2s.com/Code/Java/Swing-Components/TableRowHeaderExample.htm
在使用内置布局管理器尝试了许多不同的事情之后,我决定也为这个问题创建一个自定义布局管理器。我还没有这样做,因为我没有时间继续这个项目,但是当我完成它时,我将确保在此处发布布局管理器代码,以便任何对类似解决方案感兴趣的人可以使用它。
编辑
didxga 在评论中提醒我我想发布我的解决方案。然而,在挖掘出当时的项目并查看它之后,我实际上无法发布我的解决方案,因为事实证明我从未创建过它!
这是一个大学项目,于 2010 年 9 月中旬正式完成。我们实际上想在之后继续研究它,这可能就是为什么我说我会发布它(因为这是我想要改进的一件事),但我们从未真正得到过围绕这样做——可悲的是。相反,我只是省略了那些额外的列和行(顺便说一句,这意味着作为行/列的标签)。
所以,是的,我非常抱歉,我无法发布一个可以实现我最初想要的布局......:(也许如果有足够多的人请求这样的布局,我会创建它,但到目前为止,我并不是真的愿意再次深入研究 Java 布局;)
After trying many different things with the built-in layout managers, I decided to create a custom layout manager for this problem as well. I didn't do it yet, as I didn't have the time to continue with this project, but when I have it done, I'll make sure to post the layout manager code here, so that anyone interested in a similar solution can use it.
edit
didxga reminded me in the comments that I wanted to post my solution. However, after digging out the project from back then and looking at it, I actually cannot post my solution because it turns out that I never got to creating it!
It was a uni project that finished official mid September 2010. We actually wanted to continue working on it afterwards, which is probably why I said that I would post it (as that was one thing I wanted to improve), but we never really got around doing it – sadly. Instead I simply left out those extra column and row (which was meant as a label for the rows/columns btw).
So yeah, I’m terribly sorry that I cannot post a layout that does what I initially wanted… :( Maybe if there are enough requesting such a layout, I would create it, but as of now, I’m not really willing to dive into Java layouting again ;)