Java Gridbag missalign-第二行中的最后一个单元格显示在第一行中

Java Gridbag missalign-第二行中的最后一个单元格显示在第一行中,java,layout,gridbaglayout,Java,Layout,Gridbaglayout,我正在为表单使用GB布局。目前有两行,每行由6个元素组成(第一行包含标签,第二行包含文本字段)。我的问题是,应该在x5y1中显示的元素在x6y0中显示。你能指出我犯的错误吗 public GUI() { super("Bla"); GridBagLayout layout = new GridBagLayout(); setLayout(layout); GridBagConstraints gbc = new GridBagConstraints();

我正在为表单使用GB布局。目前有两行,每行由6个元素组成(第一行包含标签,第二行包含文本字段)。我的问题是,应该在x5y1中显示的元素在x6y0中显示。你能指出我犯的错误吗

public GUI() {
    super("Bla");
    GridBagLayout layout = new GridBagLayout();
    setLayout(layout);
    GridBagConstraints gbc = new GridBagConstraints();      
    [enter image description here][1]gbc.insets = new Insets(10,10,10,10);
    Container con = getContentPane();

    panel1 = new JPanel();

    labBrawn = new JLabel("Brawn");
    gbc.gridx = 0;
    gbc.gridy = 0;
    con.add(labBrawn, gbc); 

    labAgility = new JLabel("Agility");
    gbc.gridx = 1;
    gbc.gridy = 0;
    con.add(labAgility, gbc);

    labIntellect = new JLabel("Intellect");
    gbc.gridx = 2;
    gbc.gridy = 0;
    con.add(labIntellect, gbc);

    labCunning = new JLabel("Cunning");
    gbc.gridx = 3;
    gbc.gridy = 0;
    con.add(labCunning, gbc);

    labWillpower = new JLabel("Willpower");
    gbc.gridx = 4;
    gbc.gridy = 0;
    con.add(labWillpower, gbc);

    labPresence = new JLabel("Presence");
    gbc.gridx = 5;
    gbc.gridy = 0;
    con.add(labPresence, gbc);



    txtBrawn = new JTextField("", 2);
    gbc.gridx = 0;
    gbc.gridy = 1;
    con.add(txtBrawn, gbc);

    txtAgility = new JTextField("", 2);
    gbc.gridx = 1;
    gbc.gridy = 1;
    con.add(txtAgility, gbc);

    txtIntellect = new JTextField("", 2);
    gbc.gridx = 2;
    gbc.gridy = 1;
    con.add(txtIntellect, gbc);

    txtCunning = new JTextField("", 2);
    gbc.gridx = 3;
    gbc.gridy = 1;
    con.add(txtCunning, gbc);

    txtWillpower = new JTextField("", 2);
    gbc.gridx = 4;
    gbc.gridy = 1;
    con.add(txtWillpower, gbc);

    txtPresence = new JTextField("", 2);
    gbc.gridx = 5;
    gbc.gridy = 1;
    con.add(txtPresence);


}

使用
GridBagLayout
您需要调用

添加(零部件、对象约束)

添加您忘记的最后一个组件
GridBagConstraints
object
con.add(txtPresence)。若并没有它,组件将放在第一行的最后一个组件之后,因为这是它将看到的第一个不受约束的可用空间(它只会使Y=0,并转到下一个可用的X)。如果您要在
JPanel
中添加组件,且该组件带有
GridBagLayout
而不带有
GridBagConstraints
对象,则它们将放在一行中

请记住使用
GridBagConstraints
对象设置
weights
fill
anchor

你也可以在github上看到我的回购协议。它包含一个演示如何工作。不过,您仍然应该了解
GridBagLayout
的工作原理

SSCCE:(快速修复代码)


哦,我的。。。非常感谢,很抱歉打扰你问这个问题。@Faire是的,没问题。这种事总是发生在我身上。这就是为什么我创建了GridBagManager,并且我的抽象面板有自己的
add()
方法,它调用
add(comp,gbc)
GridBagConstraints gbc
作为此类的一个字段。就这么简单。查一查,快乐编码!
public class GUI extends JFrame {

    public static void main(String[] args) {
        GUI gui = new GUI();
        gui.setSize(600, 600);
        gui.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        SwingUtilities.invokeLater(() -> {
            gui.setVisible(true);
        });
    }

    public GUI() {
        super();

        //those should be initialized outside
        JLabel labBrawn = new JLabel("Brawn");
        JLabel labAgility = new JLabel("Agility");
        JLabel labIntellect = new JLabel("Intellect");
        JLabel labCunning = new JLabel("Cunning");
        JLabel labWillpower = new JLabel("Willpower");
        JLabel labPresence = new JLabel("Presence");

        JTextField txtBrawn = new JTextField("", 2);
        JTextField txtAgility = new JTextField("", 2);
        JTextField txtIntellect = new JTextField("", 2);
        JTextField txtCunning = new JTextField("", 2);
        JTextField txtWillpower = new JTextField("", 2);
        JTextField txtPresence = new JTextField("", 2);
        //--------------------------------------------

        //here goes actual GUI
        GridBagLayout gridBagLayout = new GridBagLayout();
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.anchor = GridBagConstraints.NORTH;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.gridwidth = 1;
        gbc.gridheight = 1;

        JPanel dataInputPanel = new JPanel();
        dataInputPanel.setLayout(gridBagLayout);

        gbc.weightx = 1;
        gbc.weighty = 0;

        gbc.gridx = 0;
        gbc.gridy = 0;
        dataInputPanel.add(labBrawn, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        dataInputPanel.add(labAgility, gbc);

        gbc.gridx = 2;
        gbc.gridy = 0;
        dataInputPanel.add(labIntellect, gbc);

        gbc.gridx = 3;
        gbc.gridy = 0;
        dataInputPanel.add(labCunning, gbc);

        gbc.gridx = 4;
        gbc.gridy = 0;
        dataInputPanel.add(labWillpower, gbc);

        gbc.gridx = 5;
        gbc.gridy = 0;
        dataInputPanel.add(labPresence, gbc);

        gbc.gridx = 0;
        gbc.gridy = 1;
        dataInputPanel.add(txtBrawn, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        dataInputPanel.add(txtAgility, gbc);

        gbc.gridx = 2;
        gbc.gridy = 1;
        dataInputPanel.add(txtIntellect, gbc);

        gbc.gridx = 3;
        gbc.gridy = 1;
        dataInputPanel.add(txtCunning, gbc);

        gbc.gridx = 4;
        gbc.gridy = 1;
        dataInputPanel.add(txtWillpower, gbc);

        gbc.gridx = 5;
        gbc.gridy = 1;
        gbc.weighty = 1;
        dataInputPanel.add(txtPresence, gbc);

        dataInputPanel.setBackground(Color.red);
        this.add(dataInputPanel, BorderLayout.NORTH);
    }
}