GUI window removal help

killing one GUI window while opening another?

Page 1 of 1

2 Replies - 627 Views - Last Post: 27 October 2009 - 10:54 AM Rate Topic: -----

#1 noctolater  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 72
  • Joined: 29-April 09

GUI window removal help

Posted 27 October 2009 - 10:33 AM

I have written a number guessing game where the user tells the computer the highest number and the computer creates a random number in the range. For example, if the user picks 100, the computer creates a random number from 1 to 100. The issue I am having is more GUI than anything else. I have a window that displays the range you're guessing in, the range that you haven't checked yet, and let's you input a guess. After you win, it tells you the number and how many tries it took.

What I need is to display the JLabel that asks the user if they want to play again and the two buttons "Yes" and "No". The two ways I see of doing this are to either clear the JFrame and add the JLabel and JButtons, or to make the JFrame invisible, dispose of it, and create a new JFrame with the three objects. I have tried searching on this forum and the Java API, but I am having no luck getting anything accomplished.

Thanks for any assistance you can give me.

import javax.swing.*;
import java.awt.event.*;
import java.awt.FlowLayout;
import java.util.Random;

/**
 * Class GUI is a GUI representation of the classic number guessing game.
 * 
 * @author Sam Lanzo
 * @version 0.1 (10/22/2009)
 */
public class GUI extends JFrame
{
	int lowNumber = 0;
	int highNumber = Integer.parseInt(JOptionPane.showInputDialog (null, "<html>Let's play a number guessing game!<P>" + 
																		 "What should the highest number be?"));
	int numGuesses = 0;
	
	JLabel guessingGame = new JLabel ("Guess a number between 1 and " + highNumber + ".");
	JLabel numberRange = new JLabel ("The number is higher than " + 
									 lowNumber + ", but less than " + 
									 (highNumber + 1) + ".");
	JTextField guess = new JTextField ("", 20);
	JButton guessButton = new JButton ("Guess");
	JLabel correctness = new JLabel ("");
	
	JLabel rightAnswer = new JLabel ("");
	
	JButton yes = new JButton ("Yes");
	JButton no = new JButton ("No");
	
	/**
	 * constructor for objects of class GUI
	 */
	public GUI ()
	{
		Random randomGenerator = new Random();
		final int RANDOM = randomGenerator.nextInt (highNumber) + 1;
		// the +1 is because it starts at 0 and goes to highNumber exclusive
		
		this.setVisible(true);
		this.setBounds (200,150,400,150);
		this.setLayout (new FlowLayout(FlowLayout.CENTER));
		this.add(this.rightAnswer);
		this.add(this.guessingGame);
		this.add(this.numberRange);
		this.add(this.guess);
		this.add(this.guessButton);
		this.add(this.correctness);
		this.guessButton.addActionListener (new ActionListener()
		{
			/**
			 * respond to a button press.
			 * 
			 * @param theEvent and ActionEvent
			 */
			public void actionPerformed (ActionEvent theEvent)
			{
				int userGuess = Integer.parseInt(guess.getText());
				
				if (userGuess != RANDOM)
				{
					if (userGuess < RANDOM)
					{
						lowNumber = userGuess;
						numGuesses++;
						correctness.setText(userGuess + " was too low, try again.");
					}
					else // if (guess >0)
					{
						highNumber = userGuess;
						numGuesses++;
						correctness.setText(userGuess + " was too high, try again.");
					}
					numberRange.setText("The number is higher than " + 
									 lowNumber + ", but less than " + 
									 highNumber + ".");
					guess.setText (null);
				}
				else // if (userGuess == RANDOM)
				{
					
					/**
					 * this is where I want it to remove everything and make a new window.
					 */
					guessingGame.setText("");
					numberRange.setText("");
					guess.setText("");
					correctness.setText("");
					rightAnswer.setText("<html> You are right!<P>" + 
										"The number was " + RANDOM + ".<P>" + 
										"It took you " + numGuesses + " attempts.<P> " +
										"Would you like to play again?");
				}
			}
		});
	}
	
	public static void main (String[]args)
	{
		new GUI();
	}
		
}



Is This A Good Question/Topic? 0
  • +

Replies To: GUI window removal help

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: GUI window removal help

Posted 27 October 2009 - 10:35 AM

Option One:

Create a window when the game initialized and make it hidden, then to switch:

currentWindowOpen.setVisible(false);
otherWindowYouWantToOpen.setVisible(true);



Option Two:

It's just a small window with yes/no? Why not use a JOptionPane?
Was This Post Helpful? 0
  • +
  • -

#3 noctolater  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 72
  • Joined: 29-April 09

Re: GUI window removal help

Posted 27 October 2009 - 10:54 AM

You can set the button name in JOptionPane, and make more than one button? I could just make it take in a string answer and use that, but then I still have the problem of making the first window invisible. I am not entirely sure how to call setVisible(false) on the window that already exists.

Edit:

never mind, I don't have to call setVisible(false) on any object since it's already inside the GUI framework... I am so stupid :P

This post has been edited by noctolater: 27 October 2009 - 11:33 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1