Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Need a little insight as to why this does not work (My code has issues)
Need a little insight as to why this does not work [message #933049] Thu, 04 October 2012 17:33 Go to next message
Ronda Wilson is currently offline Ronda WilsonFriend
Messages: 1
Registered: October 2012
Junior Member

Hi guys I am having a problem with my Java code. I was hoping maybe someone here could help me out with it. I am totally new to programming with java so don't be too harsh.
This is supposed to be a guessing game where the user guesses the number. Here are the specifics:
Your program chooses the number to be guessed by selecting an integer at random in the range 11000. 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 guess: ____________________

A JTextField should be used to input the guess. A JButton must be provided to allow the user to press each time a guess is entered. As each guess is input, the background color should change to either red or blue. Red indicates that the user is getting warmer, and blue indicates that the user is getting colder. A JLabel should display either Too High or Too Low to help the user zero in on the correct answer. When the user gets the correct answer, Correct! should be displayed, and the JTextField used for input should be cleared and changed to be uneditable. Also, a JButton should be provided to allow the user to play the game again. When the New Game JButton is clicked, a new random number should be generated and the input JTextField changed to be editable. A JButton must be provided to allow the user to exit the application. Also provide a count of the number of guesses the user entered when the correct number is guessed.

This is the code I have put together:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class GuessGame extends JFrame
	private static JFrame mainFrame;
	private static JButton tryButton;
	private static JButton newgameButton;
	private static JButton exitButton;
	private static JTextField guessField;
	private static JLabel guessLabel;
	private static JLabel headerLabel;
	private static JTextField results;
	private static JPanel panel;
	private static int randomNumber;
	public GuessGame() 
		mainFrame = new JFrame ("Guessing Game");
		tryButton = new JButton ("Try the number");
		exitButton = new JButton ("Exit");
		newgameButton = new JButton ("New Game");
		guessLabel = new JLabel ("Enter your guess:");
		headerLabel = new JLabel(
				"<html>I have a number between 1 and 1000,<br>can you guess the number?</html>");
		guessField = new JTextField(5);
		results = new JTextField(25);
		panel = new JPanel();

		mainFrame.addWindowListener(new WindowAdapter() 
			public void windowClosing(WindowEvent e) 

		TryButtonHandler tbHandler = new TryButtonHandler();

		ExitButtonHandler eHandler = new ExitButtonHandler();

		NewGameButtonHandler ngHandler = new NewGameButtonHandler();


		results.setText("Too high/Too low message will go here!");

		Container c = mainFrame.getContentPane();
		c.setLayout(new BorderLayout());
		panel.setLayout(new FlowLayout(FlowLayout.CENTER));


		mainFrame.setSize(325, 210);

	public class TryButtonHandler implements ActionListener
		public void actionPerformed(ActionEvent arg0)
			int userInput=0;
			int diff;
			int Difference;
				userInput = Integer.parseInt(
			catch (Exception ex)
				results.setText("Enter a VALID number!");
			if (userInput == randomNumber){
				JOptionPane.showMessageDialog(null, "CONGRATULATIONS! You got it!!",
						"Random Number: " + randomNumber, JOptionPane.INFORMATION_MESSAGE);
				randomNumber = new Random().nextInt(1000) + 1;
			if (userInput > randomNumber){
				results.setText( "Too High. Try a lower number." );
				diff=userInput - randomNumber;
				results.setText( "Too Low. Try a higher number." );
				diff=randomNumber - userInput;
				results.setText("You're getting colder!");

				results.setText("You're getting warmer!");


	static class ExitButtonHandler implements ActionListener
		public void actionPerformed(ActionEvent arg0)



	public class NewGameButtonHandler implements ActionListener
		public void actionPerformed(ActionEvent arg0)
			SwingUtilities.invokeLater(new Runnable() 
				public void run() 
					GuessGame GuessNumber = new GuessGame();


	private void setBackgroundColor(Color color)
	public static void main(String[] args)
		new GuessGame();

I don't get any errors but it's not behaving right. For instance I don't believe the random number is generating and the colors of the panel don't change when they are supposed to.

I really appreciate any help I can get on this.
Thank you

I am not awesome because I have never failed...
I am awesome because failure has never stopped me.
Re: Need a little insight as to why this does not work [message #934974 is a reply to message #933049] Sat, 06 October 2012 14:28 Go to previous message
Bernard Sarter is currently offline Bernard SarterFriend
Messages: 88
Registered: August 2011
Location: Paris, France

Not sure that your posting in the right forum ... having said that:

1) your random number is never set, add eg.
randomNumber = (int) (Math.random() * 1000d) in your constructor and newGame stuff, and remove the 'static' statement.

2) You have to precise the object on which you want to change the background colour: eg panel.setBackground( or tryButton.setBackground( or ...

Hope that helps,
Previous Topic:issue - print messages in eclipse console
Next Topic:SubClipse Question
Goto Forum:

Current Time: Fri Nov 27 18:46:09 GMT 2020

Powered by FUDForum. Page generated in 0.02008 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top