Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中为(JOptionPane.showInputDialog)中的(“取消”)按钮分配sepecifc操作?_Java_Swing_Actionlistener_Joptionpane - Fatal编程技术网

如何在Java中为(JOptionPane.showInputDialog)中的(“取消”)按钮分配sepecifc操作?

如何在Java中为(JOptionPane.showInputDialog)中的(“取消”)按钮分配sepecifc操作?,java,swing,actionlistener,joptionpane,Java,Swing,Actionlistener,Joptionpane,下面是我的问题和一个简短的示例代码: double num = 0.00; try { num = Double.parseDouble(JOptionPane.showInputDialog("Enter your num:")); } catch (Exception e) { System.err.println("Error: Invalid Input!"); JOptionPane.showMessageDialog(null, "Error: Inv

下面是我的问题和一个简短的示例代码:

 double num = 0.00;

try
{
    num = Double.parseDouble(JOptionPane.showInputDialog("Enter your num:"));

}

catch (Exception e)
{
    System.err.println("Error: Invalid Input!");
    JOptionPane.showMessageDialog(null, "Error: Invalid Input!",  
    "Error", JOptionPane.ERROR_MESSAGE);
}

//Validate the num

if (num > 0.0 && num <= 1000.00)
{
    functionA();
}

else if (deposit <= 0.0 || deposit > 1000.00)
{
 System.err.println("Error: out of range");
}
*上面代码的问题是,当我单击cancel按钮时,程序同时出现两个错误:超出范围和无效输入

请告诉我如何解决这个问题


提前感谢

首先,您需要验证输入是否为空。如果不是,则对其使用parseDouble

package org.life.java.so.questions;

import java.text.ParseException;
import javax.swing.JOptionPane;

/**
 *
 * @author Jigar
 */
public class InputDialog {

    public static void main(String[] args) throws ParseException {
        String input = JOptionPane.showInputDialog("Enter Input:");
        if(input == null){
            System.out.println("Calcel presed");
        }else{
            System.out.println("OK presed");
        }


    }
}
像这样:

try
{
    String i = JOptionPane.showInputDialog("Enter your num:");
    if (i != null)
        num = Double.parseDouble(i);
}
另外,尝试通过放置异常来不捕获异常,就像您所做的那样。始终尝试尽可能多地指定要查找的异常。 在这种情况下,您应该使用NumberFormatException,而不仅仅是Exception

catch (NumberFormatException e)
{
    System.err.println("Error: Invalid Input!");
    JOptionPane.showMessageDialog(null, "Error: Invalid Input!",  
    "Error", JOptionPane.ERROR_MESSAGE);
}

首先,您需要验证输入是否为null。如果不是,则对其使用parseDouble

像这样:

try
{
    String i = JOptionPane.showInputDialog("Enter your num:");
    if (i != null)
        num = Double.parseDouble(i);
}
另外,尝试通过放置异常来不捕获异常,就像您所做的那样。始终尝试尽可能多地指定要查找的异常。 在这种情况下,您应该使用NumberFormatException,而不仅仅是Exception

catch (NumberFormatException e)
{
    System.err.println("Error: Invalid Input!");
    JOptionPane.showMessageDialog(null, "Error: Invalid Input!",  
    "Error", JOptionPane.ERROR_MESSAGE);
}

我喜欢看到完整的代码示例。你的特别好。但是我可以提出一个公民的要求吗?拆下冗余线路。在IDE之外,顶部的4是无意义的。第二条评论的第二行没有添加任何内容。main末尾的两个空行也没有增加值。删除这些线路将缩短线路长度,例如从26条线路缩短到仅19条线路,纵向节省约25%。这可能与使用大型显示器的人无关,但对于使用分辨率较小的旧显示器的人来说,在单个屏幕上更容易看到整个示例。安德鲁·汤普森想:我喜欢看完整的代码示例。你的特别好。但是我可以提出一个公民的要求吗?拆下冗余线路。在IDE之外,顶部的4是无意义的。第二条评论的第二行没有添加任何内容。main末尾的两个空行也没有增加值。删除这些线路将缩短线路长度,例如从26条线路缩短到仅19条线路,纵向节省约25%。这可能与使用大型显示器的人无关,但对于使用分辨率较小的旧显示器的人来说,在单个屏幕上更容易看到整个示例。感谢您的考虑。@Andrew Thompson考虑到:虽然这些建议解决了用户点击“取消”的问题,但并没有解决用户因按“取消”或捕捉到数字格式异常而获得超出范围的升温的问题。我建议将验证代码移动到try块中的if语句中。虽然这些建议解决了用户点击cancel的问题,但它们并没有解决用户因按cancel或捕获数字格式异常而获得超出范围的警告的问题。我建议将验证代码移到try块中的if语句中。