我只想要一个具有合并单元格更改的 GridLayout。所以我找到了 GridBagLayout。看来这个布局很灵活。我不需要这种灵 active 。有没有办法告诉 GridBagLayout 它应该在整个宽度和高度上使用例如 20 列和 10 行?它应该看起来像 GridLayout,但具有合并单元格。
谢谢
最佳答案
据我所知,您可以为多个 JComponent
使用相同的高度和宽度。但是您必须在需要合并单元格的地方更改它们。
这是来自 how to use GridBagLayout 的示例
protected void makebutton(String name,
GridBagLayout gridbag,
GridBagConstraints c) {
Button button = new Button(name);
gridbag.setConstraints(button, c);
add(button);
}
public void init() {
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
setFont(new Font("SansSerif", Font.PLAIN, 14));
setLayout(gridbag);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
makebutton("Button1", gridbag, c);
makebutton("Button2", gridbag, c);
makebutton("Button3", gridbag, c);
c.gridwidth = GridBagConstraints.REMAINDER; //end row
makebutton("Button4", gridbag, c);
c.weightx = 0.0; //reset to the default
makebutton("Button5", gridbag, c); //another row
.........
}
所以你不需要一直指定高度和宽度。
关于java - 具有固定列数的 GridBagLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11482890/