取消按钮必须具有功能

时间:2013-02-13 17:22:49

标签: java swing joptionpane

我的取消按钮出现问题。

使用这些代码:

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);
}

2 个答案:

答案 0 :(得分:3)

如果用户点击取消,则dep将返回为空

所以你可以这样做:

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

         //what you ever you need to do here

 }

答案 1 :(得分: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);
    }

}