作业是写一个测算软件,也就是可以出题,可以测验正确与否
我参考了书上写了一个,但是不太懂注册的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();
}
}
请高手看看怎么弄啊
我参考了书上写了一个,但是不太懂注册的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();
}
}
请高手看看怎么弄啊
解决方案 »
- JAVA新手寻求学习帮助
- EJB3.0 and JMS 群 1320063
- 我在装oracle 9i时,总报错:加在数据库时出错,请问是什么原因?问题怎样解决啊?谢谢!
- tomcat5的配置问题!!在线等待!!!!
- jsp程序的jdk及tomcat打包发布
- 小弟有个小问题问:这里有会JDBC.oracle,java的吧?请进来...给300分!
- delphi7.0用ADO与SQL server连接出错?
- 请问能不能在一个很大的Web应用程序里,只在一个模块中使用struts?这对整个系统有影响吗?
- j2ee中字符终端怎么处理?
- 我是XML的初学者,请问各位该怎样学习XML?
- java赋值问题
- 急 !!! 求救 到底是什么原因导致了服务器挂掉,网页打开速度超慢
{
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);