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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 26

Thread: JPanel

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JPanel

    class SingleBoard extends JPanel
        {
            public SingleBoard (ActionListener e)
            {
                setLayout (new GridLayout (3, 3));
                for (int i = 0 ; i <= 8 ; i++)
                {
                    buttons [i] = new JButton ();
                    buttons [i].addActionListener (e);
                    add (buttons [i]);
                }
     
            }
        }
     
     
        public void init ()
        {
            //Set the applet size for better viewing
            setSize (300, 700);
     
            //Message for the players
            JLabel message = new JLabel ("X will go first");
            getContentPane ().setLayout (new BorderLayout ());
            getContentPane ().add (message, BorderLayout.NORTH);
     
            //Adding 3 boards
            getContentPane ().setLayout (new GridLayout (5, 5, 10, 10));
            for (int i = 0 ; i < 3 ; i++)
            {
                SingleBoard sb = new SingleBoard (this);
                getContentPane ().add (sb);
            }
        }

    My only problem is that the last panel is the only one I think retains the variable names buttons[0]-buttons[8], so I'm having problem to check the winner. I don't know how to access/reference the buttons on the first 2 panels


    Someone told me to add this code in:
    public JButton[] getButtons ()
    {
    return buttons;
    }
    But then how do I access it after returning?


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: JPanel

    But then how do I access it after returning?
    Do you mean how can you get to the values of the array being returned? If so, think about this: how do you access values in any other array?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JPanel

    Not exactly the values, lets say I want to access two arrays of JButton which are

    buttons[0] and buttons[1]

    I can access them fine on the last panel but, I don't know how to access them on the first two panels.

  4. #4
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: JPanel

    for (int i = 0 ; i < 3 ; i++)
    {
    SingleBoard sb = new SingleBoard (this);
    getContentPane ().add (sb);
    }
    Here's the issue: you add the same JPanel to three different spots. Since you only use one variable, you can only access one of the panels (the last one) because the last panel is the last one you define sb as. So perhaps to access all the panels, make an array of three panels and then add the individual elements? That way, each of the three panels has its own specific element that you can refer to individually.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JPanel

    private JButton buttons[] = new JButton [9];
        JPanel panel[] = new JPanel [3];
     
        private boolean checkWin = false;
        //Count turns starts at 0
        int count = 0;
     
        //X or O variable
        String symbol = "";
     
        //Sub-class for a single board
        class GameBoard
        {
            public GameBoard (ActionListener e)
            {
                setLayout (new GridLayout (3, 3));
                for (int i = 0 ; i <= 8 ; i++)
                {
                    buttons [i] = new JButton ();
                    buttons [i].addActionListener (e);
                    buttons [i].setBackground (Moccasin);
                    add (buttons [i]);
                }
     
            }
        }
     
     
        public void init ()
        {
            //Set the applet size for better viewing
            setSize (300, 680);
     
            //Scorer
            JLabel scoreMsgP1 = new JLabel ("Player1: ");
            JLabel scoreMsgP2 = new JLabel ("Player2: ");
     
     
            getContentPane ().setLayout (new BorderLayout ());
            getContentPane ().add (scoreMsgP1, BorderLayout.NORTH);
     
            //Adding boards to three panels
            getContentPane ().setLayout (new GridLayout (5, 5, 10, 10));
            for (int i = 0 ; i < panel.length ; i++)
            {
                GameBoard gb = new GameBoard (this);
                panel [i].add (gb);
            }
            getContentPane ().add (scoreMsgP2, BorderLayout.SOUTH);
        }

    Sorry about this but I tried looking on how to add a JButton to a panel, and I think that this should work:
    for (int i = 0 ; i < panel.length ; i++)
            {
                GameBoard gb = new GameBoard (this);
                panel [i].add (gb);
     
     
            }

    But there's an error message saying:
    No applicable overload for the method named "add" was found in type "java.awt.Container". Perhaps you wanted the overloaded version "java.awt.Component add(java.awt.Component $1);" instead?

  6. #6
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: JPanel

    Wait where is the GameBoard class coming from? I didn't see it before... Are you still encountering the same problem or is this a new one?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JPanel

    Oh I just changed SingleBoard to GameBoard. My problem right now is I'm not sure how to add the JButtons to the panels because of the error, which I'm not sure how to fix..

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

    Default Re: JPanel

    Since you only use one variable, you can only access one of the panels (the last one) because the last panel is the last one you define sb as.
    Not sure I understand what you are trying to say.

    The code sets the variable sb to a new object and then adds the contents of that variable (a reference) to the contentPane.
    Now the contentpane has a copy of the reference to that object. The next new object assigned to sb will not change what the contentPane has saved.
    When the loop is done, the contentPane will have saved references to 3 objects. The sb variable will be lost when the loop exits.

  9. #9
    Junior Member
    Join Date
    Nov 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JPanel

    So I should just make 3 panels like snowguy13 said, right? So that the references to the first two objects won't be lost after the loop.

  10. #10
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: JPanel

    When the loop is done, the contentPane will have saved references to 3 objects.
    Yes, I didn't mean to say otherwise. What I meant to say is that if he wants to refer to those objects later, he has to use multiple instances of SingleBoard that are defined individually and outside of the loop. Sorry for any ambiguity.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  11. #11
    Junior Member
    Join Date
    Nov 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JPanel

    So here's the whole code:

    // The "TicTacToe3d" class.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    import hsa.*;
     
    public class TicTacToe3D extends JFrame implements ActionListener
    {
        //Setting colors
        private Color Moccasin = new Color (255, 228, 181);
        private Color Cornsilk2 = new Color (238, 232, 205);
     
        //No. of panels (3 boards)
        public static final int panels = 3;
     
        private JButton buttons[] = new JButton [9];
        JPanel panelB1 = new JPanel ();
        JPanel panelB2 = new JPanel ();
        JPanel panelB3 = new JPanel ();
     
        private boolean checkWin = false;
     
     
        //Count turns starts at 0
        int count = 0;
     
        //X or O variable
        String symbol = "";
     
        //Sub-class for a single board
        class GameBoard
        {
            public GameBoard (ActionListener e)
            {
                setLayout (new GridLayout (3, 3));
                for (int i = 0 ; i <= 8 ; i++)
                {
                    buttons [i] = new JButton ();
                    buttons [i].addActionListener (e);
                    buttons [i].setBackground (Moccasin);
                    add (buttons [i]);
                }
     
            }
        }
     
     
        public void init ()
        {
            //Set the applet size for better viewing
            setSize (300, 680);
     
            //Scorer
            JLabel scoreMsgP1 = new JLabel ("Player1: ");
            JLabel scoreMsgP2 = new JLabel ("Player2: ");
     
            add (panelB1);
            add (panelB2);
            add (panelB3);
            getContentPane ().setLayout (new BorderLayout ());
            getContentPane ().add (scoreMsgP1, BorderLayout.NORTH);
     
            //Adding boards to three panels
            getContentPane ().setLayout (new GridLayout (5, 5, 10, 10));
     
     
     
            GameBoard gb = new GameBoard (this);
            panelB1.add (gb);
            panelB2.add (gb);
            panelB3.add (gb);
     
            getContentPane ().add (scoreMsgP2, BorderLayout.SOUTH);
        }
     
     
        public void actionPerformed (ActionEvent click)
        {
            count++;
     
     
     
            /*
            * If the count is an even number (divisible by 2) then it sets the symbol as O
            */
            if (count % 2 == 0)
            {
     
                symbol = "O";
     
            }
            else
            {
                symbol = "X";
            }
     
            /*
            * If a button is click it either puts on an X or O depending on count then disable clicking it again
            * It also changes the color of the button
            * The fontface and size is also changed
            */
            JButton pressedButton = (JButton) click.getSource ();
            pressedButton.setBackground (Cornsilk2);
            pressedButton.setText (symbol);
            pressedButton.setFont (new Font ("Comic Sans MS", Font.BOLD, 40));
            pressedButton.setEnabled (false);
     
            if (buttons [0].getText () == buttons [1].getText () && buttons [1].getText () == buttons [2].getText () && buttons [0].getText () != "")
            {
                checkWin = true;
            }
            if (checkWin == true)
            {
                JOptionPane.showMessageDialog (null, symbol + " wins the game!");
                System.exit (0);
            }
     
        }
    } // TicTacToe3d class

    I declared 3 different panels:
    JPanel panelB1 = new JPanel ();
        JPanel panelB2 = new JPanel ();
        JPanel panelB3 = new JPanel ();

    Then I added the GameBoard object:
    GameBoard gb = new GameBoard (this);
            panelB1.add (gb);
            panelB2.add (gb);
            panelB3.add (gb);

    But still an error saying:
    No applicable overload for the method named "add" was found in type "java.awt.Container". Perhaps you wanted the overloaded version "java.awt.Component add(java.awt.Component $1);" instead?

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

    Default Re: JPanel

    What does the API doc say for the data types of the parameters for the add() method? Is GameBoard one of the valid data types?

  13. #13
    Junior Member
    Join Date
    Nov 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JPanel

    Alright, thanks I was able to add it. Is there a layout manager for the Panels?

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

    Default Re: JPanel

    Are you asking if you can add one or what the default one is?

    Take a Look at the API doc

  15. #15
    Junior Member
    Join Date
    Nov 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JPanel

    So I tried the FlowLayout Manager, but the buttons didn't go inside the panels

    panelB1.setLayout (new FlowLayout ());
            panelB1.setBorder (BorderFactory.createLineBorder (Color.BLACK, 2));
            getContentPane ().add (panelB1, BorderLayout.NORTH);
     
            panelB2.setLayout (new FlowLayout ());
            panelB2.setBorder (BorderFactory.createLineBorder (Color.BLACK, 2));
            getContentPane ().add (panelB2, BorderLayout.CENTER);
     
            panelB3.setLayout (new FlowLayout ());
            panelB3.setBorder (BorderFactory.createLineBorder (Color.BLACK, 2));
            getContentPane ().add (panelB3, BorderLayout.PAGE_END);

    Here's the whole code so far..
    // The "TicTacToe3d" class.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    import hsa.*;
     
    public class TicTacToe3D extends JApplet implements ActionListener
    {
        //Setting colors
        private Color Moccasin = new Color (255, 228, 181);
        private Color Cornsilk2 = new Color (238, 232, 205);
     
        //No. of panels (3 boards)
        public static final int panels = 3;
     
        private JButton buttons[] = new JButton [9];
        JPanel panelB1 = new JPanel ();
     
        JPanel panelB2 = new JPanel ();
        JPanel panelB3 = new JPanel ();
     
        private boolean checkWin = false;
     
     
        //Count turns starts at 0
        int count = 0;
     
        //X or O variable
        String symbol = "";
     
        //Sub-class for a single board
        class GameBoard extends JButton
        {
            public GameBoard (ActionListener e)
            {
                setLayout (new GridLayout (3, 3));
                for (int i = 0 ; i <= 8 ; i++)
                {
                    buttons [i] = new JButton ();
                    buttons [i].addActionListener (e);
                    buttons [i].setBackground (Moccasin);
                    add (buttons [i]);
                }
     
            }
        }
     
     
        public void init ()
        {
            //Set the applet size for better viewing
            setSize (300, 680);
     
            //Scorer
            JLabel scoreMsgP1 = new JLabel ("Player1: ");
            JLabel scoreMsgP2 = new JLabel ("Player2: ");
     
            //Layouts
            panelB1.setLayout (new FlowLayout ());
            panelB1.setBorder (BorderFactory.createLineBorder (Color.BLACK, 2));
            getContentPane ().add (panelB1, BorderLayout.NORTH);
     
            panelB2.setLayout (new FlowLayout ());
            panelB2.setBorder (BorderFactory.createLineBorder (Color.BLACK, 2));
            getContentPane ().add (panelB2, BorderLayout.CENTER);
     
            panelB3.setLayout (new FlowLayout ());
            panelB3.setBorder (BorderFactory.createLineBorder (Color.BLACK, 2));
            getContentPane ().add (panelB3, BorderLayout.PAGE_END);
     
     
           // getContentPane ().add (scoreMsgP1, BorderLayout.NORTH);
     
            //Adding boards to three panels
            getContentPane ().setLayout (new GridLayout (5, 5, 10, 10));
     
            GameBoard gb = new GameBoard (this);
            panelB1.add (gb);
            panelB2.add (gb);
            panelB3.add (gb);
     
            //getContentPane ().add (scoreMsgP2, BorderLayout.SOUTH);
        }
     
     
        public void actionPerformed (ActionEvent click)
        {
            count++;
     
     
     
            /*
            * If the count is an even number (divisible by 2) then it sets the symbol as O
            */
            if (count % 2 == 0)
            {
     
                symbol = "O";
     
            }
            else
            {
                symbol = "X";
            }
     
            /*
            * If a button is click it either puts on an X or O depending on count then disable clicking it again
            * It also changes the color of the button
            * The fontface and size is also changed
            */
            JButton pressedButton = (JButton) click.getSource ();
            pressedButton.setBackground (Cornsilk2);
            pressedButton.setText (symbol);
            pressedButton.setFont (new Font ("Comic Sans MS", Font.BOLD, 40));
            pressedButton.setEnabled (false);
     
            if (buttons [0].getText () == buttons [1].getText () && buttons [1].getText () == buttons [2].getText () && buttons [0].getText () != "")
            {
                checkWin = true;
            }
            if (checkWin == true)
            {
                JOptionPane.showMessageDialog (null, symbol + " wins the game!");
                System.exit (0);
            }
     
        }
    } // TicTacToe3d class

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

    Default Re: JPanel

    the buttons didn't go inside the panels
    Where did they go?

  17. #17
    Junior Member
    Join Date
    Nov 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JPanel

    They pretty much just went in the center..

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

    Default Re: JPanel

    Where were they supposed to go? How many went where?

    Can you explain what was supposed to happen?
    What do you expect to see in the GUI?
    What do you see in the GUI?
    What do you want to see?

  19. #19
    Junior Member
    Join Date
    Nov 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JPanel

    So I think what's supposed to happen is that I make three panels say like this

    [] - First panel
    [] - Second panel
    [] - Third panel

    Then inside those panel there should be 9 buttons arrange in a 3x3 Grid.

    What I currently see is:

    [] - First panel
    [] - Second panel
    [] - Third panel

    then all the buttons are inside the third panel and are shrink-ed (which is at the center of the applet)

  20. #20
    Junior Member
    Join Date
    Nov 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JPanel

    // The "TicTacToe3d" class.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    import hsa.*;
     
    public class TicTacToe3D implements ActionListener
    {
     
        //Setting colors
        private Color Moccasin = new Color (255, 228, 181);
        private Color Cornsilk2 = new Color (238, 232, 205);
     
        //No. of panels (3 boards)
        public static final int panels = 3;
     
        private JButton buttons[] = new JButton [26];
        JPanel panelB1 = new JPanel ();
        JPanel panelB2 = new JPanel ();
        JPanel panelB3 = new JPanel ();
     
        private GameBoard[] boards = new GameBoard [panels];
     
        private boolean checkWin = false;
     
     
        //Count turns starts at 0
        int count = 0;
     
        //X or O variable
        String symbol = "";
     
        //Sub-class for a single board
        class GameBoard extends JButton
        {
            public GameBoard (ActionListener e)
            {
                setLayout (new GridLayout (3, 3));
                for (int i = 0 ; i <= 8 ; i++)
                {
                    buttons [i] = new JButton ();
                    buttons [i].addActionListener (e);
                    buttons [i].setBackground (Moccasin);
                    add (buttons [i]);
                }
     
            }
        }
     
     
        class GameBoard2 extends JButton
        {
            public GameBoard2 (ActionListener e)
            {
                setLayout (new GridLayout (3, 3));
                for (int i = 8 ; i <= 16 ; i++)
                {
                    buttons [i] = new JButton ();
                    buttons [i].addActionListener (e);
                    buttons [i].setBackground (Moccasin);
                    add (buttons [i]);
                }
     
            }
        }
     
     
        class GameBoard3 extends JButton
        {
            public GameBoard3 (ActionListener e)
            {
                setLayout (new GridLayout (3, 3));
                for (int i = 17 ; i <= 25 ; i++)
                {
                    buttons [i] = new JButton ();
                    buttons [i].addActionListener (e);
                    buttons [i].setBackground (Moccasin);
                    add (buttons [i]);
                }
     
            }
        }
     
     
     
     
        public TicTacToe3D ()
        {
            GameBoard gb = new GameBoard (this);
            GameBoard2 gb2 = new GameBoard2 (this);
            GameBoard3 gb3 = new GameBoard3 (this);
     
            JFrame frame = new JFrame ("3D Tic-Tac-Toe");
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            frame.getContentPane ().setLayout (new BorderLayout ());
     
            panelB1.setBorder (BorderFactory.createLineBorder (Color.black));
            panelB1.setPreferredSize (new Dimension (300, 300));
            panelB1.setLayout (new GridLayout (3, 3));
            panelB1.add (gb);
     
            panelB2.setBorder (BorderFactory.createLineBorder (Color.black));
            panelB2.setPreferredSize (new Dimension (300, 300));
            panelB2.setLayout (new GridLayout (3, 3));
            panelB2.add (gb2);
     
            panelB3.setBorder (BorderFactory.createLineBorder (Color.black));
            panelB3.setPreferredSize (new Dimension (300, 300));
            panelB3.setLayout (new GridLayout (3, 3));
            panelB3.add (gb3);
     
            frame.getContentPane ().add (panelB1, BorderLayout.NORTH);
            frame.getContentPane ().add (panelB2, BorderLayout.CENTER);
            frame.getContentPane ().add (panelB3, BorderLayout.SOUTH);
            frame.pack ();
            frame.setVisible (true);
     
     
            // //Set the applet size for better viewing
            // setSize (300, 680);
            //
            // //Scorer
            // JLabel scoreMsgP1 = new JLabel ("Player1: ");
            // JLabel scoreMsgP2 = new JLabel ("Player2: ");
            //
            // //Layouts
            // panelB1.setLayout (new FlowLayout ());
            // panelB1.setBorder (BorderFactory.createLineBorder (Color.BLACK, 2));
            // getContentPane ().add (panelB1, BorderLayout.NORTH);
            //
            // panelB2.setLayout (new FlowLayout ());
            // panelB2.setBorder (BorderFactory.createLineBorder (Color.BLACK, 2));
            // getContentPane ().add (panelB2, BorderLayout.CENTER);
            //
            // panelB3.setLayout (new FlowLayout ());
            // panelB3.setBorder (BorderFactory.createLineBorder (Color.BLACK, 2));
            // gEND);
            //
            //
            // // getContentPane ().add (scoreMsgP1, BorderLayout.NORTH);
            //
            // //Adding boards to three panels
            // getContentPane ().setLayout (new GridLayout (5, 5, 10, 10));
            //
     
            //getContentPane ().add (scoreMsgP2, BorderLayout.SOUTH);
     
     
        }
     
     
        public void actionPerformed (ActionEvent click)
        {
            count++;
     
     
     
            /*
            * If the count is an even number (divisible by 2) then it sets the symbol as O
            */
            if (count % 2 == 0)
            {
     
                symbol = "O";
     
            }
            else
            {
                symbol = "X";
            }
     
            /*
            * If a button is click it either puts on an X or O depending on count then disable clicking it again
            * It also changes the color of the button
            * The fontface and size is also changed
            */
            JButton pressedButton = (JButton) click.getSource ();
            pressedButton.setBackground (Cornsilk2);
            pressedButton.setText (symbol);
            pressedButton.setFont (new Font ("Comic Sans MS", Font.BOLD, 40));
            pressedButton.setEnabled (false);
     
            if (buttons [0].getText () == buttons [1].getText () && buttons [1].getText () == buttons [2].getText () && buttons [0].getText () != "")
            {
                checkWin = true;
            }
            if (checkWin == true)
            {
                JOptionPane.showMessageDialog (null, symbol + " wins the game!");
                System.exit (0);
            }
     
        }
     
     
        public static void main (String[] args)
        {
     
            new TicTacToe3D ();
        }
    } // TicTacToe3d class

    Now I think I was able to fix the problem by adding 3 JPanels. The only thing now is that After each panel there's a big space between each panels, how do I get rid of that? can't seem to find it on my codes. Sorry

  21. #21
    Junior Member
    Join Date
    Nov 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JPanel

    Here's the whole code:
    // The "TicTacToe3d" class.
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
    import hsa.*;
    public class TicTacToe3D extends JFrame implements ActionListener
    {
        //Setting colors
        private Color Moccasin = new Color (255, 228, 181);
        private Color Cornsilk2 = new Color (238, 232, 205);
     
        private JButton buttons[] = new JButton [27];
     
        JPanel panelB1 = new JPanel ();
        JPanel panelB2 = new JPanel ();
        JPanel panelB3 = new JPanel ();
     
       private int[][] winCombinations = new int[][] 
       {
     
        //First Board Horizontal/Vertical/Diagonal win
        {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, 
        {0, 3, 6}, {1, 4, 7}, {2, 5, 8},
        {0, 4, 8}, {2, 4, 6},  
     
        //Second Board Horizontal/Vertical/Diagonal win
        {9,10,11}, {12,13,14},{15,16,17},
        {9,12,15}, {10,13,16}, {11,14,17},
        {9,13,17}, {11,13,15},
     
        //Third Board Horizontal/Vertical/Diagonal win
        {18,19,20}, {21,22,23}, {24,25,26},
        {18,21,24}, {19,22,25}, {20,23,26},
        {18,22,26}, {20,22,24},
     
        //Across the board winning
        {0,15,21},{0,12,24},{0,10,20},{0,12,24},    
        {3,9,24}, {3,13,23}, {3,15,18}, {3,22,14},    
        {6,16,26}, {6,12,20}, {6,22,11}, {6,25,17}, {6,9,21}, {6,18,12},    
        {1,9,20},{1,18,11},{1,13,25},{1,16,22},    
        {4,10,25}, {4,12,23}, {4,16,19}, {4,14,21},{4,15,20},{4,11,24},    
        {7,15,26}, {7,17,24}, {7,13,19}, {7,22,10},    
        {2,13,24}, {2,15,22}, {2,14,26}, {2,13,17}, {2,10,8}, {2,19,9},    
        {5,13,21}, {5, 22, 12}, {5,11,26}, {5,17,20},    
        {8,13,18}, {8,9,22}, {8,15,25}, {8,11,23}, {8,20,14},  
     
        };
     
        private boolean checkWin = false;
     
        //Count turns starts at 0
        int count = 0;
     
        //X or O variable
        String symbol = "";
     
        //3 Sub-classes for each tic-tac-toe board
        class GameBoard extends JButton
        {
            public GameBoard (ActionListener e)
            {
                setLayout (new GridLayout (3, 3));
                for (int i = 0 ; i <= 8 ; i++)
                {
                    buttons [i] = new JButton ();
                    buttons [i].addActionListener (e);
                    buttons [i].setBackground (Moccasin);
                    buttons [i].setPreferredSize (new Dimension (100, 100));
                    add (buttons [i]);
                }
     
            }
        }
     
     
        class GameBoard2 extends JButton
        {
            public GameBoard2 (ActionListener e)
            {
                setLayout (new GridLayout (3, 3));
                for (int i = 9 ; i <= 17 ; i++)
                {
                    buttons [i] = new JButton ();
                    buttons [i].addActionListener (e);
                    buttons [i].setBackground (Moccasin);
                    buttons [i].setPreferredSize (new Dimension (100, 100));
                    add (buttons [i]);
                }
     
            }
        }
     
     
        class GameBoard3 extends JButton
        {
            public GameBoard3 (ActionListener e)
            {
                setLayout (new GridLayout (3, 3));
                for (int i = 18 ; i <= 26 ; i++)
                {
                    buttons [i] = new JButton ();
                    buttons [i].addActionListener (e);
                    buttons [i].setBackground (Moccasin);
                    buttons [i].setPreferredSize (new Dimension (300, 150));
                    add (buttons [i]);
                }
     
            }
        }
     
     
        public TicTacToe3D ()
        {
            GameBoard gb = new GameBoard (this);
            GameBoard2 gb2 = new GameBoard2 (this);
            GameBoard3 gb3 = new GameBoard3 (this);
     
            JFrame frame = new JFrame ("3D Tic-Tac-Toe");
            frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
            frame.getContentPane ().setLayout (new BorderLayout ());
     
            // Scorer
            // JLabel scoreMsgP1 = new JLabel ("Player1: ");
            // JLabel scoreMsgP2 = new JLabel ("Player2: ");
     
            //        frame.getContentPane ().add (scoreMsgP2, BorderLayout.SOUTH);
     
            panelB1.setBorder (BorderFactory.createLineBorder (Color.black));
            panelB1.setPreferredSize (new Dimension (320, 320));
            panelB1.setLayout (new GridLayout (3, 3));
            panelB1.add (gb);
     
            panelB2.setBorder (BorderFactory.createLineBorder (Color.black));
            panelB2.setPreferredSize (new Dimension (320, 320));
            panelB2.setLayout (new GridLayout (3, 3));
            panelB2.add (gb2);
     
            panelB3.setBorder (BorderFactory.createLineBorder (Color.black));
            panelB3.setPreferredSize (new Dimension (320, 320));
            panelB3.setLayout (new GridLayout (3, 3));
            panelB3.add (gb3);
     
            frame.getContentPane ().add (panelB1, BorderLayout.NORTH);
            frame.getContentPane ().add (panelB2, BorderLayout.CENTER);
            frame.getContentPane ().add (panelB3, BorderLayout.SOUTH);
            frame.pack ();
            frame.setVisible (true);
     
     
        }
     
     
        public void actionPerformed (ActionEvent click)
        {
            count++;
     
            /*
            * If the count is an even number (divisible by 2) then it sets the symbol as O
            */
            if (count % 2 == 0)
            {
     
                symbol = "O";
     
            }
            else
            {
                symbol = "X";
            }
     
            /*
            * If a button is click it either puts on an X or O depending on count then disable clicking it again
            * It also changes the color of the button
            * The fontface and size is also changed
            */
            JButton pressedButton = (JButton) click.getSource ();
            pressedButton.setBackground (Cornsilk2);
            pressedButton.setText (symbol);
            pressedButton.setFont (new Font ("Comic Sans MS", Font.BOLD, 40));
            pressedButton.setEnabled (false);
     
            for (int i = 0 ; i <= 7 ; i++)
            {
                if (buttons [winCombinations [i] [0]].getText ().equals (buttons [winCombinations [i] [1]].getText ()) &&
                        buttons [winCombinations [i] [1]].getText ().equals (buttons [winCombinations [i] [2]].getText ()) &&
                        buttons [winCombinations [i] [0]].getText () != "")
                {
                    checkWin = true;
                }
            }
     
     
            if (checkWin == true)
            {
                JOptionPane.showMessageDialog (null, symbol + "Gets a Point!");          
                System.exit(0);
            }
        }
     
        public static void main (String[] args)
        {
            new TicTacToe3D ();
        }
    } // TicTacToe3d class

    My problem is it doesn't do everything on the checkWinner which is this
     private int[][] winCombinations = new int[][] 
       {
     
        //First Board Horizontal/Vertical/Diagonal win
        {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, 
        {0, 3, 6}, {1, 4, 7}, {2, 5, 8},
        {0, 4, 8}, {2, 4, 6},  
     
        //Second Board Horizontal/Vertical/Diagonal win
        {9,10,11}, {12,13,14},{15,16,17},
        {9,12,15}, {10,13,16}, {11,14,17},
        {9,13,17}, {11,13,15},
     
        //Third Board Horizontal/Vertical/Diagonal win
        {18,19,20}, {21,22,23}, {24,25,26},
        {18,21,24}, {19,22,25}, {20,23,26},
        {18,22,26}, {20,22,24},
     
        //Across the board winning
        {0,15,21},{0,12,24},{0,10,20},{0,12,24},    
        {3,9,24}, {3,13,23}, {3,15,18}, {3,22,14},    
        {6,16,26}, {6,12,20}, {6,22,11}, {6,25,17}, {6,9,21}, {6,18,12},    
        {1,9,20},{1,18,11},{1,13,25},{1,16,22},    
        {4,10,25}, {4,12,23}, {4,16,19}, {4,14,21},{4,15,20},{4,11,24},    
        {7,15,26}, {7,17,24}, {7,13,19}, {7,22,10},    
        {2,13,24}, {2,15,22}, {2,14,26}, {2,13,17}, {2,10,8}, {2,19,9},    
        {5,13,21}, {5, 22, 12}, {5,11,26}, {5,17,20},    
        {8,13,18}, {8,9,22}, {8,15,25}, {8,11,23}, {8,20,14},  
     
        };
    The only thing that mainly works is the First GameBoard, other game boards doesn't check even if I get like 3 X or O in a row...

    I'm guessing that this is the problem though, I don't know how to fix it.
     for (int i = 0 ; i <= 7 ; i++)
            {
                if (buttons [winCombinations [i] [0]].getText ().equals (buttons [winCombinations [i] [1]].getText ()) &&
                        buttons [winCombinations [i] [1]].getText ().equals (buttons [winCombinations [i] [2]].getText ()) &&
                        buttons [winCombinations [i] [0]].getText () != "")
                {
                    checkWin = true;
                }
            }

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

    Default Re: JPanel

    One possible problem: != instead of ! equals()

    How many winCombinations are there?
    Last edited by Norm; January 17th, 2012 at 07:44 AM.

  23. #23
    Junior Member
    Join Date
    Nov 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JPanel

    63, I changed the for loop from 7 to 63.

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

    Default Re: JPanel

    What if you have miscounted? You should use the array's .length attribute and NOT hardcode a number in the for loop.

  25. #25
    Junior Member
    Join Date
    Nov 2011
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JPanel

    but if my miscounted wount it be outofbounds?

Page 1 of 2 12 LastLast

Similar Threads

  1. [SOLVED] Pesky <JPanel>.getWidth() and <JPanel>.getHeight() Methods...
    By snowguy13 in forum AWT / Java Swing
    Replies: 1
    Last Post: December 31st, 2011, 03:35 PM
  2. Add Image to JPanel
    By bgroenks96 in forum Java Theory & Questions
    Replies: 10
    Last Post: June 16th, 2011, 02:44 PM
  3. JButton on JPanel
    By JavaLearner in forum AWT / Java Swing
    Replies: 4
    Last Post: March 26th, 2010, 07:46 AM
  4. How to copy image from one jpanel to another jpanel
    By ramanavarayuri1986 in forum AWT / Java Swing
    Replies: 0
    Last Post: February 15th, 2010, 02:36 AM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM