I made this program but it isn't working; any suggestions?

/*
 *  Program Name : GuessGame.java
 *        Author : James Resue
 *          Date : October 6, 2010
 *  Purpose:  Write a program GuessGame.java that plays the game “guess the number” as follows: 
 *  Your program chooses the number to be guessed by selecting an integer at random in the range 1–1000. 
 *  The program then displays the following in a label: 
 *  I have a number between 1 and 1000 can you guess my number?
 *  Please enter your first guess.
*/
import javax.swing.*;
import java.awt.event.*;
import java.awt.*; //used for layout manager
import java.util.Random;;
public class GuessGame extends JFrame
{   
    private JButton newGameButton;
    private JButton enterButton;
    private JButton exitButton;
    private JTextField guessBox;
    private JLabel initialTextLabel;
    private JLabel enterLabel;
    private JLabel userMessageLabel;
    private int randomNumber;
    private int userGuess;
    private int counter = 0;
    private int lastGuess = 0;


    public GuessGame()
    {
        super("Guessing Game");
        newGameButton = new JButton("New Game");
        exitButton = new JButton("Exit Game");
        enterButton = new JButton("Enter");
        guessBox = new JTextField(4);
        initialTextLabel = new JLabel("I have a number between 1 and 1000 can you guess my number?");
        enterLabel = new JLabel("Please enter your first guess.");
        userMessageLabel = new JLabel("");
        randomNumber = new Random().nextInt(1000) + 1;
        setBackground(Color.ORANGE);

        //set the layout manager
        setLayout(new FlowLayout());

        //add components
        add(initialTextLabel);
        add(enterLabel);
        add(guessBox);
        add(newGameButton);
        add(enterButton);
        add(exitButton);

        //create accelerator keys
        newGameButton.setMnemonic('N');
        exitButton.setMnemonic('x');
        enterButton.setMnemonic('E');

        //set default frame size
        setSize(500, 125);


        //define and register windows event handler
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });

        //create and register the button event handlers
        newGameButtonHandler nghandler = new newGameButtonHandler();
        newGameButton.addActionListener(nghandler); //register the value

        ExitButtonHandler exithandler = new ExitButtonHandler();
        exitButton.addActionListener(exithandler);

        enterButtonHandler enterhandler = new enterButtonHandler();
        enterButton.addActionListener(enterhandler);

    }//end of GuessGame constructor
    //newGameButtonHandler class
    class newGameButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            setBackground(Color.ORANGE);
            userMessageLabel.setText("");
            randomNumber = new Random().nextInt(1000) + 1;
        }
    }//end of inner class
    class ExitButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            System.exit(0);
        }
    }//end of inner class
    class enterButtonHandler implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            userGuess = Integer.parseInt(guessBox.getText());
            compareGuess(userGuess, randomNumber);
        }
    }
    public void compareGuess(int userGuess, int randomNumber)
    {

        counter++;

        if (userGuess == randomNumber)
            {
                userMessageLabel.setText("You are correct, it took you: " + counter + "guess(es)");
                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.GRAY);
                }
        }//end if
        lastGuess = userGuess;

    }//end compareGuess()

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

Recommended Answers

All 6 Replies

Your forgot to add the component userMessageLabel into container:

add(userMessageLabel); // this line of code is missing

Please use CODE tag to post your code so that one may refer your certain code to a specific line number

Thanks tong1, that was a bonehead mistake I made. I also added getContentPane() before the .setBackground in all places to enable the background to change.

jtresue and tong i just want to ask about the userMessageLabel. What does it do in the program?

Is it somewhat like a notification for the user?

userMessageLabel.setText("You are correct, it took you: " + counter + "guess(es)");

userMessageLabel.setText("Too high");

userMessageLabel.setText("Too Low");

read the code, you'll see. anyway, this is an ancient <solved> thread, there's no real reason to revive it.

Is it somewhat like a notification for the user?

That's exactly what it is.

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.