Java GridBagLayout 不工作

标签 java swing gridbaglayout

我正在尝试使用 GridBagLayout,但我没有得到我所期望的,而且我找不到这段代码中的错误:

public class GridBagEx1 extends JPanel {

    private static final long serialVersionUID = 1L;

    protected void makebutton(String name, GridBagLayout gridbag, GridBagConstraints c) {
        JButton button = new JButton(name);
        gridbag.setConstraints(button, c);
        add(button);
    }

    public void init() {
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        setLayout(gridbag);

        c.fill = BOTH;

        c.weightx = 1.0;
        c.weighty = 1.0;

        c.anchor = CENTER;

        c.insets.top = 5;
        c.insets.bottom = 5;
        c.insets.left = 5;
        c.insets.right = 5;

        c.gridx = 0;
        c.gridy = 0;
        c.gridheight = 1;
        c.gridwidth = 2;
        makebutton("Button1", gridbag, c);      

        c.gridx = 2;
        c.gridy = 0;
        c.gridheight = 1;
        c.gridwidth = 1;
        makebutton("Button2", gridbag, c);

        c.gridx = 3;
        c.gridy = 0;
        c.gridheight = 2;
        c.gridwidth = 2;
        makebutton("Button3", gridbag, c);

        c.gridx = 0;
        c.gridy = 1;
        c.gridheight = 1;
        c.gridwidth = 1;
        makebutton("Button4", gridbag, c);

        c.gridx = 1;
        c.gridy = 1;
        c.gridheight = 1;
        c.gridwidth = 2;
        makebutton("Button5", gridbag, c);

        c.gridx = 0;
        c.gridy = 2;
        c.gridheight = 1;
        c.gridwidth = 1;
        makebutton("Button6", gridbag, c);

        c.gridx = 1;
        c.gridy = 2;
        c.gridheight = 1;
        c.gridwidth = 2;
        makebutton("Button7", gridbag, c);

        c.gridx = 3;
        c.gridy = 2;
        c.gridheight = 1;
        c.gridwidth = 1;
        makebutton("Button8", gridbag, c);

        c.gridx = 4;
        c.gridy = 2;
        c.gridheight = 1;
        c.gridwidth = 1;
        makebutton("Button9", gridbag, c);
    }

    public static void main(String args[]) {
        JFrame frame = new JFrame();
        GridBagEx1 ex1 = new GridBagEx1();

        ex1.init();

        frame.add(ex1);
        frame.pack();
        frame.setVisible(true);
    }
}

这张图说明了我需要什么:

Programmers love paint

黄色是按钮名称,红色是行和列。

这是真正发生的事情: Whats wrong here?

谁能解释我的代码有什么问题?

最佳答案

问题是没有什么可以说服第二个网格列 (gridx=1) 具有任何宽度,因为第二列中没有需要适合的组件。因此,第二列的宽度为 0,因此尽管 Button1 确实跨越了前两列,但它看起来并不是那样,因为第一列满足了它的所有宽度需求;尽管 Button5 和 Button7 横跨第二和第三列,但它们的所有宽度需求都由第三列满足。

要修复它,您必须说服应该更宽的按钮(1、5、7)占用更多空间。在这里,我通过设置 c.ipadx = 35; 为这些按钮添加了填充。 (我还删除了 weightx = 1.0 约束。由于我不太明白的原因,它在保留时不起作用。):

screenshot

来源:

public void init() {
    GridBagLayout gridbag = new GridBagLayout();
    GridBagConstraints c = new GridBagConstraints();
    setLayout(gridbag);

    c.fill = c.BOTH;

    //c.weightx = 1.0;
    //c.weighty = 1.0;

    c.anchor = c.CENTER;

    c.insets.top = 5;
    c.insets.bottom = 5;
    c.insets.left = 5;
    c.insets.right = 5;

    c.gridx = 0;
    c.gridy = 0;
    c.gridheight = 1;
    c.gridwidth = 2;
    c.ipadx = 35;
    makebutton("Button1", gridbag, c);

    c.gridx = 2;
    c.gridy = 0;
    c.gridheight = 1;
    c.gridwidth = 1;
    c.ipadx = 0;
    makebutton("Button2", gridbag, c);

    c.gridx = 3;
    c.gridy = 0;
    c.gridheight = 2;
    c.gridwidth = 2;
    c.ipadx = 0;
    makebutton("Button3", gridbag, c);

    c.gridx = 0;
    c.gridy = 1;
    c.gridheight = 1;
    c.gridwidth = 1;
    c.ipadx = 0;
    makebutton("Button4", gridbag, c);

    c.gridx = 1;
    c.gridy = 1;
    c.gridheight = 1;
    c.gridwidth = 2;
    c.ipadx = 35;
    makebutton("Button5", gridbag, c);

    c.gridx = 0;
    c.gridy = 2;
    c.gridheight = 1;
    c.gridwidth = 1;
    c.ipadx = 0;
    makebutton("Button6", gridbag, c);

    c.gridx = 1;
    c.gridy = 2;
    c.gridheight = 1;
    c.gridwidth = 2;
    c.ipadx = 35;
    makebutton("Button7", gridbag, c);

    c.gridx = 3;
    c.gridy = 2;
    c.gridheight = 1;
    c.gridwidth = 1;
    c.ipadx = 0;
    makebutton("Button8", gridbag, c);

    c.gridx = 4;
    c.gridy = 2;
    c.gridheight = 1;
    c.gridwidth = 1;
    c.ipadx = 0;
    makebutton("Button9", gridbag, c);
}

编辑: 正如评论中指出的那样,上述方法不合适,因为它阻止了动态调整布局大小。要使布局扩展以填充其容器的大小,需要 weightxweighty 约束,但第二列没有任何宽度。

这是对替代解决方案的尝试。这是一个肮脏的 hack,它在第二列的底部插入一个不可见的组件以强制该列具有宽度:

    c.gridx = 1;
    c.gridy = 3;
    c.gridheight = 1;
    c.gridwidth = 1;
    c.insets.set(0, 0, 0, 0);
    c.weighty = 0;
    add(Box.createRigidArea(new Dimension(50, 0)), c);

这在调整窗口大小时处理得相当好,因为虽然组件被赋予了固定的初始大小,但 GridBagLayout 会与其他组件成比例地放大它。不过,它仍然不完美。也许有更好的解决方案,但我找不到。

关于Java GridBagLayout 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26088675/

相关文章:

java - 使用 GridBagLayout 定位

java - 布局和子类

java - 从一个表中选择仅通过两个一对多表连接到另一个表中的一个值的值

java - Vector 上的增强 for 循环中的空指针异常

java - 最初隐藏的单选按钮在事件中不可见

Java - 顶部的 GridBagLayout 组件在调整大小时消失

java - 在 Java 6 中使用 Java 7 SDK 功能

java - 当我一次使用 setcontentpane 时如何添加按钮?

java - 使用图像而不是 JDialog/frame 来容纳 swing 组件?

java - jpanel 无法很好地显示 jframe 设置为 gridbaglayout