Hi,

I've gotten this program to work but I want to format it differently. I feel like I've tried everything to make the changes that I want but they just don't work. I think I may have the right idea on most of what I want to change but keep putting statements in the wrong place. Basically, this program has a random number between 1 and 1000 and the user is supposed to guess it. The program keeps up with the number of guesses, turns the frame red when the user is getting warmer, turns it blue when they are getting colder, and yellow when they guess it correctly. After each guess, it keeps their last guess in an editable JTextField but I would like it to clear the field for each new guess so that the user doesn't have to backspace to enter a new guess each time. I attached a Word doc with some screenshots of the output and explanations of what I'd like to change. I just can't seem to make any changes work. I keep doing things that mess up something else. Anyway, I've put the code back to what works and am pasting it below. I just don't know where or how to make the changes that I want. Can someone help me?

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Graphics;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Graphics;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.util.Random;

import javax.swing.JFrame;

import javax.swing.JTextField;

import javax.swing.JLabel;

import javax.swing.JButton;

import javax.swing.SwingUtilities;

 

public class GuessNumber extends JFrame

{

private int Guesses = 0;

private int GuessOld = 0;

private int number; // application's number

private JTextField guessInputJTextField; // user input field

private JLabel prompt1JLabel; // first line of instruction

private JLabel prompt2JLabel; // second line of instructions

private JLabel messageJLabel; // displays message of game status

private JLabel message2JLabel; //display number of guesses


private JLabel random1 = new JLabel("");

private JButton newGameJButton; // creates new game

private Color background; // background color of application

 

// set up GUI and initialize values

public GuessNumber()

{

super("Guessing Game");

 

setLayout(new FlowLayout());

background = Color.LIGHT_GRAY; // set background to light gray

 

prompt1JLabel = new JLabel("I have a number between 1 and 1000."); // describe game

add(prompt1JLabel);

prompt2JLabel = new JLabel("Can you guess my number? Enter your Guess:"); // prompt user

add(prompt2JLabel);

 

guessInputJTextField = new JTextField(5); // to enter guesses

guessInputJTextField.addActionListener(new GuessHandler());

add(guessInputJTextField);

 

messageJLabel = new JLabel("");

add(messageJLabel);

 

message2JLabel = new JLabel ("");

add (message2JLabel);

 

newGameJButton = new JButton("New Game"); // create "New Game" button

add(newGameJButton); // add newGame button to JFrame

 

Random generator = new Random();

number = generator.nextInt(1001);//create random number

 

   
      newGameJButton.addActionListener(
   
      new ActionListener() // anonymous inner class
   
      {
   
      public void actionPerformed(ActionEvent e)
   
      {
   
      theGame ();
   
      } // end method actionPerformed
   
      } // end anonymous inner class
   
      ); // end call to addActionListener

 

 

theGame(); // start new game

} // end GuessGameFrame constructor

 

// choose a new random number
    public void theGame()
    {
        guessInputJTextField.setText("");
        Random generator = new Random();
        number = generator.nextInt(1001);
        random1.setText("" + number);
        SwingUtilities.updateComponentTreeUI(random1);
        messageJLabel.setText("");
        guessInputJTextField.setEditable(true);
        Guesses = 0;
        message2JLabel.setText(" # of guesses = " + Integer.toString(Guesses));
		  getContentPane().setBackground(Color.LIGHT_GRAY);
    } // end method theGame
 

// change background color

class GuessHandler implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

int Guess;

 

Guess = Integer.parseInt(guessInputJTextField.getText());

if (Math.abs(number - Guess) < Math.abs(number - GuessOld))

{

// Hotter

getContentPane().setBackground(Color.RED);


}

else

{

// Colder

getContentPane().setBackground(Color.BLUE);

}

 

GuessOld = Guess;

if (Guess > number)

{

messageJLabel.setText("Too High.");

SwingUtilities.updateComponentTreeUI(messageJLabel);

}

 

if (Guess < number)

{

messageJLabel.setText("Too Low.");

SwingUtilities.updateComponentTreeUI(messageJLabel);

}

// end if

 

if (Guess == number) // guess is too low

{

messageJLabel.setText("Correct!");
getContentPane().setBackground(Color.YELLOW);

SwingUtilities.updateComponentTreeUI(messageJLabel);

guessInputJTextField.setEditable(false);

}

 

Guesses++;

message2JLabel.setText(" # of guesses = " + Integer.toString(Guesses));

}

}

}
import javax.swing.JFrame;
   
       
   
      public class GameGuessNumber{
   
      public static void main(String args[]) throws Exception{
   
      GuessNumber game = new GuessNumber();
   
      game.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   
      game.setSize(550, 150);
   
      game.setVisible(true);
      }
		}

Ok,

I've gotten my program to tell the user to "Enter guess number n" but I still can't figure out how to clear the JTextField each time so that the user doesn't have to backspace just to enter a new guess. I have gotten it to tell the user "You're getting warmer" or "Your getting colder" but after the first time when it says "You're getting warmer" every time after that they both pop up no matter what and then when I use my 'New Game' button, they're STILL both there. Everything else resets like it should, though. The colder and warmer messages is something that I think my instructor wants to to see. I'm not sure about the clear JTextField each time but I think that would make it more user-friendly and I like making programs just as user-friendly as possible. I'll post my updated code below. Can anyone see where I'm messing up?

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Graphics;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.util.Random;

import javax.swing.JFrame;

import javax.swing.JTextField;

import javax.swing.JLabel;

import javax.swing.JButton;

import javax.swing.SwingUtilities;

 

public class GuessNumber extends JFrame

{

private int Guesses = 0;

private int GuessOld = 0;

private int number; // application's number

private JTextField guessInputJTextField; // user input field

private JLabel prompt1JLabel; // first line of instruction

private JLabel prompt2JLabel; // second line of instructions

private JLabel messageJLabel; // displays message of game status

private JLabel message2JLabel; //display number of guesses

private JLabel message3JLabel;

private JLabel message4JLabel;

private JLabel random1 = new JLabel("");

private JButton newGameJButton; // creates new game

private Color background; // background color of application

 

// set up GUI and initialize values

public GuessNumber()

{

super("Guessing Game");

 

setLayout(new FlowLayout());

background = Color.LIGHT_GRAY; // set background to light gray

 

prompt1JLabel = new JLabel("I have a number between 1 and 1000."); // describe game

add(prompt1JLabel);

prompt2JLabel = new JLabel("Can you guess my number? Type your Guess and then press 'Enter':"); // prompt user

add(prompt2JLabel);

 

guessInputJTextField = new JTextField(5); // to enter guesses

guessInputJTextField.addActionListener(new GuessHandler());

add(guessInputJTextField);

 

messageJLabel = new JLabel("");

add(messageJLabel);

 

message2JLabel = new JLabel ("");

add (message2JLabel);

message3JLabel = new JLabel ("");
add (message3JLabel);

message4JLabel = new JLabel ("");
add (message4JLabel);

 

newGameJButton = new JButton("New Game"); // create "New Game" button

add(newGameJButton); // add newGame button to JFrame

 

Random generator = new Random();

number = generator.nextInt(1001);//create random number

 

   
      newGameJButton.addActionListener(
   
      new ActionListener() // anonymous inner class
   
      {
   
      public void actionPerformed(ActionEvent e)
   
      {
   
      theGame ();
   
      } // end method actionPerformed
   
      } // end anonymous inner class
   
      ); // end call to addActionListener

 

 

theGame(); // start new game

} // end GuessGameFrame constructor

 

// choose a new random number
    public void theGame()
    {
        guessInputJTextField.setText("");
        Random generator = new Random();
        number = generator.nextInt(1001);
        random1.setText("" + number);
        SwingUtilities.updateComponentTreeUI(random1);
        messageJLabel.setText("");
        guessInputJTextField.setEditable(true);
        Guesses = 0;
        message2JLabel.setText("Enter guess # " + Integer.toString(++Guesses));
		  getContentPane().setBackground(Color.LIGHT_GRAY);

    } // end method theGame
 

// change background color

class GuessHandler implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

int Guess;

 

Guess = Integer.parseInt(guessInputJTextField.getText());
SwingUtilities.updateComponentTreeUI(guessInputJTextField);

if (Math.abs(number - Guess) < Math.abs(number - GuessOld))

{

// Hotter

getContentPane().setBackground(Color.RED);
message3JLabel.setText("You're getting warmer!");
SwingUtilities.updateComponentTreeUI(message3JLabel);

}

else
{

// Colder
message4JLabel.setText("You're getting colder!");
SwingUtilities.updateComponentTreeUI(message4JLabel);
getContentPane().setBackground(Color.BLUE);


}

 

GuessOld = Guess;

if (Guess > number)

{

messageJLabel.setText(+ Guess + " is too high.");

SwingUtilities.updateComponentTreeUI(messageJLabel);



}

 

if (Guess < number)

{

messageJLabel.setText(+ Guess + " is too low.");

SwingUtilities.updateComponentTreeUI(messageJLabel);


}

// end if

Guesses++;

message2JLabel.setText("Enter guess #  " + Integer.toString(Guesses)); 

if (Guess == number) // guess is too low

{

messageJLabel.setText("Congratulations!  You guessed my number!");
getContentPane().setBackground(Color.YELLOW);

SwingUtilities.updateComponentTreeUI(messageJLabel);
Guesses++;

message2JLabel.setText("# of guesses = " + Integer.toString(Guesses));

guessInputJTextField.setEditable(false);

}

 


}

}

}

I left off my main() class this time because I haven't changed it. If you'd like to see it, you'll find it further up in this thread.

Ok,

I've gotten the colder and warmer thing finally fixed but I STILL can't figure out how the heck to clear the JTextField after each time that the user hits 'Enter'. Can someone please, at least point me in the right direction of how to make it do that? Here's my most recent code.

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Graphics;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.util.Random;

import javax.swing.JFrame;

import javax.swing.JTextField;

import javax.swing.JLabel;

import javax.swing.JButton;

import javax.swing.SwingUtilities;

 

public class GuessNumber extends JFrame

{

private int Guesses = 0;

private int GuessOld = 0;

private int number; // application's number

private JTextField guessInputJTextField; // user input field

private JLabel prompt1JLabel; // first line of instruction

private JLabel prompt2JLabel; // second line of instructions

private JLabel messageJLabel; // displays message of game status

private JLabel message2JLabel; //display number of guesses

private JLabel message3JLabel;

private JLabel random1 = new JLabel("");

private JButton newGameJButton; // creates new game

private Color background; // background color of application

 

// set up GUI and initialize values

public GuessNumber()

{

super("Guessing Game");

 

setLayout(new FlowLayout());

background = Color.LIGHT_GRAY; // set background to light gray

 

prompt1JLabel = new JLabel("I have a number between 1 and 1000."); // describe game

add(prompt1JLabel);

prompt2JLabel = new JLabel("Can you guess my number? Type your Guess and then press 'Enter':"); // prompt user

add(prompt2JLabel);

 

guessInputJTextField = new JTextField(5); // to enter guesses

guessInputJTextField.addActionListener(new GuessHandler());

add(guessInputJTextField);

 

messageJLabel = new JLabel("");

add(messageJLabel);

 

message2JLabel = new JLabel ("");

add (message2JLabel);

message3JLabel = new JLabel ("");
add (message3JLabel);

 

newGameJButton = new JButton("New Game"); // create "New Game" button

add(newGameJButton); // add newGame button to JFrame

 

Random generator = new Random();

number = generator.nextInt(1001);//create random number

 

   
      newGameJButton.addActionListener(
   
      new ActionListener() // anonymous inner class
   
      {
   
      public void actionPerformed(ActionEvent e)
   
      {
   
      theGame ();
   
      } // end method actionPerformed
   
      } // end anonymous inner class
   
      ); // end call to addActionListener

 

 

theGame(); // start new game

} // end GuessGameFrame constructor

 

// choose a new random number
    public void theGame()
    {
        guessInputJTextField.setText("");
        Random generator = new Random();
        number = generator.nextInt(1001);
        random1.setText("" + number);
        SwingUtilities.updateComponentTreeUI(random1);
        messageJLabel.setText("");
        guessInputJTextField.setEditable(true);
		  message2JLabel.setText("");
        Guesses = 0;
        message3JLabel.setText("Enter guess # " + Integer.toString(++Guesses));
		  getContentPane().setBackground(Color.LIGHT_GRAY);

    } // end method theGame
 

// change background color

class GuessHandler implements ActionListener

{

public void actionPerformed(ActionEvent e)

{

int Guess;

 

Guess = Integer.parseInt(guessInputJTextField.getText());
SwingUtilities.updateComponentTreeUI(guessInputJTextField);

if (Math.abs(number - Guess) < Math.abs(number - GuessOld))

{

// Hotter

getContentPane().setBackground(Color.RED);
message2JLabel.setText("You're getting warmer!");
SwingUtilities.updateComponentTreeUI(message2JLabel);

}

if (Math.abs(number - Guess) > Math.abs(number - GuessOld))
{

// Colder
message2JLabel.setText("You're getting colder!");
SwingUtilities.updateComponentTreeUI(message2JLabel);
getContentPane().setBackground(Color.BLUE);


}

 

GuessOld = Guess;

if (Guess > number)

{

messageJLabel.setText(+ Guess + " is too high.");

SwingUtilities.updateComponentTreeUI(messageJLabel);



}

 

if (Guess < number)

{

messageJLabel.setText(+ Guess + " is too low.");

SwingUtilities.updateComponentTreeUI(messageJLabel);


}

// end if

Guesses++;

message3JLabel.setText("Enter guess #  " + Integer.toString(Guesses)); 

if (Guess == number) 

{

messageJLabel.setText("Congratulations!  You guessed my number!");
getContentPane().setBackground(Color.YELLOW);
message2JLabel.setText("");

SwingUtilities.updateComponentTreeUI(messageJLabel);
Guesses++;

message3JLabel.setText("# of guesses = " + Integer.toString(Guesses));
SwingUtilities.updateComponentTreeUI(message3JLabel);
guessInputJTextField.setEditable(false);

}

 


}

}

}
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.