1

当我从转换JTextField为 int 时,我的 GUI 程序崩溃。可以帮助我查看此代码段并查看问题所在,因为我使用了相同的方法,并且有效。

我所做的:

new JTextLabel l;

得到什么

String A = l.getText() >int i = Interger.parse(A);

代码:

btnAdd.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        int nr = Integer.parseInt(txtApt.getText());
        int nrP = Integer.parseInt(txtNrPips.getText());
        String owner = txtOwner.toString();
        Apartment a = new Apartment(nr, nrP, owner, false);
        Expense b = new Expense(Float.parseFloat(textWW.getText()), Float.parseFloat(textCW.getText()),
                Float.parseFloat(textGAS.getText()), Float.parseFloat(txtELEC.getText()), Float.parseFloat(textHEAT.getText()));
        ArrayList<Expense> l = new ArrayList<>();
        l.add(b);
        cont.addKeyWithList(a,l);
    }
});

堆:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: ""
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at gui.Gui$4.actionPerformed(Gui.java:195)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$200(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
4

3 回答 3

5

您的问题是当一个或多个 JTextFields 为空时调用此方法,并且当您尝试将“”解析为 int 时发生错误。如果发生这种情况,一个解决方案是阻止调用此方法。例如,您可以对您的 JTextField 文档使用 DocumentListener 来查看 JTextField 是否有文本,并保持您的 JButton 处于禁用状态,直到文本出现在所有必要的文本字段中。

另外,请使用我上面建议的 try / catch 块来捕获异常并优雅地处理它们:

btnAdd.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
      try {
        int nr = Integer.parseInt(txtApt.getText());
        int nrP = Integer.parseInt(txtNrPips.getText());
        // ... etc ...
      } catch (NumberFormatException nfe) {
        // warn the user that the textfields are not parsing well.
      }
    }
});
于 2012-12-20T00:11:51.403 回答
2

您的某些文本字段没有有效的数字内容,这会阻塞解析。要解决此问题,您需要预先过滤数据。

float floatWW = MY_DEFAULT_VALUE;
boolean errorWW = false;
try {
  float floatWW = Float.parseFloat(textWW.getText());
} catch (NumberFormatException ex) {
  errorWW = true;
}

(对于你们每个人的 parseFloat 调用,依此类推)

接下来,您可以决定是显示错误还是在给出错误 iuput 时只接受默认值。

于 2012-12-20T00:16:38.853 回答
1

当您转换无法转换为数字的字符串时会出现问题。例如:有 2 个字符串变量,一个是“3”,另一个是“三”。在后面的情况下,当您尝试转换为整数时,它会给出 NumberFormatException。

这里也存在同样的问题,如果你没有在文本字段中提供任何值,它是空白的并且会发生异常。

所以,为了摆脱异常。首先检查 getText() 值不应为 null 和空白,然后应用正则表达式检查它将只接受数字而不是字母和特殊字符。

于 2012-12-20T02:18:54.687 回答