Hi this isnt wrking i need help!!!!

import javax.swing.*;
import java.awt.event.*;
import java.awt.*; //used for layout manager
import java.util.Random;;

class GuessGame extends JFrame
{
   private JButton newButton;
   private JTextField gField; //stands for guess field
   private JLabel firstPrompt, enterLabel, userMessageLabel;
   private int randomNumber, userGuess;
   private int counter = 0;
   private int lastGuess = 0;

   public GuessGame()
   {
      super("Guessing Game");
      setLayout(new FlowLayout());

      firstPrompt = new JLabel("I have a number between 1 and 1000 can you guess");
      enterLabel = new JLabel("Please enter your first guess.");
      userMessageLabel = new JLabel("");

      //need to have ranom number
      randomNumber = new Random().nextInt(1000) + 1;
      setBackground(Color.WHITE);

      newButton = new JButton("New Game");
      newButton.addActionListener(
      new ActionListener(){
        public void actionPerformed(ActionEvent e)
        {
         setBackground(Color.WHITE);
         userMessageLabel.setText("");
         randomNumber = new Random().nextInt(1000) + 1;
         }
      }
    );


       setSize( 600, 250 );
       setVisible(true);

      randomNumber = new Random().nextInt(1000)+1; 
      gField = new JTextField(4); //setting it to four because the guessed number should$

      gField.addActionListener(    
      new ActionListener(){

            public void actionPerformed( ActionEvent event)
            {
            counter++;
            userGuess = Integer.parseInt( gField.getText());
               if (userGuess == randomNumber)
               {
                    userMessageLabel.setText("You are correct, it took you: " + counter $
                    setBackground(Color.GREEN);
               }
               else if (userGuess > randomNumber)
               {
                    userMessageLabel.setText("Too high");
               }
               else if (userGuess < randomNumber)
               {
                    userMessageLabel.setText("Too Low");
               }

               if (counter > 1)
               {
                  if ((randomNumber - userGuess) > (randomNumber - lastGuess))
                  {
                     setBackground(Color.RED);
                  }
                   else if ((randomNumber - userGuess) < (randomNumber - lastGuess))
                  {
                     setBackground(Color.BLUE);
                  }
               }
               else
               {
                  setBackground(Color.WHITE);
               }
               lastGuess = userGuess;

            }

         }

   );

   Container c = getContentPane();
   c.setLayout( new FlowLayout());
   c.add(newButton);
   c.add(gField);
   c.add(firstPrompt);
   c.add(enterLabel);
   c.add(userMessageLabel);
 repaint();
}



public static void main(String[] args)
{
   GuessGame myGuessGame = new GuessGame(); //instantiate a GUI object
}
}

it is attached!!!!

Recommended Answers

All 3 Replies

What is it that exactly 'not works' ?
If its a compilation issue check your line 56, '$' should be ');'

Other than that you have to tell us how you want the program to behave to point you to a solution.

What is it that exactly 'not works' ?
If its a compilation issue check your line 56, '$' should be ');'

Other than that you have to tell us how you want the program to behave to point you to a solution.

im sorry well two things first is that the "make your first guess" wont go away
second thing is that it doesnt loop it just runs through i feel like i can only supmit a limited amount of answers........

thank you and sorry for the confusion....

For "make your first guess" to go away, in your actionPerformed() method for the gField, set the text of the enterLabel to "".

Second question why do you "feel" you have a limited number of guesses, programs work on logic not feelings :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.