java GridBagLayout:均匀分布的单元格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3695402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
GridBagLayout: equally distributed cells
提问by poke
Is it possible to completely emulate the behavior of a GridLayout with the GridBagLayout manager?
是否可以使用 GridBagLayout 管理器完全模拟 GridLayout 的行为?
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 sameas 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?
基本上,我有一个 8x8 的网格,其中每个单元格都应该具有相同的宽度和高度。GridLayout 自动执行此操作。但我想向网格中添加另一行和列,其大小与其他行和列不同。该行/列应占用可能剩余的所有剩余空间(因为可用大小不能平均分配到 8 个单元格中)。这是可能的,还是我 - 再次 - 必须使用不同的布局管理器?
edit
编辑
Here is a simple graphic of what I want to achieve, simplified to just 4 cells:
这是我想要实现的简单图形,简化为仅 4 个单元格:
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.
彩色单元格是我添加到实际网格(灰色)中的单元格,该网格具有相同的高度和宽度x
。所以网格的高度和宽度是4*x
. 我现在希望额外的单元格具有必要的宽度/高度(minimumSize)加上全尺寸的其余可用宽度/高度。
If the whole panel's size is changed, the gray grid cells should again take up as much as space as possible.
如果整个面板的大小发生变化,灰色网格单元应再次占用尽可能多的空间。
采纳答案by poke
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
编辑
didxgareminded 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!
didxga在评论中提醒我,我想发布我的解决方案。然而,在挖掘出当时的项目并查看它之后,我实际上无法发布我的解决方案,因为事实证明我从未创建过它!
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).
这是一个 uni 项目,于 2010 年 9 月中旬正式完成。我们实际上想在之后继续工作,这可能就是为什么我说我会发布它(因为这是我想要改进的一件事),但我们从未真正得到围绕这样做 - 可悲的是。相反,我只是省略了那些额外的列和行(顺便说一句,这意味着作为行/列的标签)。
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 ;)
所以,是的,我非常抱歉,我无法发布符合我最初想要的布局...... :( 也许如果有足够多的人要求这样的布局,我会创建它,但截至目前,我不太愿意再次深入 Java 布局 ;)
回答by Kintaro
set weightx
and weighty
of GridBagConstraints
of the fixed cells to 0
and the fill
to NONE
. For the floating cells set fill
to BOTH
, for the floating cells that should expand only horizontally set weightx
to 1
and for the vertically expanding ones set weighty
to 1
.
集weightx
和weighty
的GridBagConstraints
固定细胞的0
和fill
来NONE
。对于设置漂浮细胞fill
到BOTH
,对于应扩大只会水平设置漂浮细胞weightx
,以1
和为竖直扩展来设定的weighty
到1
。
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 call invalidate()
to recalculate the layout.
单元格只有在它们有任何内容时才会扩展,所以你需要用一些东西来填充它。我选择了JLabel
s 并为固定单元格中的标签设置了固定尺寸。在调整大小时,您需要重新计算尺寸并调用invalidate()
重新计算布局。
Here is an example for a w
x h
grid:
以下是w
xh
网格的示例:
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class GridBag {
public static void main(String[] args) {
final JFrame f = new JFrame("Gridbag Test");
final Container c = f.getContentPane();
c.setLayout(new GridBagLayout());
final Dimension dim = new Dimension(70, 70);
final int w = 4;
final int h = 4;
final JLabel[] yfloating = new JLabel[w];
final JLabel[] xfloating = new JLabel[h];
final JLabel[][] fixed = new JLabel[w][h];
// adding the vertically floating cells
final GridBagConstraints gc = new GridBagConstraints();
gc.fill = GridBagConstraints.BOTH;
gc.weightx = 0.0;
gc.weighty = 1.0;
for(int i = 0; i < w; ++i) {
yfloating[i] = new JLabel("floating " + i);
yfloating[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
yfloating[i].setHorizontalTextPosition(JLabel.CENTER);
yfloating[i].setVerticalTextPosition(JLabel.CENTER);
gc.gridy = 0;
gc.gridx = i+1;
c.add(yfloating[i], gc);
}
// adding the horizontally floating cells
gc.fill = GridBagConstraints.BOTH;
gc.weightx = 1.0;
gc.weighty = 0.0;
for(int i = 0; i < w; ++i) {
xfloating[i] = new JLabel("floating " + i);
xfloating[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
xfloating[i].setHorizontalTextPosition(JLabel.CENTER);
xfloating[i].setVerticalTextPosition(JLabel.CENTER);
gc.gridy = i+1;
gc.gridx = 0;
c.add(xfloating[i], gc);
}
// adding the fixed cells
gc.fill = GridBagConstraints.NONE;
gc.weightx = 0.0;
gc.weighty = 0.0;
for(int i = 0; i < w; ++i) {
for(int j = 0; j < h; ++j) {
fixed[i][j] = new JLabel("fixed " + i);
fixed[i][j].setBorder(BorderFactory.createLineBorder(Color.BLACK));
fixed[i][j].setMaximumSize(dim);
fixed[i][j].setMinimumSize(dim);
fixed[i][j].setPreferredSize(dim);
gc.gridx = i+1;
gc.gridy = j+1;
c.add(fixed[i][j], gc);
}
}
c.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
final Component comp = e.getComponent();
final int newSize = Math.min(comp.getHeight() / h, comp.getWidth() / w);
final Dimension newDim = new Dimension(newSize, newSize);
for(int i = 0; i < w; ++i) {
for(int j = 0; j < h; ++j) {
fixed[i][j].setMaximumSize(newDim);
fixed[i][j].setMinimumSize(newDim);
fixed[i][j].setPreferredSize(newDim);
}
}
comp.invalidate();
}
});
f.pack();
f.setVisible(true);
}
}
回答by BobBrez
If you're not tied to the GridBagLayout necessarily, you should look into the MigLayoutlibrary. You can do something along the lines of:
如果您不一定绑定到 GridBagLayout,则应该查看MigLayout库。您可以执行以下操作:
MigLayout layout = new MigLayout(
"", // Layout Constraints
"[10][10][10][10]", // Column constraints
"[10][10][10][10]"); // Row constraints
回答by Brad Mace
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.
是否有理由不在 JTable 中执行此操作?似乎正是它为之而生的那种东西。使用适当的单元格渲染器,您可以在单元格中放置任何您想要的内容。
http://www.java2s.com/Code/Java/Swing-Components/TableRowHeaderExample.htm
http://www.java2s.com/Code/Java/Swing-Components/TableRowHeaderExample.htm
回答by Starkey
All values are set in the GridBagConstraints
.
所有值都在GridBagConstraints
.
If you want your 8x8 grid fixed, you can set the ipadx
and ipady
values.
如果您想要固定 8x8 网格,您可以设置ipadx
和ipady
值。
Then set the weightx
and weighty
values of your new row/column to 1.0
and set fill
to FULL
.
然后将新行/列的weightx
和weighty
值1.0
设置fill
为并设置为FULL
。
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
and ipady
values in addition to the weightx
and weighty
values. Or make a panel for the 8x8 grid and use a GridLayout
in there. Use your GridBagLayout
on the panel and the additional row/column.
如果您还希望扩展 8x8 网格,那就更复杂了。除了ipadx
和ipady
值之外,您还可以调整weightx
和weighty
值。或者为 8x8 网格制作一个面板并GridLayout
在其中使用 a 。GridBagLayout
在面板上使用您的和附加的行/列。