0

这个取消按钮有问题。

使用这些代码:

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

应该会出现一个“确定”和“取消”按钮,但是每当我单击取消按钮时,根本没有任何响应。我想要的是,每当我点击“取消”按钮时,它应该让我回到主菜单,但是如何呢?

编码:

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 回答