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();
}
}

New Topic/Question
Reply




MultiQuote




|