So for a course project, I'm building a word spelling game. The basic UI should look like this:
But after running the program it primarily takes this form:
Only after I resize or minimize and restore it does it change to the expected form.
Here's the source code:
package game;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class GameBlock {
private static int i, j;
private static JTextField textField;
public static void main(String[] args) {
GameBlock gb = new GameBlock();
}
public GameBlock(){
JFrame frame = new JFrame("Word-A-Lot");
frame.setBounds(230, 70, 900, 451);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
//frame.setResizable(false);
frame.setVisible(true);
JPanel panel = new JPanel();
panel.setBounds(40, 71, 350, 262);
GridBagLayout gbl_panel = new GridBagLayout();
gbl_panel.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0};
gbl_panel.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
panel.setLayout(gbl_panel);
frame.getContentPane().add(panel);
JPanel panel_1 = new JPanel();
panel_1.setBounds(450, 71, 339, 262);
panel_1.setLayout(null);
frame.getContentPane().add(panel_1);
JLabel lblPlayer = new JLabel("Player 1");
lblPlayer.setFont(new Font("Tw Cen MT", Font.PLAIN, 14));
lblPlayer.setBounds(55, 26, 77, 14);
panel_1.add(lblPlayer);
JLabel label = new JLabel("00");
label.setBounds(72, 51, 21, 14);
panel_1.add(label);
JLabel label_1 = new JLabel("00");
label_1.setBounds(247, 51, 21, 14);
panel_1.add(label_1);
JButton btnPlay = new JButton("Insert Letter");
btnPlay.setBounds(119, 165, 106, 23);
panel_1.add(btnPlay);
JLabel lblPlayer_1 = new JLabel("Player 2");
lblPlayer_1.setFont(new Font("Tw Cen MT", Font.PLAIN, 14));
lblPlayer_1.setBounds(228, 26, 77, 14);
panel_1.add(lblPlayer_1);
JLabel lblPlayersTurn = new JLabel("Player 1's turn");
lblPlayersTurn.setFont(new Font("Tw Cen MT Condensed Extra Bold", Font.PLAIN, 17));
lblPlayersTurn.setBounds(125, 95, 147, 29);
panel_1.add(lblPlayersTurn);
textField = new JTextField();
textField.setBounds(129, 134, 86, 20);
textField.setColumns(10);
panel_1.add(textField);
JButton btnQuit = new JButton("New Game");
btnQuit.setBounds(160, 358, 98, 23);
frame.add(btnQuit);
JButton[][] buttons = new JButton[8][8];
ButtonGroup bg = new ButtonGroup();
GridBagConstraints gbc_button = new GridBagConstraints();
for(i=0; i<8; i++){
for(j=0; j<8; j++){
buttons[i][j] = new JButton(" ");
gbc_button.insets = new Insets(0, 0, 5, 5);
gbc_button.gridx = i;
gbc_button.gridy = j;
panel.add(buttons[i][j], gbc_button);
bg.add(buttons[i][j]);
buttons[i][j].setActionCommand(i + " " + j);
buttons[i][j].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent b) {
JOptionPane.showMessageDialog(null, b.getActionCommand());
}
});
}
}
}
}
The result is the same regardless of where I call the class from or adding the buttons separately instead of in a loop.