当我点击no或取消选项时,为什么thisJOptionPane程序没有关闭?

时间:2015-04-06 00:21:42

标签: java swing loops exception joptionpane

在此程序的第一个对话框中,单击" yes"," no"或者"取消"都做同样的事情。如果用户点击" no"如何关闭程序?或者"取消"?我可以使用一些异常处理吗?或者可能是另一个循环?

package dice;
import java.awt.Component;
import javax.swing.JOptionPane;

public class DialogBox 
{
public static void main(String[] args) throws IllegalStateException
{
    int again = 0, money, guess, bet = 0;
    String message, message2, message3, guessStr, betStr, resultStr;
    Account account = new Account(100);

    IllegalStateException problem = new IllegalStateException("You're out of cash!");

    do
    {
     money = account.getCashValue();
     message = "You have $" + money + ". Would you like to bet?";
     JOptionPane.showConfirmDialog(null, message);
     betStr = JOptionPane.showInputDialog("How much would you like to bet?($): ");
     bet = Integer.parseInt(betStr);

     guessStr = JOptionPane.showInputDialog("Guess the sum of two die, then press OK to roll the die.");
     guess = Integer.parseInt(guessStr);

     Dice dice1 = new Dice(6);
     Dice dice2 = new Dice(6);

     dice1.setValue();
     dice2.setValue();

     int diceSum = (dice1.getValue() + dice2.getValue());
     boolean win = Account.winBet(guess, diceSum);

     if (win == true) 
        resultStr = "You win! \n";
     else
        resultStr = "You Lose! \n";

     message2 = resultStr + "first die: " + dice1.getValue() + "\nsecond die: " + dice2.getValue() + "\nsum of die: " + diceSum;
     JOptionPane.showMessageDialog(null, message2);

     account.newBalance(win, bet);
     money = account.getCashValue();

     if (money <= 0)
         throw problem;


    }
    while (again == JOptionPane.showConfirmDialog(null, message));
}

}

1 个答案:

答案 0 :(得分:1)

您没有将确认对话框的结果分配给任何内容。

message3 = "You now have $" + money + ". Would you like to bet again?";
// vvvv
again = JOptionPane.showConfirmDialog(null, message3);

https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html