简体   繁体   English

JAVA - 无法在 Java Swing 中隐藏按钮和文本字段

[英]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).我有一个游戏,用户试图猜测 1 到 10 之间的数字(或者 1 到 100,无论你设置什么)。

I show a button called btnPlayAgain when the user either wins or loses asking them if they want to play again.当用户赢或输时,我会显示一个名为btnPlayAgain的按钮,询问他们是否想再玩一次。 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.btnPlayAgain按钮不可见时,我想设置允许您猜测的按钮( btnGuess )。 But for some reason that's not happening.但由于某种原因,这并没有发生。 the btnPlayAgain button is still visible when the user sees the play again button.当用户看到再次播放按钮时, btnPlayAgain按钮仍然可见。

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.当用户猜对时,按钮btnGuess不会被隐藏。 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:这是我玩游戏的 function 及其支持功能:

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:这是 window 在您玩游戏时的样子:

在此处输入图像描述

I want to hide the Guess!我想隐藏Guess! button and text field and show the playAgain button.按钮和文本字段并显示playAgain按钮。 The playAgain button should allow the user to start a new game. playAgain按钮应该允许用户开始新游戏。 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.问题是您在构造函数btnGuess成为局部变量而不是设置字段,因此当数字正确时会引发NullPointerException Since you are catching Exception , the program assumes that an invalid number was entered.由于您正在捕获Exception ,因此程序假定输入了无效数字。 Catch NumberFormatException instead to avoid missing errors in the program.而是捕获NumberFormatException以避免在程序中丢失错误。

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.要正确重置游戏,您需要修改newGame方法以仅显示适当的元素并重置 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!");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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