用java编写一个算书测试小软件,用来训练小学生的算术能力。(学习处理ActionEvent事件)

用java编写一个算书测试小软件,用来训练小学生的算术能力。(学习处理ActionEvent事件),第1张

用java编写一个算书测试小软件,用来训练小学生的算术能力。(学习处理ActionEvent事件
package gaoshuang;
public class Teacher
{
int numberOne,numberTwo;
String operator="";
boolean right;
public int giveNumberOne(int n)
{numberOne =(int)(Math.random()*n)+1;
return numberOne;}
public int giveNumberTwo(int n)
{numberTwo=(int)(Math.random()*n)+1;
return numberTwo;
}
public String giveOperator()
{double d=Math.random();
if(d>=0.5)
 operator="+";
 else
 operator="-";
 return operator;
}
public boolean getRight(int answer)
{if(operator.equals("+"))
{if (answer==numberOne+numberTwo)
right=true;
else 
right=false;
}
else if(operator.equals("-"))
{if (answer==numberOne-numberTwo)
right=true;
else
right=false;
}
return right;
}
}
package gaoshuang;

import java.awt.*;
import java.awt.event.*;
public class Computerframe extends frame implements ActionListener
{TextField textOne,textTwo,textResult;
Button getProblem,giveAnwser;
Label operatorLabel,message;
Teacher teacher;
Computerframe(String s)
{super(s);
teacher=new Teacher();
setLayout(new FlowLayout());
textOne=new TextField(10);//创建 textOne,其可见字符长是 10
textTwo=new TextField(10);//创建 textTwo,其可见字符长是 10
textResult=new TextField(10);//创建textResult,其可见字符长是 10
operatorLabel=new Label("+");
message=new Label(" 你还没有回答呢 ");
getProblem=new Button(" 获取题目 ");
giveAnwser=new Button(" 确认答案 ");
add(getProblem);
add(textOne);
add(operatorLabel);
add(textTwo);
add(new Label("="));
add(textResult);
add(giveAnwser);
add(message);
textResult.requestFocus();
textOne.setEditable(false);
textTwo.setEditable(false);
getProblem.addActionListener(this);//将当前窗口注册为 getProblem 的 ActionEvent 事件监视器
giveAnwser.addActionListener(this);//将当前窗口注册为giveAnwser 的 ActionEvent 事件监视器
textResult.addActionListener(this);//将当前窗口注册为 textResult 的 ActionEvent 事件监视器
setBounds(100,100,450,100);
setVisible(true);
validate();
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()==getProblem)//判断事件源是否是 getProblem
{int number1=teacher.giveNumberOne(100);
int number2=teacher.giveNumberTwo(100);
String operator=teacher.giveOperator();
textOne.setText(""+number1);
textTwo.setText(""+number2);
operatorLabel.setText(operator);
message.setText(" 请回答 ");
textResult.setText(null);
}
if(e.getSource()==giveAnwser)//判断事件源是否是giveAnwser
{String answer=textResult.getText();
try{int result=Integer.parseInt(answer);
if(teacher.getRight(result)==true)
{message.setText(" 你回答正确 ");}
else
{message.setText(" 你回答错误 ");
}
}
catch(NumberFormatException ex)
{message.setText(" 请输入数字字符 ");
}
}
textResult.requestFocus();
validate();
}
}

package gaoshuang;

public class MainClass { public static void main(String args[])
{ Computerframe frame;
frame=new Computerframe(" 算术测试 ");
 //创建窗口,其标题为:算术测试
}
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5637281.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存