简体   繁体   English

在“ JOptionPane.showInputDialog”中,如果用户按下转义或X按钮(Java Swing),则会显示错误

[英]In “JOptionPane.showInputDialog” show error if user press escape or X button (Java Swing)

i am new to java, i just want to show an error message if user hit escape key from the keyboard or click on X button of showInputDialog or press cancel the program closes normally, 我是Java的新手,如果用户从键盘上按了转义键或单击showInputDialog X按钮或按“ 取消 ”,则我只想显示一条错误消息,程序正常关闭,

like now if i close or cancels the inputDialog it gives following error 像现在一样,如果我关闭或取消inputDialog,则会出现以下错误

 Exception in thread "main" java.lang.NullPointerException at Main.main(Main.java:11) 

i also tried to throw exception JVM but it does not works as i expected, here is my code: 我还尝试抛出异常JVM,但是它没有按我的预期工作,这是我的代码:

String userInput;
BankAccount myAccount = new BankAccount();

while (true){
   userInput = JOptionPane.showInputDialog("1. Withdraw\n2. Deposit\n3. View Balance\n4. Exit");
    switch (userInput){

        case "1":
            myAccount.withdraw(Integer.parseInt(JOptionPane.showInputDialog("Please Enter ID: ")),Double.parseDouble(JOptionPane.showInputDialog("Please Enter Amount to Withdraw: ")));
            break;
        case "2":
            myAccount.deposit(Integer.parseInt(JOptionPane.showInputDialog("Please Enter ID: ")),Double.parseDouble(JOptionPane.showInputDialog("Please enter Amount to Deposit: ")));
            break;
        case "3":
            myAccount.viewBalance(Integer.parseInt(JOptionPane.showInputDialog("Please Enter ID: ")));
            break;
        case "4":
            myAccount.exit();
            System.exit(0);
        default:
            JOptionPane.showMessageDialog(null,"Invalid Input\nPlease Try Again");
            break;
    }
}

i just want to show an error message if user click X or cancels the prompt, how can i catch this? 如果用户单击X或取消提示,我只想显示一条错误消息,我该如何捕捉? so i'll implement my logic there 所以我会在那里执行我的逻辑

JOptionPane.showInputDialog returns null, instead of a string, if the user clicks the 'x' or the Cancel button. 如果用户单击“ x”或“取消”按钮,则JOptionPane.showInputDialog返回null而不是字符串。 So instead of: 所以代替:

while (true){
  userInput = JOptionPane.showInputDialog("1. Withdraw\n2. Deposit\n3. View Balance\n4. Exit");
  switch (userInput){

    case "1": ...

You would want to do something like: 您可能想要执行以下操作:

while (true){
  userInput = JOptionPane.showInputDialog("1. Withdraw\n2. Deposit\n3. View Balance\n4. Exit");
  if (userInput == null) {
    JOptionPane.showMessageDialog(null, "Invalid Input\nPlease Try Again", "Cannot Cancel", JOptionPane.ERROR_MESSAGE);
    continue;
  }
  switch (userInput){

    case "1": ...

This will catch the cancel/'x' case, and the continue will have it skip to the next iteration of the while loop rather than throwing an error when it tries to use a switch statement with null. 这将捕获cancel /'x'的情况,而continue将使其跳至while循环的下一个迭代,而不是在尝试使用带有null的switch语句时抛出错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM