(Java)GUI NumberFormatException捕获异常,但挂起窗口

时间:2016-04-21 17:53:50

标签: java user-interface exception try-catch numberformatexception

非常新的GUI。当用户将文本字段留空时,或者当用户未输入整数时,尝试使我的代码捕获异常。当我使用InputMismatchException时,GUI窗口将继续工作,但我会在NetBeans的输出窗口上出现大量错误。

当我将catch块切换到NumberFormationException时,输出窗口上没有任何内容,但GUI窗口停止工作,因为我无法输入任何内容,更改任何内容甚至关闭窗口而不强制停止运行。

放入整数时工作正常,但其他任何问题都会挂起。

包含try-catch块的代码是:

private void createAnswerField()
{
    answerField = new JTextField(5);

    answerField.addActionListener(new CustomActionListener());
}

/**
 * When the user presses enter in the text field, this is the class that makes the rest happen
 */
class CustomActionListener implements ActionListener
{
    @Override
    public void actionPerformed(ActionEvent event)
    {
        boolean correctInput = false;

        while(!correctInput)
        {
            try
            {   
                userInput = Integer.parseInt(answerField.getText());
                correctInput = true;

                if (userInput == getCorrectAnswer())
                {
                    if (counter > 1) //Fixes the try/tries problem
                    {
                        answerLabel.setText("Yay! It took you " + counter + " tries.");  
                    }
                    else
                    {
                        answerLabel.setText("Yay! It took you " + counter + " try."); 
                    }
                }
                else //hangs up here, never changes the answerLabel
                { //I believe I need to clear userInput or something of that nature
                    answerLabel.setText("Incorrect. Please try again.");
                    counter ++;
                }
            }
            catch(NumberFormatException e)
            {
                answerLabel.setText("Integers only please!");
                correctInput = false;
            }
        }
    }
}

编辑:当我使用InputMismatch时它给我的错误是:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "a"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at guiapp.MathTutor$CustomActionListener.actionPerformed(GUIApp.java:167)
at javax.swing.JTextField.fireActionPerformed(JTextField.java:508)
at javax.swing.JTextField.postActionEvent(JTextField.java:721)
at javax.swing.JTextField$NotifyAction.actionPerformed(JTextField.java:836)
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1663)
at javax.swing.JComponent.processKeyBinding(JComponent.java:2882)
at javax.swing.JComponent.processKeyBindings(JComponent.java:2929)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2845)
at java.awt.Component.processEvent(Component.java:6302)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:806)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1074)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:945)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:771)
at java.awt.Component.dispatchEventImpl(Component.java:4752)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

1 个答案:

答案 0 :(得分:1)

你正好赶上NFE,但你仍然陷入while循环中。通过这种方式,您只需抛出无限异常,因为您将correctInput设置为false,这将继续循环。