Swing GridBagLayout:将第二行的按钮居中

Swing GridBagLayout: centering a button on the second row

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

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 执行此操作?我为 anchor 尝试了不同的值,但没有任何效果。

编辑:

添加 gbc.gridwidth = 2 显示:

按钮需要跨越两列,所以设置gridwidth

    gbc.gridwidth = 2;

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