在今天我对之前的小测内容进行了更新和对之前的想法进行了实现,改变了界面化的形式。
Count.java
点击查看代码
import java.util.ArrayList;
import java.util.Random;
public class Count {
ArrayList<Integer> number1=new ArrayList<>();
ArrayList<Integer> number2=new ArrayList<>();
ArrayList<Integer> results= new ArrayList<>();
ArrayList<String> flags=new ArrayList<>();
public Count() {
count();
}
public void count() {
Random r=new Random();
String []flag={"+","-","*","/"};
int count = 0;
while(count < 30) {
int num1= r.nextInt(100);
int num2= r.nextInt(1,100);
int flagsIndex=r.nextInt(flag.length);
String flagtemp=flag[flagsIndex];
int result;
switch(flagtemp) {
case "+":
result=num1+num2;
break;
case "-":
if(num1 < num2){
continue;
}
result=num1-num2;
break;
case "*":
if(num1 * num2 >= 1000){
continue;
}
result=num1*num2;
break;
case "/":
if(num1 % num2 != 0){
continue;
}
result=num1/num2;
break;
default:
continue;
}
number1.add(num1);
number2.add(num2);
results.add(result);
flags.add(flagtemp);
count++;
}
}
public ArrayList<Integer> getNumber1() {
return number1;
}
public ArrayList<Integer> getNumber2() {
return number2;
}
public ArrayList<Integer> getResults() {
return results;
}
public ArrayList<String> getFlags() {
return flags;
}
}
CountJFrame.java
点击查看代码
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class CountJFrame extends JFrame {
Count count = new Count();
JTextField[] inputFields;
JLabel timerLabel;
Timer timer;
int counter = 30;
int remainingTime = 180; //设置计时器时间;
public CountJFrame() {
//界面初始化
initJFrame();
//添加题目到界面
initView();
//启动计时器
startTimer();
//让界面显示
this.setVisible(true);
}
private void initJFrame() {
//设置界面宽高
this.setSize(1100, 750);
//界面标题
this.setTitle("四则运算");
//界面置顶
this.setAlwaysOnTop(true);
//界面居中
this.setLocationRelativeTo(null);
//设置界面关闭
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//设置布局为null
this.setLayout(null);
}
private void initView() {
int numQuestions = count.number1.size();//题目数量
inputFields = new JTextField[numQuestions];//创建输入框数组
//设置计时器标签
timerLabel = new JLabel("剩余时间: " + remainingTime + " 秒");
timerLabel.setBounds(10, 10, 200, 30);
this.getContentPane().add(timerLabel);
//题目显示
JPanel questionPanel = new JPanel();
questionPanel.setLayout(new GridLayout(5, 6, 10, 10));
for (int i = 0; i < numQuestions; i++) {
//创建题目标签
JLabel questionLabel = new JLabel(count.number1.get(i) + " " + count.flags.get(i)
+ " " + count.number2.get(i) + " = ?");
questionLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));
questionPanel.add(questionLabel);
//输入框创建
inputFields[i] = new JTextField();
questionPanel.add(inputFields[i]);
}
// 将问题面板添加到主界面
questionPanel.setBounds(10, 50, 1060, 600); // 设置面板位置和大小
this.getContentPane().add(questionPanel);
//提交按钮添加
JButton submitButton = new JButton("提交");
submitButton.setBounds(900,650 , 100, 50);
submitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
StringBuilder incorrectAnswers = new StringBuilder();
boolean allCorrect = true;
for (int i = 0; i < inputFields.length; i++) {
String userInput = inputFields[i].getText();
//检查用户输入与正确答案是否相等
if (!userInput.equals(String.valueOf(count.results.get(i)))) {
counter--;
incorrectAnswers.append(count.number1.get(i))
.append(" ").append(count.flags.get(i))
.append(" ").append(count.number2.get(i))
.append(" = ? (正确答案为: ")
.append(count.results.get(i)).append(")\n");
allCorrect = false;
}
}
// 创建一个JDialog作为父窗口
JDialog dialog = new JDialog(CountJFrame.this, "结果", true);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setLocationRelativeTo(CountJFrame.this); // 居中于主窗口
if (allCorrect) {
JOptionPane.showMessageDialog(dialog, "所有答案正确!");
} else {
JOptionPane.showMessageDialog(dialog, "共答对"+counter+"个题目,有答案不正确,请查看:\n"
+ incorrectAnswers);
}
counter = 30;
timer.stop();//计时器停止;
}
});
this.getContentPane().add(submitButton);
}
//启动计时器
private void startTimer(){
timer = new Timer(1000,new ActionListener() {
public void actionPerformed(ActionEvent e) {
remainingTime--;
timerLabel.setText("剩余时间: "+remainingTime + "秒");
if(remainingTime <= 0){
timer.stop();
//超时自动判断
JOptionPane.showMessageDialog(null,"时间到,请提交答案!");
StringBuilder incorrectAnswers = new StringBuilder();
boolean allCorrect = true;
for (int i = 0; i < inputFields.length; i++) {
String userInput = inputFields[i].getText();
//检查用户输入与正确答案是否相等
if (!userInput.equals(String.valueOf(count.results.get(i)))) {
incorrectAnswers.append(count.number1.get(i))
.append(" ").append(count.flags.get(i))
.append(" ").append(count.number2.get(i))
.append(" = ? (正确答案为: ")
.append(count.results.get(i)).append(")\n");
allCorrect = false;
}
}
if (allCorrect) {
JOptionPane.showMessageDialog(null, "所以答案正确!");
} else {
JOptionPane.showMessageDialog(null, "有错误,请比对答案!");
}
}
}
});
timer.start();
}
}
Main.java
点击查看代码
public class Main {
public static void main(String[] args) {
//界面
new CountJFrame();
}
}
较大的差别在于
// 将问题面板添加到主界面
questionPanel.setBounds(10, 50, 1060, 600); // 设置面板位置和大小
this.getContentPane().add(questionPanel);
以及
//题目显示
JPanel questionPanel = new JPanel();
questionPanel.setLayout(new GridLayout(5, 6, 10, 10));
通过更改网格结构将主界面格式修改来使达到6*5的形式。
标签:count,10,questionPanel,ArrayList,2024,add,new,日志,public From: https://www.cnblogs.com/MoonbeamsC/p/18455246