我正在尝试选择何时输赢游戏,询问用户是否愿意再次玩。如果是,则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);
}
}
}
}