0

このキャンセルボタンに問題があります。

これらのコードで:

int deposit;
String dep =
   JOptionPane.showInputDialog("How much would you like to deposit?\n\t$: ");
deposit = Integer.parseInt(dep);

「OK」と「キャンセル」ボタンが表示されるはずですが、キャンセルボタンをクリックしてもまったく反応しません。私が望むのは、「キャンセル」ボタンをクリックするたびにメインメニューに戻ることだけですが、どうすればいいですか?.

コード:

private static void processDeposit(String num, int rc){
    int deposit;
    String dep =
        JOptionPane.showInputDialog(
            "How much would you like to deposit?\n\t$: ");
    deposit = Integer.parseInt(dep);
    JOptionPane.showMessageDialog(
        null, "You have deposited $" + dep + " into the account of " + name);
    myAccount.setBalance(myAccount.getBalance() + deposit);
}
4

2 に答える 2

3

ユーザーがキャンセルをクリックすると、dep は null として返されます

したがって、次のことができます。

 if(input == null || (input != null && ("".equals(input))))   
 {

         //what you ever you need to do here

 }
于 2013-02-13T17:25:56.237 に答える
0

これを試して:

private static void processDeposit(String num, int rc) {
    int deposit;
    String dep =
            JOptionPane.showInputDialog(
                    "How much would you like to deposit?\n\t$: ");
    if (dep == null || dep.equals("")) {  // cancel pressed
        JOptionPane.getRootFrame().dispose(); // close dialog
    } else {
        deposit = Integer.parseInt(dep);  // no else or try/catch = NFE exception
        JOptionPane.showMessageDialog(
                null, "You have deposited $" + dep + " into the account of " + name);
        myAccount.setBalance(myAccount.getBalance() + deposit);
    }

}
于 2013-09-24T00:21:23.717 に答える