我正在尝试制作单板游戏。对于3x3 tictactoe表,我使用9个按钮。但是,似乎按钮上方和下方的jtext会更改每个3x3按钮的长度。有没有一种方法可以将3x3按钮的大小设置为相等,而不会受到其他组件的干扰。
import java.awt.Button;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.Dimension;
public class makeGUI {
JFrame frame;
public void initialise() {
frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel playPanel = new JPanel();
playPanel.setPreferredSize(new Dimension(300, 500));
playPanel.setBackground(Color.WHITE);
playPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JTextField field = new JTextField(5);
field.setEditable(false);
c.gridwidth = 2;
c.gridx = 0;
c.gridy = 0;
playPanel.add(field, c);
JButton button = new JButton("");
//c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
button.setPreferredSize(new Dimension(40, 40));
playPanel.add(button, c);
button = new JButton("");
c.fill = GridBagConstraints.HORIZONTAL;
//c.weightx = 0.5;
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 1;
button.setPreferredSize(new Dimension(40, 40));
playPanel.add(button, c);
button = new JButton("");
c.fill = GridBagConstraints.HORIZONTAL;
//c.weightx = 0.5;
c.gridx = 2;
c.gridy = 1;
c.gridwidth = 1;
button.setPreferredSize(new Dimension(40, 40));
playPanel.add(button, c);
button = new JButton("");
c.fill = GridBagConstraints.HORIZONTAL;
//c.weightx = 0.5;
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
button.setPreferredSize(new Dimension(40, 40));
playPanel.add(button, c);
button = new JButton("");
c.fill = GridBagConstraints.HORIZONTAL;
//c.weightx = 0.5;
c.gridx = 1;
c.gridy = 2;
c.gridwidth = 1;
button.setPreferredSize(new Dimension(40, 40));
playPanel.add(button, c);
button = new JButton("");
c.fill = GridBagConstraints.HORIZONTAL;
//c.weightx = 0.5;
c.gridx = 2;
c.gridy = 2;
c.gridwidth = 1;
button.setPreferredSize(new Dimension(40, 40));
playPanel.add(button, c);
button = new JButton("");
c.fill = GridBagConstraints.HORIZONTAL;
//c.weightx = 0.5;
c.gridx = 0;
c.gridy = 3;
c.gridwidth = 1;
button.setPreferredSize(new Dimension(40, 40));
playPanel.add(button, c);
button = new JButton("");
c.fill = GridBagConstraints.HORIZONTAL;
//c.weightx = 0.5;
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 1;
button.setPreferredSize(new Dimension(40, 40));
playPanel.add(button, c);
button = new JButton("");
c.fill = GridBagConstraints.HORIZONTAL;
//c.weightx = 0.5;
c.gridx = 2;
c.gridy = 3;
c.gridwidth = 1;
button.setPreferredSize(new Dimension(40, 40));
playPanel.add(button, c);
field = new JTextField(5);
c.gridwidth = 1;
c.gridx = 0;
c.gridy = 4;
playPanel.add(field, c);
button = new JButton("Submit");
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.5;
c.gridx = 2;
c.gridy = 4;
playPanel.add(button, c);
frame.add(playPanel);
frame.setTitle("A Simple Card Game");
frame.setSize(400, 700);
frame.pack();
frame.setVisible(true);
}
}
这就是我现在拥有的:
我想要类似的东西