Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help with my GUI for TicTacToe Game

  1. #1
    Junior Member
    Join Date
    Jan 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Help with my GUI for TicTacToe Game

    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]);
                }
            }
     
     
     
        }
    }
    Last edited by nicsxox; January 16th, 2018 at 07:17 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with my GUI for TicTacToe Game

    The API doc says:
    Specifying the number of columns affects the layout only when the number of rows is set to zero.
    Try setting the number of rows to 0.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2018
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with my GUI for TicTacToe Game

    Quote Originally Posted by Norm View Post
    The API doc says:
    Try setting the number of rows to 0.

    Hi what do you mean by this? I am new to Java so sorry for the question. Also, another problem. Tried to run the code again with no changes, but now it doesn't show anything. PLEASE HELP

    IMG_2089.JPG

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with my GUI for TicTacToe Game

    The GridLayout class's constructor takes two args: number of rows and number of columns.
    Try setting the number of rows to zero.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Tictactoe with out GUI
    By Andile in forum Java Theory & Questions
    Replies: 2
    Last Post: September 15th, 2012, 03:49 PM
  2. tictactoe client-server game
    By nuubikz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 20th, 2011, 05:25 AM
  3. while loop in GUI game
    By Hamran2011 in forum Loops & Control Statements
    Replies: 6
    Last Post: November 4th, 2011, 10:04 AM
  4. Game of Life GUI Error
    By Lavace in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 3rd, 2011, 09:15 AM
  5. help building up GUI for poker game
    By Pencil in forum AWT / Java Swing
    Replies: 5
    Last Post: October 26th, 2010, 02:53 PM