解析JTextField输入时的NumberFormatException

时间:2016-04-21 23:29:20

标签: java swing input jtextfield numberformatexception

我认为Java applet有一个可用的代码。但是,我没有按照我想要的方式执行程序。

以下是运行时显示的消息(解析JTextField输入时):

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 skiRace.init(skiRace.java:24)
    at ready.AppletRunner.run(AppletRunner.java:209)
    at java.lang.Thread.run(Unknown Source)

这是我的代码:

import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class skiRace extends Applet implements ActionListener
{
int txt = 0;
int txt2 = 0;
int txt3 = 0;
int txt4 = 0;

public void init ()
{
    resize (300, 350);
    setBackground (Color.blue);
    JLabel pic = new JLabel (createImageIcon ("race.jpg"));

    JLabel ins = new JLabel ("Enter the number of points beside each skier.");
    ins.setFont (new Font ("Arial", Font.BOLD, 12));

    JLabel p1 = new JLabel (createImageIcon ("player1.jpg"));
    JTextField answer = new JTextField (3);
    int txt = Integer.parseInt (answer.getText ());

    JLabel p2 = new JLabel (createImageIcon ("player2.jpg"));
    JTextField answer2 = new JTextField (3);
    int txt2 = Integer.parseInt (answer2.getText ());

    JLabel p3 = new JLabel (createImageIcon ("player3.jpg"));
    JTextField answer3 = new JTextField (3);
    int txt3 = Integer.parseInt (answer3.getText ());

    JLabel p4 = new JLabel (createImageIcon ("player4.jpg"));
    JTextField answer4 = new JTextField (3);
    int txt4 = Integer.parseInt (answer4.getText ());

    JButton done = new JButton ("Done");
    done.setActionCommand ("done");
    done.addActionListener (this);

    add (pic);
    add (ins);
    add (p1);
    add (answer);
    add (answer2);
    add (answer3);
    add (answer4);
    add (done);

}


public void actionPerformed (ActionEvent e)
{
    if (e.getActionCommand ().equals ("done"))
    {
        if ((txt == 7) && (txt2 == 8) && (txt3 == 6) && (txt4 == 9))
        {
            JOptionPane.showMessageDialog (null, "You got it! Great work!", "Correct",
                    JOptionPane.INFORMATION_MESSAGE);
        }
        else
        {
            JOptionPane.showMessageDialog (null, "You are incorrect, please try again.",
                    "Incorrect", JOptionPane.ERROR_MESSAGE);
        }
    }
}


protected static ImageIcon createImageIcon (String path)
{
    java.net.URL imgURL = dice.class.getResource (path);
    if (imgURL != null)
    {
        return new ImageIcon (imgURL);
    }
    else
    {
        System.err.println ("Couldn't find file: " + path);
        return null;
    }
}
}

为什么会这样?

我认为它与带有输入的对话框或文本字段有关,因为这是我在Java applet中使用这两个函数中的任何一个的第一个程序。

1 个答案:

答案 0 :(得分:2)

由于此行(#24)位于init()方法中:

int txt = Integer.parseInt (answer.getText ());

answer将具有与其构造完全相同的文本(即没有 - 空字符串)。空字符串不是整数或可解析为整数。

我建议改为:

  • 创建SpinnerNumberModel并保留对该模型的引用。在JSpinner的构造函数中添加模型,而不是JTextField
  • 当微调器的值发生变化时,或者当用户激活按钮时,而不是直接在创建它之后和用户看到控件之前,对数字(通过查询模型获得)进行操作,更不用说了改变它。

其他说明:

  • 不要'影子'变量。 E.G。

    int txt4 = 0;
    

    被声明为类属性,但在init()方法中将其声明为再次作为局部变量。 E.G。

    int txt4 = Integer.parseInt (answer4.getText ());
    

    要解决此问题,请将其更改为:

    txt4 = Integer.parseInt (answer4.getText ()); // use class attribute to store result