为此苦苦挣扎了一段时间。我的方法如下所示:
public Frame(){
JFrame window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(800, 600);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridheight =3;
gbc.gridwidth = 3;
JButton upButt = new JButton();//Buttons.upButton();
gbc.gridx = 1;
gbc.gridy = 0;
panel.add(upButt, gbc);
JButton downButt = new JButton();
gbc.gridx = 1;
gbc.gridy = 2;
panel.add(downButt, gbc);
JButton leftButt = new JButton();//Buttons.leftButton();
gbc.gridx=0;
gbc.gridy = 1;
panel.add(leftButt, gbc);
JButton rightButt = new JButton();//Buttons.rightButton();
gbc.gridx =2;
gbc.gridy = 1;
panel.add(rightButt, gbc);
window.add(panel);
window.setVisible(true);
}
据我了解 - 在阅读和重读 Java 文档之后。- 这应该给我 4 个十字形的按钮。然而,情况并非如此,按钮在窗口中心彼此堆叠。我错过了什么?