这个
来一份这个类完整的源码,最好贴文本,截图信息不全 :)
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
public class Y extends JFrame implements ActionListener {
private JPanel panel3;
//private JPanel panel4;
private JLabel exam;
private JLabel who;
private JLabel countdown;
private int time=20;
private int quantity;
private int RightQuantity;
private JLabel[] timu = new JLabel[5];
private JLabel[] as = new JLabel[6];
private JLabel[] rf = new JLabel[5];
private int [] ra = new int[5];
private JButton give;
private JButton check;
private Font JLB = new Font(" 微软雅黑",Font.BOLD,18);
private Font JTF = new Font(" 微软雅黑",Font.PLAIN,18);
private Font JLBA= new Font(" 微软雅黑",Font.PLAIN,16);
//用于用户输入答案的文本框数组
//定义变量时需要赋初值,不然会出现空指针异常问题
private JTextField[] answer = new JTextField[5];
public Y(){
this.setTitle("本系统由数字媒体技术2021级刘馨然开发,学号542113570119");
panel3= new JPanel();
//panel4 = new JPanel();
panel3.setLayout(null);
//panel4.setLayout(null);
panel3.setBackground(Color.PINK);
//panel4.setBackground(Color.PINK);
exam = new JLabel("考试题目");
exam.setBounds(15, 40, 80, 25);
exam.setFont(JLB);
quantity=5;
who= new JLabel("正在考试考生:张三");
who.setFont(JLBA);
who.setBounds(10, 10, 200, 25);
panel3.add(exam);
panel3.add(who);
for (int i = 0; i < quantity; i++) {
timu[i] = new JLabel("题目");
timu[i].setFont(JLB);
timu[i].setBounds(30, 70*(i+1), 80, 50);
answer[i]= new JTextField(5);
answer[i].setFont(JLBA);
answer[i].setBounds(150, 70*(i+1), 150, 50);
as[i]= new JLabel("正确答案:");
as[i].setFont(JLB);
as[i].setBounds(350, 70*(i+1), 100, 50);
rf[i] = new JLabel("对错:");
rf[i].setFont(JLB);
rf[i].setBounds(600, 70*(i+1), 100, 50);
panel3.add(timu[i]);
panel3.add(answer[i]);
panel3.add(as[i]);
panel3.add(rf[i]);
}
give= new JButton("出题 ");
give.setFont(JLB);
give.setBounds(40, 450, 200, 50);
give.setBorder(BorderFactory.createRaisedBevelBorder());
give.setBackground(Color.WHITE);
panel3.add(give);
check = new JButton("检查");
check.setFont(JLB);
check.setBounds(400, 450, 200, 50);
check.setBorder(BorderFactory.createRaisedBevelBorder());
check.setBackground(Color.WHITE);
panel3.add(check);
countdown=new JLabel();
countdown.setFont(JLB);
countdown.setBounds(450, 450, 200, 50);
panel3.add(countdown);
this.getContentPane().add(panel3);
this.setSize(800,600);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
check.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CheckAnswer();
time=0;
}
});
give.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
Create();
new Thread(){
public void run() {
while(true) {
countdown.setForeground(Color.red);
countdown.setText("剩余时间为"+time+"秒!");
countdown.setBounds(450, 450, 200, 50);
time--;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(time==0){
CountDown();
break;
}
}
}
}.start();
}
});
}
public static void main(String[] args) {
Y y = new Y();
}
public void Create(){
Random ran= new Random();
String[] operator={"+","-","*","/"};
String OperChar;
int a,b,c;
String Questiontext;
for (int i = 0; i < quantity; i++) {
a=ran.nextInt(10);
b=ran.nextInt(10);
c=ran.nextInt(4);
if(a<b&&c==1){
int m = a;
a = b;
b = m;
}else if(b==0&&c==3){int n= a;
a = b;
b = n;
}
OperChar=operator[c];
Questiontext=a+OperChar+b+"=";
timu[i].setText(Questiontext);
ra[i]=get(a,b,c);}
}
public void CheckAnswer(){
RightQuantity=0;
for (int i = 0; i < quantity; i++) {
as[i].setText(Integer.toString(ra[i]));
if(!answer[i].getText().isEmpty()){
if(Integer.parseInt(answer[i].getText())==ra[i]){
rf[i].setText(" 正确");
RightQuantity++;
}else{
rf[i].setText(" 错误");
}}}
int fen = (int)(RightQuantity*100.0f/quantity);
String FeedbackStr="题目数量共"+quantity+"题,您答对"+RightQuantity+"题,得分为:"+fen;
JOptionPane.showMessageDialog(this,FeedbackStr);
}
public void actionPerformed(ActionEvent e){
}
public int get(int n1,int n2,int n3){
int result = 0;
switch(n3){
case 0:result=n1+n2;
break;
case 1:if(n1>=n2){result=n1-n2;
}else if(n1<n2){
int k = n1 ;
n1= n2 ;
n2 = k ;
result=n1-n2;
}
break;
case 2:result=n1*n2;
break;
case 3:result=n1/n2;
break;
}
return result;
}
public void CountDown(){
RightQuantity=0;
for (int i = 0; i < quantity; i++) {
as[i].setText(Integer.toString(ra[i]));}
for (int i = 0; i < quantity; i++) {
if(answer[i].getText().isEmpty()){
answer[i].setText("-999");
rf[i].setText(" 错误");
}else if(Integer.parseInt(answer[i].getText())==ra[i]){
rf[i].setText(" 正确");
RightQuantity++;
}else{
rf[i].setText(" 错误");
}
}
int fen = (int)(RightQuantity*100.0f/quantity);
String tips="题目数量"+quantity+"答对"+RightQuantity+"题,得分为;"+fen;
JOptionPane.showMessageDialog(this,tips);
}
}