0

GUI非常新穎。當用戶離開文本字段爲空或者用戶沒有輸入整數時,試圖讓我的代碼捕獲異常。當我使用InputMismatchException時,GUI窗口將繼續工作,但是在NetBeans的輸出窗口上會出現大量錯誤。(Java)GUI NumberFormatException捕獲異常,但掛起窗口

當我將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

你捕捉NFE就好了,但你仍然陷入你的while循環。這樣,您只會拋出無限例外,因爲您將correctInput設置爲false,這會繼續循環。

+0

哦,你說得對。我需要修復我的循環。編輯:如果我刪除while循環它工作正常。非常感謝 – bankey

+0

樂於幫助。請接受我的解決方案並帶有複選標記。 – Zircon