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)

最佳答案

您的问题是,当一个或多个JTextField为空时,将调用此方法,而当您尝试将“”解析为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.
      }
    }
});

07-24 09:49
查看更多