So for my university project, we are supposed to create a TicTacToe Game with GUI. I'm quite new and I've started my coding for it, but I can't seem to figure out what's wrong with my code. Everytime I run it, my 3x3 grid never displays 9 buttons. It displays anything from 1-8 but never 9 buttons! Also, sometime it doesn't display anything either Please someone help
Driver class:
public class GameDriver { //The main method of prompting the game public static void main(String[] args) { //New Noughts and Crosses game new BoardGUI(); } }
GUI:
public class BoardGUI extends JFrame { // instance variables - replace the example below with your own private JFrame window; private JPanel controlPanel; private JPanel gamePanel; private JPanel scorePanel; private JPanel buttonPanel; private JButton[][] buttons; private JButton newGame; private JButton quit; private JLabel compPlayer; private JLabel humanPlayer; private JLabel humanScore; private JLabel compScore; private Container contentPane; /** * Constructor for objects of class BoardGUI */ public BoardGUI() { // Setting the window JFrame window = new JFrame("Noughts And Crosses"); window.setSize(500,350); window.setResizable(false); window.setDefaultCloseOperation(EXIT_ON_CLOSE); window.setVisible(true); // Setting the content pane contentPane = window.getContentPane(); contentPane.setLayout(new BorderLayout(10,0)); // Setup the game board gamePanel = new JPanel(); gamePanel.setLayout(new GridLayout(3,3)); gamePanel.setBorder(BorderFactory.createLoweredBevelBorder()); contentPane.add(gamePanel,BorderLayout.CENTER); // Setup control panel layout controlPanel = new JPanel(); controlPanel.setLayout(new BorderLayout()); contentPane.add(controlPanel,BorderLayout.EAST); // Score panel layout setup scorePanel = new JPanel(); scorePanel.setLayout(new GridLayout(2,2)); scorePanel.setBorder(BorderFactory.createTitledBorder("Scores:")); controlPanel.add(scorePanel,BorderLayout.SOUTH); // Setting up the button panel buttonPanel = new JPanel(); buttonPanel.setLayout(new GridLayout(2,2)); controlPanel.add(buttonPanel,BorderLayout.NORTH); // Adding the labels for the score board humanPlayer = new JLabel("Player:"); humanScore = new JLabel(" 0"); compPlayer = new JLabel("Comp:"); compScore = new JLabel(" 0"); scorePanel.add(humanPlayer); scorePanel.add(humanScore); scorePanel.add(compPlayer); scorePanel.add(compScore); // Adding game button options to the button panel newGame = new JButton("New Game"); quit = new JButton("Quit Game"); buttonPanel.add(newGame); buttonPanel.add(quit); // Adding the buttons to the game panel buttons = new JButton[3][3]; for (int row =0; row<3; row++){ for (int col =0; col<3; col++){ buttons[row][col] = new JButton(""); gamePanel.add(buttons[row][col]); } } } }