Results 1 to 4 of 4
  1. #1
    nonybrighto is offline Member
    Join Date
    Oct 2012
    Location
    ph
    Posts
    43
    Rep Power
    0

    Default my guess game problem

    my game is having a problem. i want my jbutton to be red if the user guesses the correct number on my textfield but it shows red for a random number

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    
    public class Gridgame extends JFrame
    { 
            
        int row =5;
        int col =5;
        int guess;
        int secretenum;
        
        
         JTextArea textfield;
          JButton[][] button;
          JPanel panel1;
          JPanel panel2;
        
        Random rand = new Random();
        
      public Gridgame()
      {
          super("guess the number");
          
         
          
          
          panel1=new JPanel();
          textfield= new JTextArea("\n\n           the number",7,25);
          textfield.setBackground(Color.BLUE);
          textfield.setForeground(Color.pink);
          textfield.setFont(new Font("serif",Font.BOLD,23));
          panel1.add(textfield);
          add(panel1,BorderLayout.NORTH);
          
          panel2 = new JPanel();
        
         
         button= new JButton[row][col];
         panel2.setLayout(new GridLayout(row,button.length));
         for(int i=0;i<=button.length-1;i++)
         {
             for(int j=0;j<=button[i].length-1;j++)
             { 
                 
               button[i][j]=new JButton("$");
               button[i][j].setBackground(Color.white);
                guess=1+rand.nextInt(25);
               button[i][j].setText(String.valueOf(guess));
               panel2.add(button[i][j]);
             }
         }
          
          add(panel2);
         
         actions handler = new actions();
           for(int i=0;i<=button.length-1;i++)
                {
             for(int j=0;j<=button[i].length-1;j++)
             { 
                 
               button[i][j].addActionListener(handler);
             }
                
            }
          
        
         
      }
      
      public static void main(String[] args)
      {
           Gridgame app = new Gridgame();
           app.setSize(400,500);
           app.setVisible(true);
           app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           
      }
      
      public class actions implements ActionListener
      {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                
                String toguess="";
                int i;
                int j;
                for( i=0;i<=button.length-1;i++)
                {
              for(j=0;j<=button[i].length-1;j++)
             { 
                
                int randi=1+rand.nextInt(button.length-1);
                int randj=1+rand.nextInt(button.length-1);
               
                  toguess =button[randi][randj].getText();
                textfield.setText("\n           guess the button with "+ toguess +" " +button[randi][randj].getText());
                
                int itoguess =Integer.parseInt(toguess);
                int ibutton =Integer.parseInt(button[i][j].getText());
                        
                if(e.getSource()==button[randi][3])
              {
                 button[randi][randj].setBackground(Color.red);
                
              }
                
             }
                
            }
              
            }
          
      }
    }
    joy wouldnt feel so gud if it wasnt for pain

  2. #2
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    123
    Rep Power
    0

    Default Re: my guess game problem

    Hello,

    please indent your code properly so it's easier to read it.
    If you are using Eclipse, use Ctrl+A to select all and then Ctrl+I for indenting it automatically.

    Can you elaborate on your problem. I tried your code but I do not understand what the game is about or what you should do.

  3. #3
    nonybrighto is offline Member
    Join Date
    Oct 2012
    Location
    ph
    Posts
    43
    Rep Power
    0

    Default Re: my guess game problem

    wow....thanks a lot.you just thought me one new thing....ok the game is all about giving the buttons random name and then a random buttons name is placed on the screen for the name to be guessed if the button is clicked, the color should be red.... But instead it is giving a different button the red color
    joy wouldnt feel so gud if it wasnt for pain

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,252
    Blog Entries
    1
    Rep Power
    24

    Default Re: my guess game problem

    You should sprinkle your code with println statements to find out why it is not behaving as you are expecting it to behave.

Similar Threads

  1. help needed plz with some loop problem I guess
    By Tommeke in forum New To Java
    Replies: 6
    Last Post: 07-21-2011, 07:42 PM
  2. guess game GUI
    By tomandhisjones in forum New To Java
    Replies: 4
    Last Post: 04-04-2011, 06:30 AM
  3. Replies: 3
    Last Post: 12-20-2009, 03:22 PM
  4. guess number game
    By mistah in forum New To Java
    Replies: 10
    Last Post: 11-23-2008, 03:37 AM
  5. sample of guess high and low game
    By pouria62 in forum AWT / Swing
    Replies: 1
    Last Post: 10-26-2008, 12:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •