猜游戏-JButton doClick无法正常工作?

时间:2020-03-23 15:43:56

标签: java swing jframe jbutton

我正在尝试选择何时输赢游戏,询问用户是否愿意再次玩。如果是,则doClick功能应单击重新启动按钮并重新启动游戏,但这不起作用吗?

                if (GuessNumber == theNumber) {
                    statusLabel.setText("Winner! Guess is correct.");
                    statusLabel.setForeground(Color.green);
                    guessTF.setEditable(false);
                    guessTF.setBackground(Color.green);
                    int newGame = JOptionPane.showConfirmDialog(null, "Winner! Congratulations you have won."
                            + "\nYou guessed " +theNumber
                            + "\nWould you like to play again?" , "Guessing Game", JOptionPane.YES_NO_OPTION);
                    if(newGame == JOptionPane.YES_OPTION)
                    {
                        newGameButton.doClick();
                    }
                    else 
                    {
                        int no = JOptionPane.showConfirmDialog(null, "Are you sure you want to quit Guessing Game?", "Guessing Game", JOptionPane.YES_NO_OPTION);
             if(no == JOptionPane.YES_OPTION)
            {
            System.exit(0);
            }
                    }
                }

                if (maxAttempt == 10 && GuessNumber != theNumber) {
                    statusLabel.setText("Game Over. You have exceeded your guessing limit.");
                    statusLabel.setForeground(Color.red);
                    guessTF.setEditable(false);
                    guessTF.setBackground(Color.red);
                    int gameNew = JOptionPane.showConfirmDialog(null, "Game over.\n The guessing limit has been exceeded :("
                            + "\nThe number was: " +theNumber
                            + "\nWould you like to play again?" , "Guessing Game", JOptionPane.YES_NO_OPTION);
                    if(gameNew == JOptionPane.YES_OPTION)
                    {
                        newGameButton.doClick();
                    }
                    else 
                    {
                        int no = JOptionPane.showConfirmDialog(null, "Are you sure you want to quit Guessing Game?", "Guessing Game", JOptionPane.YES_NO_OPTION);
             if(no == JOptionPane.YES_OPTION)
            {
            System.exit(0);
            }
                    }
                }


            }

0 个答案:

没有答案