Java Swing GridBagLayout:将按钮置于第二行的中心

Java Swing GridBagLayout:将按钮置于第二行的中心,java,swing,gridbaglayout,Java,Swing,Gridbaglayout,我有一个显示标签、文本字段和按钮的代码: import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Form extends JFrame implements ActionListener { private JLabel label = new JLabel("What's y

我有一个显示标签、文本字段和按钮的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Form extends JFrame implements ActionListener  {
    private JLabel label = new JLabel("What's your name:");
    private JTextField field = new JTextField(15);
    private JButton button = new JButton("Send");

    public Form()  {
        setTitle("Name Form");
        setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 1);
        gbc.anchor = GridBagConstraints.LINE_END;
        gbc.gridx = 0;
        gbc.gridy = 0;
        add(label, gbc);

        gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 1;
        gbc.gridy = 0;
        add(field, gbc);

        gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);
        gbc.anchor = GridBagConstraints.LINE_END;
        gbc.gridx = 0;
        gbc.gridy = 1;
        add(button, gbc);
    }

    @Override
    public void actionPerformed(ActionEvent event) {


    }

    public static void main(String[] args) {
        Form myFrame = new Form();

        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.pack();
        myFrame.setVisible(true);
    }

}
它表明:

我需要按钮水平居中,如下所示:

如何使用
GridBagLayout
?我尝试了
锚定的不同值,但没有任何效果

编辑:

添加
gbc.gridwidth=2
显示:


按钮需要延伸到两列上,因此设置
网格宽度

    gbc.gridwidth = 2;

(顺便说一句,您不需要为每个组件创建一个新的
GridBagConstraints
。只需重复使用该组件,只需更改不同的属性即可。)

按钮需要在两列上伸展,因此设置
GridBagWidth

    gbc.gridwidth = 2;

(顺便说一句,您不需要为每个组件创建一个新的
GridBagConstraints
。只需重复使用该组件,只需更改不同的属性即可。)

@parser将成为
锚定
属性,该属性应为
CENTER
(默认值)而不是
LINE\u END
@parsecor,它将是
锚定
属性,该属性应为
中心
(默认值),而不是
LINE\u END