Guessing Game reset

Trying to reset game without closing application.

Page 1 of 1

7 Replies - 4077 Views - Last Post: 05 February 2010 - 07:45 PM Rate Topic: -----

#1 Moogoo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 82
  • Joined: 06-January 10

Guessing Game reset

Posted 05 February 2010 - 06:39 PM

I have this program, but I'm not sure how to clear a JTextField after it has been used to read input from a user. Here's my program:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GuessGame extends JFrame 
{
	
	private static final long serialVersionUID = 7621432443622124269L;
	public static double random = Math.random()*1000;
	public static long number = Math.round(random);
	public static String triedNum;
	public static int tested;
	public static JFrame guessFrame;
	public JLabel starter;
	public JLabel userPrompt;
	public static JTextField input;
	public static JLabel correct;
	public JButton guesser;
	public JButton startOver;
	public Container c;

	public GuessGame()
	{
		guessFrame = new JFrame("Guessing Game!");
		starter = new JLabel("I have a number between 1 and 1000.  Can you guess what it is?");
		userPrompt = new JLabel("Please enter your guess.");
		input = new JTextField(5);
		guesser = new JButton("Guess");
		correct = new JLabel("");
		
		startOver = new JButton("Play again");

		c = guessFrame.getContentPane();
		c.setLayout(new FlowLayout());

		c.add(starter);
		c.add(userPrompt);
		c.add(input);
		c.add(guesser);
		c.add(correct);
		c.add(startOver);

		guessFrame.setSize(250,250);

		guessFrame.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e) {System.exit(0);}
		});

		
		guesserButtonHandler ghandler = new guesserButtonHandler();
		guesser.addActionListener(ghandler);
		
		startOverButtonHandler shandler = new startOverButtonHandler();
		startOver.addActionListener(shandler);

		guessFrame.show();
	}
	
	public static void setOutput(String display)
	{
		correct.setText(display);
	}

	static class guesserButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
					
			System.out.println("in guesserButtonHandler");
			
			triedNum = input.getText();
			tested = Integer.parseInt(triedNum);
			if (tested > number)
			{
				setOutput("Too High");
				guessFrame.setForeground(Color.RED);
			}
			if (tested < number)
			{
				setOutput("Too low");
			}
			else if (tested == number)
			{
				setOutput("Right on!");
			}
		}
	}
	static class startOverButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			setOutput("");
			
		}
	}
	

	public static void main(String[] args)
	{
		new GuessGame();
	}
}


I want the Start Over button to reset the JLabel, which it does right now, clear the JTextField, and generate a new random number. Could anyone give me a hand with the algorithm?

Is This A Good Question/Topic? 0
  • +

Replies To: Guessing Game reset

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Guessing Game reset

Posted 05 February 2010 - 06:45 PM

It seems as though you have successfully cleared the JLabel, and the idea is the same the JTextArea. The setText() method allows you to enter any String, and like your label, you want to set it to blank, "".

So I would do something like this.

        public static void setOutput(String display)
        {
                correct.setText(display);
        }

        public static void setTextArea(String display)
        {
                input.setText(display);
        }

        static class startOverButtonHandler implements ActionListener
        {
                public void actionPerformed(ActionEvent e)
                {
                        setOutput("");
                        setTextArea("");
                }
        }



Now it sets the input to blank as well.
Was This Post Helpful? 1
  • +
  • -

#3 Moogoo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 82
  • Joined: 06-January 10

Re: Guessing Game reset

Posted 05 February 2010 - 07:04 PM

Awesome! DogStopper, you have solved another one of my problems. Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Guessing Game reset

Posted 05 February 2010 - 07:12 PM

Glad to have helped! :)
Was This Post Helpful? 0
  • +
  • -

#5 Moogoo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 10
  • View blog
  • Posts: 82
  • Joined: 06-January 10

Re: Guessing Game reset

Posted 05 February 2010 - 07:13 PM

Do I reset the content pane to get a new number also? with a new guessFrame(); statement in the startOverButtonHandler as well?
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Guessing Game reset

Posted 05 February 2010 - 07:18 PM

What might be better is to make a static method that generates a new number.
// Set this later
public static double random;

...
public static void genRand() {
     random = Math.random()*1000;
}



Now, whenever you need a random number, call this method. It also needs to be called by the constructor.
Was This Post Helpful? 0
  • +
  • -

#7 honey_yhaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 23-February 09

Re: Guessing Game reset

Posted 05 February 2010 - 07:34 PM

just wanna try...

static class startOverButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			input.setText("");
		}
	}



and then try to put

guessFrame.setBounds(350,250,250,250);


for center window pops!

good day!
Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2874
  • View blog
  • Posts: 11,047
  • Joined: 15-July 08

Re: Guessing Game reset

Posted 05 February 2010 - 07:45 PM

It isn't center for me...the 250, 250 on my screen is actually further to left than center.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1