【问题标题】:My try catch does not show a warning message我的 try catch 没有显示警告信息
【发布时间】:2015-11-04 16:51:26
【问题描述】:

我正在制作一个小程序,并制作了一个 try catch 块,当程序从三个文本字段中获取文本时,如果任何文本字段不是整数,它应该显示一个对话框,说明输入无效,但是,什么都没有出现,我不知道如何在我的小程序上添加一个对话框

try{
        if (e.getSource() == bttn)
        {

            as=T1.getText();
            ag=T2.getText();
            ab=T3.getText();
            as=as.trim();
            ag=ag.trim();
            ab=ab.trim();

            redColor= Integer.parseInt(as);
            greenColor= Integer.parseInt(ag);
            blueColor= Integer.parseInt(ab);
        }
    }
    catch (InputMismatchException exception){
        JOptionPane.showMessageDialog(null,"Invalid Input","TITLE",JOptionPane.WARNING_MESSAGE);

    }

【问题讨论】:

    标签: java exception applet dialog


    【解决方案1】:

    你的问题可能就在这里:

    if (e.getSource() == bttn)
    

    这种情况可能永远不会成立。

    改成

    if (e.getSource().equals(bttn))
    

    哦,parseInt 抛出 NumberFormatException,而不是 InputMismatchException

    【讨论】:

    • 我爱你,终于成功了我从昨天开始一直在尝试这样做,非常感谢你
    猜你喜欢
    • 2014-11-14
    • 2014-03-08
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多