I have a button called 'Play Again!' that I want to hide and then show when the user wins or loses and wants to play again.
This is my function:
public void checkGuess() {
String guessText = txtGuess.getText();
txtGuess.setText("");// Empties the contents of the text field.
String message = "";
try {
int guess = Integer.parseInt(guessText);
if (numberOfTries == 0) {
message = "You Lost! A new game has begun and you have 8 guesses remaining.";
}
else if (guess < theNumber) {
message = guess + " is too low. Try again. You have " + numberOfTries + " tries left!";
}
else if (guess > theNumber) {
message = guess + " is too high. Try again. You have " + numberOfTries + " tries left!";
}
else {
message = guess + " is correct. Let's play again!";
}
} catch (Exception e) {
message = "Enter a whole number between 1 and 100.";
} finally {
lblOutput.setText(message);
txtGuess.requestFocus();
txtGuess.selectAll();
}
decrementNumberOfTries();
}
If I show the button when it's created the button is visible. This is only a demo I want to hide it when the program starts, by adding the lines:
JButton btnPlayAgain = new JButton("Play Again");
btnPlayAgain.setVisible(true);
Ordinarily it would start out as hidden:
JButton btnPlayAgain = new JButton("Play Again");
btnPlayAgain.setVisible(false);
This is how the window should look when the button is hidden:
If I set the button visible where I want it in my function, the button does not become visible and the other messages (such as "You lost...") don't appear on the screen at all :
public class GuessingGame extends JFrame {
private static final long serialVersionUID = 1L;
private JLabel lblOutput;
private int theNumber;
private JTextField txtGuess;
private int numberOfTries = 8;
private JButton btnPlayAgain;
public void checkGuess() {
String guessText = txtGuess.getText();
txtGuess.setText("");// Empties the contents of the text field.
String message = "";
try {
int guess = Integer.parseInt(guessText);
if (numberOfTries == 0) {
message = "You Lost! A new game has begun and you have 8 guesses remaining.";
btnPlayAgain.setVisible(false);
}
else if (guess < theNumber) {
message = guess + " is too low. Try again. You have " + numberOfTries + " tries left!";
}
else if (guess > theNumber) {
message = guess + " is too high. Try again. You have " + numberOfTries + " tries left!";
}
else {
message = guess + " is correct. Let's play again!";
btnPlayAgain.setVisible(false);
}
} catch (Exception e) {
message = "Enter a whole number between 1 and 100.";
} finally {
lblOutput.setText(message);
txtGuess.requestFocus();
txtGuess.selectAll();
}
decrementNumberOfTries();
}
I'm declaring the button at the top of my class:
private JButton btnPlayAgain;
These are my decermentNumberOfTries()
and newGame()
methods:
public void newGame() {
numberOfTries = 8;
theNumber = (int) (Math.random() * 100 + 1);
}
public void decrementNumberOfTries() {
--numberOfTries;
}
And this is the function that creates the window:
public GuessingGame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Tim's Hi-Lo Guessing Game");
getContentPane().setLayout(null);
JLabel lblTitle = new JLabel("Tim's Hi-Lo Guessing Game");
lblTitle.setFont(new Font("Tahoma", Font.BOLD, 15));
lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
lblTitle.setBounds(-10, 15, 436, 32);
getContentPane().add(lblTitle);
JLabel lblGuessANumber = new JLabel("Guess a number between 1 and 100:");
lblGuessANumber.setBackground(new Color(240, 240, 240));
lblGuessANumber.setHorizontalAlignment(SwingConstants.RIGHT);
lblGuessANumber.setBounds(83, 57, 215, 13);
getContentPane().add(lblGuessANumber);
txtGuess = new JTextField();
txtGuess.addActionListener((ActionEvent e) -> {
checkGuess();
});
txtGuess.setHorizontalAlignment(SwingConstants.RIGHT);
txtGuess.setBounds(239, 85, 27, 19);
getContentPane().add(txtGuess);
txtGuess.setColumns(10);
JButton btnGuess = new JButton("Guess!");
btnGuess.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
checkGuess();
}
});
btnGuess.setBounds(146, 84, 85, 21);
getContentPane().add(btnGuess);
lblOutput = new JLabel("Enter a number above and click Guess!");
lblOutput.setHorizontalAlignment(SwingConstants.CENTER);
lblOutput.setBounds(-40, 152, 500, 13);
getContentPane().add(lblOutput);
JButton btnPlayAgain = new JButton("Play Again");
btnPlayAgain.setVisible(false);
btnPlayAgain.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
newGame();
}
});
btnPlayAgain.setBounds(146, 115, 132, 21);
getContentPane().add(btnPlayAgain);
}
My questions are, why does the button not become visible when I set visible to true?
If I try to set visible in an if statement like this:
if (numberOfTries == 0) {
message = "You Lost! A new game has begun and you have 8 guesses remaining.";
btnPlayAgain.setVisible(false);
}
Why does the message
not appear?
How can I set the button to visible correctly?
This is all my code, in case that helps: full code