package mybag;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Haha extends JFrame
{
static int opt=1; //操作符,opt=1时取+,2、3、4分别取-、*、/
static float r=0,f=0; //r保存正确题数,f保存错误题数
JLabel succes,fail,percent,su,fa,pe;
JButton sure,nex; //“确定”按钮和“下一题”按钮
JTextField op1,op2,result;
JComboBox operator;
JLabel is,non;
JMenuBar manage;
JMenu menu; //菜单
JMenuItem start,out; //菜单子项:开始、退出
private Object ope[]={"+","-","*","/"};
public Haha()
{
super("四则运算测试");
Container c=getContentPane();
c.setLayout(new FlowLayout());
setBounds(500,300,230,180); //设置窗口位置和大小
sure=new JButton("确定");
sure.setEnabled(false);
nex=new JButton("下一题");
nex.setEnabled(false);
op1=new JTextField(4);
op2=new JTextField(4);
result=new JTextField(4);
operator=new JComboBox(ope);
is=new JLabel("=");
op1.setEditable(false);
op2.setEditable(false);
succes=new JLabel("正确数:");
fail=new JLabel("错误数:");
percent=new JLabel("正确率:");
su=new JLabel("0");
fa=new JLabel("0");
pe=new JLabel("0");
non=new JLabel();
manage=new JMenuBar();
menu=new JMenu("菜单");
start=new JMenuItem("开始");
out=new JMenuItem("退出");
manage.add(menu);
menu.add(start);
menu.add(out);
dose begin=new dose(1); //1代表菜单“开始“
dose calculate=new dose(2); //2代表按钮“确定”
dose next=new dose(3); //3代表按钮“下一题”
dose op=new dose(4); //4代表“操作符”
dose exit=new dose(5); //5代表菜单“退出”
start.addActionListener(begin); //添加开始事件
out.addActionListener(exit); //添加退出事件
sure.addActionListener(calculate); //确定时间,即‘计算’
nex.addActionListener(next); //下一题按钮事件
operator.addActionListener(op); //操作符选择事件
Box hbox0=Box.createHorizontalBox(); //hbox0水平容器摆放确定和下一题按钮
hbox0.add(sure);
hbox0.add(Box.createHorizontalStrut(3));
hbox0.add(nex);
Box hbox1=Box.createHorizontalBox(); //hbox1水平容器摆放两个操作数、操作符、= 和 用户填写结果的文本框
hbox1.add(op1);
hbox1.add(Box.createHorizontalStrut(3));
hbox1.add(operator);
hbox1.add(Box.createHorizontalStrut(3));
hbox1.add(op2);
hbox1.add(Box.createHorizontalStrut(3));
hbox1.add(is);
hbox1.add(Box.createHorizontalStrut(3));
hbox1.add(result);
Box hbox21=Box.createHorizontalBox(); //hbox21水平容器摆放“正确数”标签和正确数r
hbox21.add(succes);
hbox21.add(Box.createGlue());
hbox21.add(su);
Box hbox22=Box.createHorizontalBox(); //hbox22水平容器摆放“错误数”标签和错误数f
hbox22.add(fail);
hbox22.add(Box.createGlue());
hbox22.add(fa);
Box hbox23=Box.createHorizontalBox(); //hbox23水平容器摆放“正确率”标签和正确路 [r/(r+f)]*100%
hbox23.add(percent);
hbox23.add(Box.createGlue());
hbox23.add(pe);
Box hbox11=Box.createHorizontalBox(); //hbox11水平容器摆放“菜单”和“空标签”,空标签无任何作用,只是为了使菜单左放
hbox11.add(manage);
hbox11.add(Box.createGlue());
hbox11.add(non);
Box vbox=Box.createVerticalBox(); //vbox垂直容器,依次放入水平容器
vbox.add(hbox11);
vbox.add(hbox1);
vbox.add(hbox0);
vbox.add(hbox21);
vbox.add(hbox22);
vbox.add(hbox23);
c.add(vbox);
setVisible(true);
}
public class dose implements ActionListener
{
int want;
public dose(int a) //构造函数,a识别事件
{
want=a;
}
public void actionPerformed(ActionEvent e)
{
switch(want)
{
case 1:begintest();break;
case 2:suredo();break;
case 3:nextque();break;
case 4:choop();break;
case 5:System.exit(0);;break;
}
}
public void begintest() //开始
{
int a,b;
b=(int)(Math.random()*10);
a=(int)(Math.random()*10);
op1.setText(String.valueOf(a));
op2.setText(String.valueOf(b));
r=0;
f=0;
su.setText(String.valueOf(r));
fa.setText(String.valueOf(f));
pe.setText(String.valueOf(0));
result.setText("");
start.setText("重新开始");
result.requestFocus();
sure.setEnabled(true);
}
public void suredo() //确定按钮
{
if(result.getText().equals(""))
{
JOptionPane.showMessageDialog(null,"请输入你的答案!!");
result.requestFocus();
}
if(opt==1) //+
{
if (Integer.parseInt(op1.getText())+Integer.parseInt(op2.getText())==Integer.parseInt(result.getText()))
{
r=r+1;
su.setText(String.valueOf(r));
pe.setText(String.valueOf((r/(r+f))*100)+"%");
}
else
{
f=f+1;
fa.setText(String.valueOf(f));
pe.setText(String.valueOf((r/(r+f))*100)+"%");
}
}
if(opt==2) //-
{
if (Integer.parseInt(op1.getText())-Integer.parseInt(op2.getText())==Integer.parseInt(result.getText()))
{
r=r+1;
su.setText(String.valueOf(r));
pe.setText(String.valueOf((r/(r+f))*100)+"%");
}
else
{
f=f+1;
fa.setText(String.valueOf(f));
pe.setText(String.valueOf((r/(r+f))*100)+"%");
}
}
if(opt==3) //*
{
if (Integer.parseInt(op1.getText())*Integer.parseInt(op2.getText())==Integer.parseInt(result.getText()))
{
r=r+1;
su.setText(String.valueOf(r));
pe.setText(String.valueOf((r/(r+f))*100)+"%");
}
else
{
f=f+1;
fa.setText(String.valueOf(f));
pe.setText(String.valueOf((r/(r+f))*100)+"%");
}
}
if(opt==4) ///
{
if(Float.parseFloat(op1.getText())/Float.parseFloat(op2.getText())==Float.parseFloat(result.getText()))
{
r=r+1;
su.setText(String.valueOf(r));
pe.setText(String.valueOf((r/(r+f))*100)+"%");
}
else
{
f=f+1;
fa.setText(String.valueOf(f));
pe.setText(String.valueOf((r/(r+f))*100)+"%");
}
}
op1.setText("");
op2.setText("");
result.setText("");
result.requestFocus();
sure.setEnabled(false);
nex.setEnabled(true);
}
public void nextque() //下一题
{
int a,b;
a=(int)(Math.random()*10);
b=(int)(Math.random()*10);
op1.setText(String.valueOf(a));
op2.setText(String.valueOf(b));
result.requestFocus();
sure.setEnabled(true);
nex.setEnabled(false);
if(op2.getText().equals("0") && operator.getSelectedItem().equals("/")) nextque();
}
public void choop() //选择操作符
{
Object opstr;
opstr=operator.getSelectedItem();
if (opstr.equals("+"))
opt=1;
else if(opstr=="-")
opt=2;
else if (opstr.equals("*"))
opt=3;
else if (opstr.equals("/"))
opt=4;
}
}
public static void main(String args[])
{
Haha frm=new Haha();
frm.addWindowListener(new Handler());
}
static class Handler extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
}