简体   繁体   中英

JAVA - Can't hide button and text field in Java Swing

I have a game where the user tries to guess a number between 1 and 10 (or 1 and 100 whatever you set it to be).

I show a button called btnPlayAgain when the user either wins or loses asking them if they want to play again. I want to hide that button when the program starts and show it when they win or lose.

I want to set the button that allows you to guess ( btnGuess ) when the btnPlayAgain button to be invisible. But for some reason that's not happening. the btnPlayAgain button is still visible when the user sees the play again button.

If I try:

        else {
            btnGuess.setVisible(false);
            txtGuess.setVisible(false);
            message = guess + " is correct. Let's play again!";
            btnPlayAgain.setVisible(true);
        }

The button btnGuess does not get hidden when the user guesses right. And the "guess is correct. Let's play again." message does not get printed to the screen.

This is my function that plays the game and it's supporting functions:

public void checkGuess() {
    btnPlayAgain.setVisible(false);
    String guessText = txtGuess.getText();
    txtGuess.setText("");// Empties the contents of the text field.
    String message = "";
    try {
        int guess = Integer.parseInt(guessText);
        if (numberOfTries == 0) {
            btnGuess.setVisible(false);
            txtGuess.setVisible(false);
            message = "You Lost! A new game has begun and you have 8 guesses remaining.";
            btnPlayAgain.setVisible(true);
        }
        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 {
            btnGuess.setVisible(false);
            txtGuess.setVisible(false);
            message = guess + " is correct. Let's play again!";
            btnPlayAgain.setVisible(true);
        }
    } catch (Exception e) {
        message = "Enter a whole number between 1 and 10.";
    } finally {
        lblOutput.setText(message);
        txtGuess.requestFocus();
        txtGuess.selectAll();
        
    }
    decrementNumberOfTries();
}

public void newGame() {
    numberOfTries = 8;
    theNumber = (int) (Math.random() * 10 + 1);
}

public void decrementNumberOfTries() {
    --numberOfTries;        
}

And here is a link to all my code .

This is what the window looks like where you play the game:

在此处输入图像描述

I want to hide the Guess! button and text field and show the playAgain button. The playAgain button should allow the user to start a new game. How can I do that?

The issue is that you made btnGuess a local variable in the constructor instead of setting the field, so a NullPointerException is thrown when the number is correct. Since you are catching Exception , the program assumes that an invalid number was entered. Catch NumberFormatException instead to avoid missing errors in the program.

In the constructor, change

JButton btnGuess = new JButton("Guess!");

To

this.btnGuess = new JButton("Guess!");

To reset the game properly, you need to modify the newGame method to show only the appropriate elements and reset the text of the output label.

public void newGame() {
    numberOfTries = 8;
    theNumber = (int) (Math.random() * 10 + 1);
    btnGuess.setVisible(true);
    txtGuess.setVisible(true);
    btnPlayAgain.setVisible(false);
    lblOutput.setText("Enter a number above and click Guess!");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM