Java GridBagLayout:GridBagWidth>1时的对齐和宽度

Java GridBagLayout:GridBagWidth>1时的对齐和宽度,java,swing,layout-manager,gridbaglayout,Java,Swing,Layout Manager,Gridbaglayout,在具有4列的JFrame中,棕色线应为第1列和第2列之间的限制,确定和取消按钮应位于该限制的每一侧: 问题是: 确定+取消设置未与其他按钮居中。 左侧和右侧JTextArea的宽度不相同。 当我期望第1列和第2列相等时,第1列的宽度似乎为零 使用的代码: import java.awt.Component; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; imp

在具有4列的JFrame中,棕色线应为第1列和第2列之间的限制,确定和取消按钮应位于该限制的每一侧:

问题是:

确定+取消设置未与其他按钮居中。 左侧和右侧JTextArea的宽度不相同。 当我期望第1列和第2列相等时,第1列的宽度似乎为零

使用的代码:

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;


public class GblSO extends JFrame {

    // Instance variables
    GridBagConstraints gbc = new GridBagConstraints();

    public GblSO() {
        // Set frame
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(new GridBagLayout());

        // Text areas
        JTextArea left = new JTextArea("Left!");
        JTextArea right = new JTextArea("Right!");
        setConstraints(1, 1, GridBagConstraints.BOTH, null);
        addToFrame(left, 0, 1, 1, 5, GridBagConstraints.CENTER);
        addToFrame(right, 3, 1, 1, 5, GridBagConstraints.CENTER);

        // Transfer buttons
        JButton addBtn = new JButton(">");
        JButton rmvBtn = new JButton("<");
        setConstraints(0, 0, GridBagConstraints.NONE, new Insets(3, 5, 3, 5));
        addToFrame(addBtn, 1, 1, 2, 1, GridBagConstraints.CENTER);
        addToFrame(rmvBtn, 1, 3, 2, 1, GridBagConstraints.CENTER);

        // OK / Cancel buttons
        JButton okBtn = new JButton("OK");
        JButton canBtn = new JButton("Cancel");
        setConstraints(0, 0, GridBagConstraints.NONE, new Insets(15, 4, 15, 4));
        addToFrame(okBtn, 0, 6, 2, 1, GridBagConstraints.EAST);
        addToFrame(canBtn, 2, 6, 2, 1, GridBagConstraints.WEST);

        // Show
        pack();
        setVisible(true);
    }

    private void setConstraints(double weightx, double weighty, int fill, Insets insets) {
        gbc.weightx = weightx; // how much cell resizes
        gbc.weighty = weighty; // "
        gbc.fill = fill; // how component fills cell
        gbc.insets = (insets == null ? new Insets(0, 0, 0, 0) : insets);
    }

    private void addToFrame(Component comp,
            int gridx, int gridy, int gridwidth, int gridheight, int anchor) {
        gbc.gridx = gridx;
        gbc.gridy = gridy;
        gbc.gridwidth = gridwidth;
        gbc.gridheight = gridheight;
        gbc.anchor = anchor;
        add(comp, gbc);
    }

    public static void main(String[] args) {
        new GblSO();

    }

}
结果:


这两个JTextArea仍然有不同的宽度:-,显然是>和,这是因为您没有在每个列上添加任何组件,这是GridBagLayout按预期工作所必需的

现在我要说几句话:

您是否注意到在链接到的教程中,“确定”和“取消”按钮与JTextArea对齐,而不是与整个上部组件对齐

使用GridBagLayout时,最好为要添加的每个组件实例化一个新的GridBagConstraint。它可以防止您忘记在某个地方重置属性,这很难排除故障

然后,如果希望“确定”和“取消”按钮与上部组件居中对齐,最简单的方法是创建两个JPanel:一个与上部组件对齐,另一个与按钮对齐。您仍然可以对上面板使用GridBagLayout,对按钮使用默认的FlowLayout。你可以把它们放在一个中间和南部的边界布局位置,这样你就可以走了。请注意,面板将居中对齐,而不是确定/取消按钮和按钮之间的空间

回到问题的解决方案:您必须添加空组件容器、JPanel、emtpy JLabel。。。在第一行或最后一行上,如果要使用gridheight而不是gridwidth,则将weightx设置为1.0以使单元格实际在X方向上填充,将weighty设置为0.0以使其在Y方向上不可见,反之亦然

这样,单元格将存在,您将得到预期的结果

addToFrame(addBtn, 1, 1, 1, 1, GridBagConstraints.CENTER);
addToFrame(rmvBtn, 2, 3, 1, 1, GridBagConstraints.CENTER);
...
setLayout(new GridBagLayout());

setConstraints(1, 0, GridBagConstraints.BOTH, null);
addToFrame(new Container(), 0, 0, 1, 1, GridBagConstraints.CENTER);
addToFrame(new Container(), 1, 0, 1, 1, GridBagConstraints.CENTER);
addToFrame(new Container(), 2, 0, 1, 1, GridBagConstraints.CENTER);
addToFrame(new Container(), 3, 0, 1, 1, GridBagConstraints.CENTER);

// Text areas
...