|
TestFrame.java
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.*;
public class TestFrame extends JFrame implements ActionListener
{
JTextField textOne,textTwo,textResult;
JButton getProblem,giveAnwser;
JLabel operatorJLabel,message;
TestOperator testOperator;
public TestFrame()
{
testOperator=new TestOperator();
setLayout(new FlowLayout());
textOne=new JTextField(10); //创建textOne,其可见字符长是10
textTwo=new JTextField(10); //创建textTwo,其可见字符长是10
textResult=new JTextField(10); //创建textResult,其可见字符长是10
operatorJLabel=new JLabel("+");
message=new JLabel("你还没有回答呢");
getProblem=new JButton("获取题目");
giveAnwser=new JButton("确认答案");
add(getProblem);
add(textOne);
add(operatorJLabel);
add(textTwo);
add(new JLabel("="));
add(textResult);
add(giveAnwser);
add(message);
textOne.setEditable(false);
textTwo.setEditable(false);
getProblem.addActionListener(this);//当前窗口注册为getProblem的ActionEvent事件监视器
giveAnwser.addActionListener(this);//当前窗口注册为giveAnwser的ActionEvent事件监视器
textResult.addActionListener(this);//当前窗口注册为textResult的ActionEvent事件监视器
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==getProblem) //判断事件源是否是getProblem
{
int number1=testOperator.giveNumberOne(100);
int number2=testOperator.giveNumberTwo(100);
String operator=testOperator.giveOperator();
textOne.setText(""+number1);
textTwo.setText(""+number2);
operatorJLabel.setText(operator);
message.setText("请回答");
textResult.setText(null);
}
if(e.getSource()==giveAnwser) //判断事件源是否是giveAnwser
{
String answer=textResult.getText();
try{ int result=Integer.parseInt(answer);
if(testOperator.getRight(result)==true)
{
message.setText("你回答正确");
}
else
{
message.setText("你回答错误");
}
}
catch(NumberFormatException ex)
{
message.setText("请输入数字字符");
}
}
}
}
TestOperator.java
public class TestOperator
{
int numberOne,numberTwo;
String operator="";
boolean right;
//随机生成第一个数
public int giveNumberOne(int n)
{
numberOne=(int)(Math.random()*n)+1; //强制转换为int型
//因为Math.random()返回的为double型
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&&d<0.25)
operator="+";
else if(d>0.25&&d<=0.5)
operator="-";
else if(d>0.5&&d<=0.75)
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;
}
else 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;
}
}
MainClass.java
import javax.swing.JFrame;
import javax.swing.UIManager;
public class MainClass
{
public static void main(String args[])
{
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); //windows界面风格
} catch (Exception e) {
e.printStackTrace();
}
TestFrame frame=new TestFrame();
frame.setTitle("算数测试小程序");
frame.setBounds(100,100,500,100);
frame.setVisible(true);
frame.setLocationRelativeTo(null);//窗体居中显示
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
}
}
效果图:
|
|