📄 tianlijiao.java
字号:
import java.awt.*;import java.awt.event.*;
class Myframe extends Frame implements ActionListener,ItemListener
{
Label lab1,lab2,content, correctLab;Choice ch;
TextField txt1; Checkbox radio1,radio2,radio3,radio4,check1;
CheckboxGroup reply;
Button btn1,btn2;
int count;
int add1[];int add2[];int correct[];char ope1[];String ope2[];int answer[][];String oo="=";String pp=".";
double temp;boolean flag;
public Myframe(String s)
{
super(s);
lab1=new Label("题号:");
lab2=new Label("答案:");
txt1=new TextField();
ch=new Choice();content=new Label(" ");
reply=new CheckboxGroup();
radio1=new Checkbox("",false,reply); radio2=new Checkbox("",false,reply);
radio3=new Checkbox("",false,reply); radio4=new Checkbox("",false,reply);
check1=new Checkbox("帮助");
btn1=new Button("上一题");btn2=new Button("下一题");
correctLab=new Label(" ");
correctLab.setVisible(false);
add1=new int[10];add2=new int[10];
ope1=new char[10];ope2=new String[10];
answer= new int[10][4];
correct=new int[10];
setLayout(null);setBackground(Color.yellow);//设置不同的背景
btn1.setBackground(Color.pink);btn2.setBackground(Color.pink);//设置颜色
add(lab1); lab1.setBounds(30,20,40,30);
add(lab2); lab2.setBounds(230,20,40,30); //题目内容
add(ch); ch.setBounds(65,50,80,30);//下拉列表框
add(txt1); txt1.setBounds(265,50,80,20);//选择答案显示的文本框
add(content); content.setBounds(50,100,300,30);//帮助答案的显示
add(radio1); radio1.setBounds(50,130,40,30);
add(radio2); radio2.setBounds(100,130,40,30);
add(radio3); radio3.setBounds(150,130,40,30);
add(radio4); radio4.setBounds(200,130,40,30);
add(check1); check1.setBounds(80,180,50,30);
add(btn1); btn1.setBounds(200,250,80,25);
add(btn2); btn2.setBounds(300,250,80,25);
add(correctLab); correctLab.setBounds(135,180,150,30);
txt1.addActionListener(this);
btn1.addActionListener(this);
btn2.addActionListener(this);
radio1.addItemListener(this);
radio2.addItemListener(this);
radio3.addItemListener(this);
radio4.addItemListener(this);
ch.addItemListener(this);
check1.addItemListener(this);
for(int i=0;i<10;i++)
{
ch.add(String.valueOf(i+1));
}
setSize(500,400);setVisible(true);
addWindowListener(new WindowAdapter()//实现窗体的关闭
{public void windowClosing(WindowEvent e)
{ System.exit(0);setVisible(false);
}
});
rad();answer();Trueanswer();
content.setText(String.valueOf(ch.getSelectedIndex()+1)+pp+" "+String.valueOf(add1[0])+ope2[0]+String.valueOf(add2[0])+oo);
radio1.setLabel(String.valueOf(answer[0][0]));
radio2.setLabel(String.valueOf(answer[0][1]));
radio3.setLabel(String.valueOf(answer[0][2]));
radio4.setLabel(String.valueOf(answer[0][3]));
btn1.setEnabled(false);
validate();
}
public void rad()//产生随机数
{
for(int i=0;i<10;i++)
{
add1[i]=(int)(100*Math.random())+1;
add2[i]=(int)(100*Math.random())+1;
correct[i]=add1[i]+add2[i];
if(Math.random()>0.5)
{
ope1[i]='-';
ope2[i]="-";
}
else
{
ope1[i]='+';
ope2[i]="+";
}
//ch.add(String.valueOf(i+1));
}
for(int i=0;i<10;i++)
{
switch(ope1[i])
{
case '+':
correct[i]=add1[i]+add2[i];
break;
case '-':
if (add1[i]>add2[i])
{
correct[i]=add1[i]-add2[i];
}
else
{
correct[i]=add2[i]-add1[i];
}
break;
}
}
}
public void answer() //产生答案
{
for(int i=0;i<10;i++)
{
int max;
int temp;
boolean flag=false;
max=correct[i];
if(max>100)
{
max=200;
}
else if(max>10)
{
max=100;
}
else
{
max=10;
}
for(int j=0;j<4;j++)
{
temp=(int)(max*Math.random())+1;
int k=0;
while(k<j)
{
if(temp==answer[i][k++])
{
temp=(int)(max*Math.random())+1;
}
}
answer[i][j]=temp;
}
}
}
public void Trueanswer()//正确答案
{
for(int i=0;i<10;i++)
{
temp=Math.random();
flag=false;
for(int j=0;j<4;j++)
{
if(answer[i][j]==correct[i])
{
flag=true;
}
}
if(flag) continue;
if(temp<=0.25)
{
answer[i][0]=correct[i];
}
else if(temp>0.25&&temp<=0.5)
{
answer[i][1]=correct[i];
}
else if(temp>0.5&&temp<=0.75)
{
answer[i][2]=correct[i];
}
else
{
answer[i][3]=correct[i];
}
}
}
public void actionPerformed(ActionEvent e)
{int temp=ch.getSelectedIndex();int count;
if(e.getSource()==btn1)
{
ch.select(--temp);
count=temp;
content.setText(String.valueOf(ch.getSelectedIndex()+1)+pp+" "+String.valueOf(add1[temp])+ope2[temp]+String.valueOf(add2[temp])+oo);
radio1.setLabel(String.valueOf(answer[temp][0]));
radio2.setLabel(String.valueOf(answer[temp][1]));
radio3.setLabel(String.valueOf(answer[temp][2]));
radio4.setLabel(String.valueOf(answer[temp][3]));
if (count==0)
{
btn1.setEnabled(false);
}
btn2.setEnabled(true);
txt1.setText("");
check1.setState(false);
correctLab.setVisible(false);
}
if(e.getSource()==btn2)
{
ch.select(++temp);
count=temp;
content.setText(String.valueOf(ch.getSelectedIndex()+1)+pp+" "+String.valueOf(add1[temp])+ope2[temp]+String.valueOf(add2[temp])+oo);
radio1.setLabel(String.valueOf(answer[temp][0]));
radio2.setLabel(String.valueOf(answer[temp][1]));
radio3.setLabel(String.valueOf(answer[temp][2]));
radio4.setLabel(String.valueOf(answer[temp][3]));
if (count==9)
{
btn2.setEnabled(false);
}
btn1.setEnabled(true);
txt1.setText("");
check1.setState(false);
correctLab.setVisible(false);
}
count=temp;
//setKey();
if(e.getSource()==txt1)
{
String name=txt1.getText();
if(name.equals(radio1.getLabel()))
{
radio1.setState(true);
}
if(name.equals(radio2.getLabel()))
{
radio2.setState(true);
}
if(name.equals(radio3.getLabel()))
{
radio3.setState(true);
}
if(name.equals(radio4.getLabel()))
{
radio4.setState(true);
}
}
}
public void itemStateChanged(ItemEvent e)
{
if(e.getItemSelectable()==ch)
{
int temp=ch.getSelectedIndex();
count=temp;
content.setText(String.valueOf(ch.getSelectedIndex()+1)+pp+" "+String.valueOf(add1[temp])+ope2[temp]+String.valueOf(add2[temp])+oo);
radio1.setLabel(String.valueOf(answer[temp][0]));
radio2.setLabel(String.valueOf(answer[temp][1]));
radio3.setLabel(String.valueOf(answer[temp][2]));
radio4.setLabel(String.valueOf(answer[temp][3]));
}
//setKey();
if (count==0)
{
btn1.setEnabled(false);
}
else
{btn1.setEnabled(true);
}
if (count==9)
{ btn2.setEnabled(false);
}
else
{btn2.setEnabled(true);
}
txt1.setText("");
check1.setState(false);
//correctLab.setVisible(false);
if(e.getItemSelectable()==radio1)
{
txt1.setText(radio1.getLabel());
}
if(e.getItemSelectable()==radio2)
{
txt1.setText(radio2.getLabel());
}
if(e.getItemSelectable()==radio3)
{
txt1.setText(radio3.getLabel());
}
if(e.getItemSelectable()==radio4)
{
txt1.setText(radio4.getLabel());
}
if(e.getItemSelectable()==check1)
{
if(e.getStateChange()==e.SELECTED)
{
int temp;
correctLab.setVisible(true);
temp=ch.getSelectedIndex();
correctLab.setText("正确答案是:"+String.valueOf(correct[temp]));
}
else if(e.getStateChange()==e.DESELECTED)
{
correctLab.setVisible(false);
}
}
}
}
public class tianlijiao
{
public static void main(String args[])
{
Myframe dl=new Myframe(" ");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -