作业是写一个测算软件,也就是可以出题,可以测验正确与否
我参考了书上写了一个,但是不太懂注册的actionevent怎么用请高手看看毛病在哪,运行有错误,错误如下
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 mathtestFrame.actionPerformed(mathtestFrame.java:58)
at java.awt.AWTEventMulticaster.actionPerformed(Unknown Source)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(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)自己写的代码如下
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class mathtestFrame extends Frame implements ActionListener,WindowListener
{
private TextField text_l,text_r,text_re;
private Button button_get,button_confirm;
private Dialog dialog;
private Label label_dialog;public mathtestFrame()
{
super("算术测试");
this.setSize(500,200);
this.setResizable(false);
this.setBackground(java.awt.Color.BLUE);
this.setLocation(300,240);
this.setLayout(new java.awt.FlowLayout(FlowLayout.CENTER));

button_get=new Button("获取题目");
this.add(button_get);
button_get.addActionListener(this);

text_l=new TextField(5);
text_l.setEditable(false);
this.add(text_l);

this.add(new Label("-"));

text_r=new TextField(5);
text_r.setEditable(false);
this.add(text_r);

this.add(new Label("="));

text_re=new TextField(5);
text_re.setEditable(true); text_re.addActionListener(this);
this.add(text_re);

button_confirm=new Button("获取答案");
this.add(button_confirm);
button_get.addActionListener(this);

this.addWindowListener(this);
this.setVisible(true);

dialog=new Dialog(this,"判断结果",true);

dialog.setSize(240,80);
label_dialog=new Label("",Label.CENTER);
dialog.add(label_dialog);
dialog.addWindowListener(this);
}

public void actionPerformed(ActionEvent e)
{   int k=Integer.parseInt(text_re.getText());
    int i=0,j=0;
if(e.getSource()==button_get)

{i=(int) (Math.random()*100);
j=(int)Math.random()*100;
text_l.setText(""+i);
text_r.setText(""+j);}
else

    if(e.getSource()==button_confirm)
     text_re.setText(""+k);
{if (k==i-j)
this.add(new Label("答案正确"));
else this.add(new Label("答案错误"));
}
    



}
 public void windowClosing(WindowEvent e)
 {
 System.exit(0);
 }
 public void windowOpened(WindowEvent e){}
 public void windowActivated(WindowEvent e){}
 public void windowDeactivated(WindowEvent e){}
 public void windowClosed(WindowEvent e){}
 public void windowIconified(WindowEvent e){}
 public void windowDeiconified(WindowEvent e){}
 
 
 public static void main(String arg[])
 {new mathtestFrame();
 }
 }
请高手看看怎么弄啊

解决方案 »

  1.   

    类型转换错误!仔细检查一下存在类型转换的地方,比如int k=Integer.parseInt(text_re.getText());你这个text_re.getText本身的值是不是int类型的,我看着你前面好像将这个值加了个“”;这样就变成string类型了,你在强转int的时候肯定会报错的
      

  2.   

    public void actionPerformed(ActionEvent e)

    String result = text_re.getText();
    if(result==null||"".equals(result))
    {
    this.add(new Label("请输入答案"));
    return;
    }
    //最好加上是否数字的判断,如果不是数字答案肯定不对Pattern p=Pattern.compile("\\d+");
    Matcher m = p.matcher(result);
    if(!m.matches())
    {
    this.add(new Label("答案错误"));
    return;
    }
    int k=Integer.parseInt(result);