gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:01:39 25 4
gpt4 key购买 nike

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

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

当输入一个整数时工作正常,但其他任何东西都会出现问题。

包含try-catch block 的代码是:

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)

最佳答案

您很好地捕获了 NFE,但仍然陷入了 while 循环中。这样,您只会抛出无限的异常,因为您将 CorrectInput 设置为 false,这会继续循环。

关于java - (Java) GUI NumberFormatException 捕获异常,但挂起窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36777037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com