Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2

    Java class help for game


    I'm learning Java myself, and I'm learning about GUI. I made a tictactoe game using GUI, and it was working, so I decided to modify if using more classes. Now my problem is that, if the player wins, nothing is displayed in the JLabel. Before, I used if statements to display the message on JLabel, it was working then, but I've made it to boolean, and now it's not working.

    Thanks in advance,

    Tony Antony

    Code:
    package tictactoe;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class gamePanel extends JPanel{
        
        private JButton b1,b2,b3,b4,b5,b6,b7,b8,b9;
        private JLabel label;
        private String mark;
        private int count=0;
        private boolean winner;
        
        public gamePanel()
        {      
            ButtonListener listener = new ButtonListener();
            
            setPreferredSize(new Dimension(450,450));
            setLayout(new GridLayout(4,3));
                          
            b1=new JButton(" ");
            b1.addActionListener(listener);
            add(b1);
            
            b2=new JButton(" ");
            b2.addActionListener(listener);
            add(b2);
            
            b3=new JButton(" ");
            b3.addActionListener(listener);
            add(b3);
            
            b4=new JButton(" ");
            b4.addActionListener(listener);
            add(b4);
            
            b5=new JButton(" ");
            b5.addActionListener(listener);
            add(b5);
            
            b6=new JButton(" ");
            b6.addActionListener(listener);
            add(b6);
            
            b7=new JButton(" ");
            b7.addActionListener(listener);
            add(b7);
            
            b8=new JButton(" ");
            b8.addActionListener(listener);
            add(b8);
            
            b9=new JButton(" ");
            b9.addActionListener(listener);
            add(b9);
            
            label = new JLabel("");
            add(label);
        }
        
        
        
        private class ButtonListener implements ActionListener
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                count++;
                
                if(count %2 !=0)
                {
                    mark="X";
                }  
                else
                {
                    mark="O";
                } 
                
                if(e.getSource()==b1)
                {
                    b1.setText(mark);
                    b1.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b1.setEnabled(false);
                }
                else if(e.getSource()==b2)
                {
                    b2.setText(mark);
                    b2.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b2.setEnabled(false);
                }
                else if(e.getSource()==b3)
                {
                    b3.setText(mark);
                    b3.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b3.setEnabled(false);
                }
                else if(e.getSource()==b4)
                {
                    b4.setText(mark);
                    b4.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b4.setEnabled(false);
                }
                else if(e.getSource()==b5)
                {
                    b5.setText(mark);
                    b5.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b5.setEnabled(false);
                }
                else if(e.getSource()==b6)
                {
                    b6.setText(mark);
                    b6.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b6.setEnabled(false);
                }
                else if(e.getSource()==b7)
                {
                    b7.setText(mark);
                    b7.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b7.setEnabled(false);
                }
                else if(e.getSource()==b8)
                {
                    b8.setText(mark);
                    b8.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b8.setEnabled(false);
                }
                else if (e.getSource()==b9)
                {
                    b9.setText(mark);
                    b9.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b9.setEnabled(false);
                }     
            }        
        }    
         public boolean playerWin()
                {                
                if(b1.getText()=="X" && b2.getText()=="X" && b2.getText()=="X" && b3.getText()=="X")
                    winner=true;
                else if(b4.getText()=="X" && b5.getText()=="X" && b5.getText()=="X" && b6.getText()=="X")
                    winner=true;
                else if(b7.getText()=="X" && b8.getText()=="X" && b8.getText()=="X" && b9.getText()=="X")
                    winner=true;
                else if(b1.getText()=="X" && b4.getText()=="X" && b4.getText()=="X" && b7.getText()=="X")
                    winner=true;
                else if(b2.getText()=="X" && b5.getText()=="X" && b5.getText()=="X" && b8.getText()=="X")
                    winner=true;
                else if(b3.getText()=="X" && b6.getText()=="X" && b6.getText()=="X" && b9.getText()=="X")
                    winner=true;
                else if(b1.getText()=="X" && b5.getText()=="X" && b5.getText()=="X" && b9.getText()=="X")
                    winner=true;
                else if(b3.getText()=="X" && b5.getText()=="X" && b5.getText()=="X" && b7.getText()=="X")
                    winner=true;
                else
                    winner=false;
                
                return winner;
                }
    
        @Override
         public void show()
         {      
             if(winner==true)
             {
                 label.setText("Player wins");
             }
             else
             {
                 label.setText("Computer wins");
             }
         }
    }
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    One problem I see is using == to compare Strings. Use the equals() method to compare Strings.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    Originally Posted by NormR
    One problem I see is using == to compare Strings. Use the equals() method to compare Strings.
    Thank you, I fixed that, but still same problem.

    Code:
    package tictactoe;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class gamePanel extends JPanel{
        
        private JButton b1,b2,b3,b4,b5,b6,b7,b8,b9;
        private JLabel label;
        private String mark;
        private int count=0;
        private boolean winner;
        
        public gamePanel()
        {      
            ButtonListener listener = new ButtonListener();
            
            setPreferredSize(new Dimension(450,450));
            setLayout(new GridLayout(4,3));
                          
            b1=new JButton(" ");
            b1.addActionListener(listener);
            add(b1);
            
            b2=new JButton(" ");
            b2.addActionListener(listener);
            add(b2);
            
            b3=new JButton(" ");
            b3.addActionListener(listener);
            add(b3);
            
            b4=new JButton(" ");
            b4.addActionListener(listener);
            add(b4);
            
            b5=new JButton(" ");
            b5.addActionListener(listener);
            add(b5);
            
            b6=new JButton(" ");
            b6.addActionListener(listener);
            add(b6);
            
            b7=new JButton(" ");
            b7.addActionListener(listener);
            add(b7);
            
            b8=new JButton(" ");
            b8.addActionListener(listener);
            add(b8);
            
            b9=new JButton(" ");
            b9.addActionListener(listener);
            add(b9);
            
            label = new JLabel("");
            add(label);
        }
        
        
        
        private class ButtonListener implements ActionListener
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                count++;
                
                if(count %2 !=0)
                {
                    mark="X";
                }  
                else
                {
                    mark="O";
                } 
                
                if(e.getSource()==b1)
                {
                    b1.setText(mark);
                    b1.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b1.setEnabled(false);
                }
                else if(e.getSource()==b2)
                {
                    b2.setText(mark);
                    b2.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b2.setEnabled(false);
                }
                else if(e.getSource()==b3)
                {
                    b3.setText(mark);
                    b3.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b3.setEnabled(false);
                }
                else if(e.getSource()==b4)
                {
                    b4.setText(mark);
                    b4.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b4.setEnabled(false);
                }
                else if(e.getSource()==b5)
                {
                    b5.setText(mark);
                    b5.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b5.setEnabled(false);
                }
                else if(e.getSource()==b6)
                {
                    b6.setText(mark);
                    b6.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b6.setEnabled(false);
                }
                else if(e.getSource()==b7)
                {
                    b7.setText(mark);
                    b7.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b7.setEnabled(false);
                }
                else if(e.getSource()==b8)
                {
                    b8.setText(mark);
                    b8.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b8.setEnabled(false);
                }
                else if (e.getSource()==b9)
                {
                    b9.setText(mark);
                    b9.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b9.setEnabled(false);
                }     
            }        
        }    
         public boolean playerWin()
                {                
                if(b1.equals(b2) && b3.equals(b2))
                    winner=true;
                else if(b4.equals(b5) && b6.equals(b5))
                    winner=true;
                else if(b7.equals(b8) && b9.equals(b8))
                    winner=true;
                else if(b1.equals(b4) && b4.equals(b4))
                    winner=true;
                else if(b2.equals(b5) && b8.equals(b5))
                    winner=true;
                else if(b3.equals(b6) && b9.equals(b6))
                    winner=true;
                else if(b1.equals(b5) && b9.equals(b5))
                    winner=true;
                else if(b3.equals(b5) && b7.equals(b5))
                    winner=true;
                else
                    winner=false;
                
                return winner;
                }
    
        @Override
         public void show()
         {      
             if(winner==true)
             {
                 label.setText("Player wins");
             }
             else
             {
                 label.setText("Computer wins");
             }
         }
    }
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Is the code that sets the label ever executed? Add a println statement that prints out a message just before where the label's value is changed to see if the code is executed.
    If nothing prints out, the code is not executed.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    Originally Posted by NormR
    Is the code that sets the label ever executed? Add a println statement that prints out a message just before where the label's value is changed to see if the code is executed.
    If nothing prints out, the code is not executed.
    I tried that earlier, and now, but java is giving me an error message saying "illegal start of type". The error is on the System.out.println("The winner is " + winner); section

    Code:
    package tictactoe;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class gamePanel extends JPanel{
        
        private JButton b1,b2,b3,b4,b5,b6,b7,b8,b9;
        private JLabel label;
        private String mark;
        private int count=0;
        private boolean winner;
        
        public gamePanel()
        {      
            ButtonListener listener = new ButtonListener();
            
            setPreferredSize(new Dimension(450,450));
            setLayout(new GridLayout(4,3));
                          
            b1=new JButton(" ");
            b1.addActionListener(listener);
            add(b1);
            
            b2=new JButton(" ");
            b2.addActionListener(listener);
            add(b2);
            
            b3=new JButton(" ");
            b3.addActionListener(listener);
            add(b3);
            
            b4=new JButton(" ");
            b4.addActionListener(listener);
            add(b4);
            
            b5=new JButton(" ");
            b5.addActionListener(listener);
            add(b5);
            
            b6=new JButton(" ");
            b6.addActionListener(listener);
            add(b6);
            
            b7=new JButton(" ");
            b7.addActionListener(listener);
            add(b7);
            
            b8=new JButton(" ");
            b8.addActionListener(listener);
            add(b8);
            
            b9=new JButton(" ");
            b9.addActionListener(listener);
            add(b9);
            
            label = new JLabel("test");
            add(label);
        }
        
        private class ButtonListener implements ActionListener
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                count++;
                
                if(count %2 !=0)
                {
                    mark="X";
                }  
                else
                {
                    mark="O";
                } 
                
                if(e.getSource()==b1)
                {
                    b1.setText(mark);
                    b1.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b1.setEnabled(false);
                }
                else if(e.getSource()==b2)
                {
                    b2.setText(mark);
                    b2.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b2.setEnabled(false);
                }
                else if(e.getSource()==b3)
                {
                    b3.setText(mark);
                    b3.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b3.setEnabled(false);
                }
                else if(e.getSource()==b4)
                {
                    b4.setText(mark);
                    b4.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b4.setEnabled(false);
                }
                else if(e.getSource()==b5)
                {
                    b5.setText(mark);
                    b5.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b5.setEnabled(false);
                }
                else if(e.getSource()==b6)
                {
                    b6.setText(mark);
                    b6.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b6.setEnabled(false);
                }
                else if(e.getSource()==b7)
                {
                    b7.setText(mark);
                    b7.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b7.setEnabled(false);
                }
                else if(e.getSource()==b8)
                {
                    b8.setText(mark);
                    b8.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b8.setEnabled(false);
                }
                else if (e.getSource()==b9)
                {
                    b9.setText(mark);
                    b9.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b9.setEnabled(false);
                }     
            }        
        }    
         public boolean playerWin()
                {                
                if(b1.equals(b2) && b3.equals(b2))
                    winner=true;
                else if(b4.equals(b5) && b6.equals(b5))
                    winner=true;
                else if(b7.equals(b8) && b9.equals(b8))
                    winner=true;
                else if(b1.equals(b4) && b4.equals(b4))
                    winner=true;
                else if(b2.equals(b5) && b8.equals(b5))
                    winner=true;
                else if(b3.equals(b6) && b9.equals(b6))
                    winner=true;
                else if(b1.equals(b5) && b9.equals(b5))
                    winner=true;
                else if(b3.equals(b5) && b7.equals(b5))
                    winner=true;
                else
                    winner=false;
                
                return winner;
                }
         
         System.out.println("The winner is " + winner);
    
        @Override
         public void show()
         {      
             if(winner==true)
             {
                 label.setText("Player wins");
             }
             else
             {
                 label.setText("Computer wins");
             }
         }
    }
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    The line with the error is outside of a method which is illegal. Move that statement inside of a method.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    Originally Posted by NormR
    The line with the error is outside of a method which is illegal. Move that statement inside of a method.
    I added a system.out.println inside the show() method, but when I ran the program, I didn't get anything in the console

    Code:
    package tictactoe;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class gamePanel extends JPanel{
        
        private JButton b1,b2,b3,b4,b5,b6,b7,b8,b9;
        private JLabel label;
        private String mark;
        private int count=0;
        private boolean winner;
        
        public gamePanel()
        {      
            ButtonListener listener = new ButtonListener();
            
            setPreferredSize(new Dimension(450,450));
            setLayout(new GridLayout(4,3));
                          
            b1=new JButton(" ");
            b1.addActionListener(listener);
            add(b1);
            
            b2=new JButton(" ");
            b2.addActionListener(listener);
            add(b2);
            
            b3=new JButton(" ");
            b3.addActionListener(listener);
            add(b3);
            
            b4=new JButton(" ");
            b4.addActionListener(listener);
            add(b4);
            
            b5=new JButton(" ");
            b5.addActionListener(listener);
            add(b5);
            
            b6=new JButton(" ");
            b6.addActionListener(listener);
            add(b6);
            
            b7=new JButton(" ");
            b7.addActionListener(listener);
            add(b7);
            
            b8=new JButton(" ");
            b8.addActionListener(listener);
            add(b8);
            
            b9=new JButton(" ");
            b9.addActionListener(listener);
            add(b9);
            
            label = new JLabel("");
            add(label);
        }
        
        private class ButtonListener implements ActionListener
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                count++;
                
                if(count %2 !=0)
                {
                    mark="X";
                }  
                else
                {
                    mark="O";
                } 
                
                if(e.getSource()==b1)
                {
                    b1.setText(mark);
                    b1.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b1.setEnabled(false);
                }
                else if(e.getSource()==b2)
                {
                    b2.setText(mark);
                    b2.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b2.setEnabled(false);
                }
                else if(e.getSource()==b3)
                {
                    b3.setText(mark);
                    b3.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b3.setEnabled(false);
                }
                else if(e.getSource()==b4)
                {
                    b4.setText(mark);
                    b4.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b4.setEnabled(false);
                }
                else if(e.getSource()==b5)
                {
                    b5.setText(mark);
                    b5.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b5.setEnabled(false);
                }
                else if(e.getSource()==b6)
                {
                    b6.setText(mark);
                    b6.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b6.setEnabled(false);
                }
                else if(e.getSource()==b7)
                {
                    b7.setText(mark);
                    b7.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b7.setEnabled(false);
                }
                else if(e.getSource()==b8)
                {
                    b8.setText(mark);
                    b8.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b8.setEnabled(false);
                }
                else if (e.getSource()==b9)
                {
                    b9.setText(mark);
                    b9.setFont(new Font("Helvetica", Font.BOLD, 72));
                    b9.setEnabled(false);
                }     
            }        
        }    
         public boolean playerWin()
                {                
                if(b1.equals(b2) && b3.equals(b2))
                    winner=true;
                else if(b4.equals(b5) && b6.equals(b5))
                    winner=true;
                else if(b7.equals(b8) && b9.equals(b8))
                    winner=true;
                else if(b1.equals(b4) && b4.equals(b4))
                    winner=true;
                else if(b2.equals(b5) && b8.equals(b5))
                    winner=true;
                else if(b3.equals(b6) && b9.equals(b6))
                    winner=true;
                else if(b1.equals(b5) && b9.equals(b5))
                    winner=true;
                else if(b3.equals(b5) && b7.equals(b5))
                    winner=true;
                else
                    winner=false;
                
                return winner;
               
                }  
         
        @Override
         public void show()
         {      
             System.out.println("The value is changed");
             
             if(winner==true)
             {
                 label.setText("The winner is player");
             }
             else
             {
                 label.setText("The winner is computer");
             }
         }
    }
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    I didn't get anything in the console
    That means that the println() statement was not executed.
    Where does the code call the show() method?
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    I don't see any obvious problems. I returned the value of boolean winner, so shouldn't show() know what the value of winner from the playerWin() method? My logic is, so if the value of playerWin=true, show player win, else show computer win.
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    The show() method must be called for the statements in it to be executed.
    Where is the show() method called?
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    should I call the show() method in the main class?
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Call the show() method when you want it to be executed.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    Originally Posted by tonynsx
    should I call the show() method in the main class?
    currently, it's being called the gamePanel() class, with out a main
  26. #14
  27. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Are you sure it is being called? The println() statement would print on the console IF the method was called. I guess it is not being called if the print out does not show.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    so how do I call it? I'm out of ideas, everything else is working.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo