Guess Example Utilizing JButton, JTextField, and JLabels
I'm also totally stuck on a guess assignment. I have the flow diagram done which is pretty easy, but to place in java code with an action happening with input of JTextField.
Here is what we are suppose to do: Application that plays "Guess the number" with an integer in the range of 1-1000. A label needs to state the following what is in my program, a JTextField is for the input guess. As each guess is input, the background color should change to red if "warmer" and blue if "cooler". Another JLabel is suppose to state too high or too low or correct. After a correct a Play again button if pressed should loop and give a new random # and start the process again.
My code just seems to hang...
/* Name: Julie JubinDate: July 14, 2007
* Chapter 11.15
*/
package Dialogs;
import java.awt.*;//import java awt
import java.awt.event.*;//import events
import javax.swing.*;//import java swing
import java.util.Random;//import random
//class to create a random number and guess the number
publicclass GuessGameextends JFrame
{
//declare variables
int guess;//store guess integer
int num;//stores number of random choice
int rpt;//stores play again option
private JLabel lOutput;//stores label message for guess
private JLabel lMessage;//stores label for output of Too Low/Too High
private JTextField gText;//input of guess
private JButton okButton;//ok button
private JButton cancelButton;//cancel button
private JButton playAgain;//play again button
//create fields, button for form frame
public GuessGame()
{
super("GuessGame");//call super
//null layout requires programmer to set sizes
setLayout(null);
//label to enter for output
lOutput =new JLabel("I have a number between 1-1000. Can you guess my number? Please enter your first number.");
lOutput.setSize(180,20);
lOutput.setLocation(12, 12);
add(lOutput);
//label to hold output message too low/too high
lMessage =new JLabel("");//creates instance of label
lMessage.setSize(200,20);//sets size
lMessage.setLocation(12, 50);//sets position
add(lMessage);//adds label field
//set up ok button
okButton =new JButton("OK");
okButton.setSize(90,30);//sets size of OK button
okButton.setLocation(12,150);//sets location
add(okButton);//adds ok button
//set up cancel button
cancelButton =new JButton("Cancel");
cancelButton.setSize (90,30);
cancelButton.setLocation(200,150);
add(cancelButton);
cancelButton.addActionListener
(
new ActionListener()
{
publicvoid actionPerformed(ActionEvent event)
{
//close by calling dispose method
GuessGame.this.dispose();
}//end actionPerformed cancel method
}//end actionListener
);//end AddActionListener
//set up play again button
playAgain =new JButton("Play Again");
playAgain.setSize (90,30);
playAgain.setLocation(300,150);
add(playAgain);
playAgain.setVisible(false);
playAgain.addActionListener
(
new ActionListener()
{
publicvoid actionPerformed(ActionEvent event)
{
//repeat option and set back to defaults
rpt = 1;
playAgain.setEnabled(false);//disable button
gText.setEditable(true);
lOutput.setText("");
//lMessage.setBackground(Color.TRANSLUCENT); //set back to default
}//end actionPerformed cancel method
}//end actionListener
);//end AddActionListener
//create and generate random number
Random rNum =new Random();
num = 1 + rNum.nextInt(1000);
//text box for guess
gText =new JTextField(15);
gText.setSize (40,20);//sets size
gText.setLocation(190, 12);//sets location
add(gText);//adds text field
gText.setEditable(true);//set text field for guess as editable
do
{
TextFieldHandler handler =new TextFieldHandler();
gText.addActionListener(handler);
}while (guess !=num);//end of do loop
}//end of public guessgame
privateclass TextFieldHandlerimplements ActionListener
{
publicvoid actionPerformed(ActionEvent event)
{
if (guess > 1 && guess < 1000)
{
if (guess < num)
{
//set background to red and display
lMessage.setBackground(Color.RED);
lMessage.setText("Too Low");
}//end of if guess < num
if (guess > num)
{
//set background to cold and display
lMessage.setBackground(Color.BLUE);
lMessage.setText("Too High");
}//end of if guess > num
if (guess == num)
{
//guess is correct
lMessage.setBackground(Color.WHITE);
lMessage.setText("Correct!");
gText.setEditable(false);
playAgain.setVisible(true);
//create new play again button
}//end of if Guess = num
}//end of if guess within bounds
}//end of actionperformed
}//end of class TextFieldHandler
}//end of class GuessGame
*****************************************************************************
/* Name: Julie JubinDate: July 13, 2007
* Chapter 11.15 Test
*/
package Dialogs;
import javax.swing.*;//import javax swing
//Guessing Game Test
publicclass GuessGameTest
{
publicstaticvoid main(String[] args)
{
//start new instance of Guessing Game class
GuessGame guessGame =new GuessGame();
guessGame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
guessGame.setSize(400,300);
guessGame.setVisible(true);
}//end of main
}//end of GuessGame class
Any assistance to point to me what I'm missing would be great. Thanks.

