Java算数测试小程序
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("请输入数字字符");
}
补充:软件开发 , Java ,