java - 具有固定列数的 GridBagLayout

标签 java swing grid-layout gridbaglayout

我只想要一个具有合并单元格更改的 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/

相关文章:

css - 如何显示 Bourbon Neat 网格的列?

java - 测试 OpenNLP 分类器模型

java - 在控制台中手动写入的替代方法

java - 使用哈希表的更好方法? java

java - Swing:实现完全可扩展组件的好方法是什么?

java - 从面板中删除/清空 gridLayout

java - Selenium 挂起实例化 FirefoxDriver

java - 按列值动态过滤 jTable

java - 是什么阻止程序退出?

android gridlayout 未在 23 岁以下的 api 中显示加权项目