今天是星期天,写了一下大数据的作业和软件构造的作业。
package com.example.test; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import java.util.Random; public class Test1 extends JFrame { private JLabel [] questionLabels; private JTextField [] answerFields; private JButton submitButton; private JButton resButton; private JButton addButton; private JButton readButton; private List<Calculate> calculates; public Test1() { // 设置窗口标题 super("数学题卡"); // 设置窗口关闭时的默认操作 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 初始化题目和界面组件 initQuestion(); initUI(); // 设置窗口大小和布局 setSize(300, 600); setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); } public Test1(int i) { // 设置窗口标题 super("数学题卡"); // 设置窗口关闭时的默认操作 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 初始化题目和界面组件 initQuestion1(); initUI(); // 设置窗口大小和布局 setSize(300, 600); setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS)); } private void initQuestion() { calculates=Play.setCAl(100,0,10); questionLabels=new JLabel[calculates.size()]; answerFields=new JTextField[calculates.size()]; // 创建题目标签 for (int i=0;i<questionLabels.length;i++) { questionLabels[i] = new JLabel("题目"+i+": " + calculates.get(i).getFirst() + calculates.get(i).getStr() + calculates.get(i).getSecond() + "= ?"); } } private void initQuestion1() { calculates=Play.setCalculates(JDBC.find(10)); questionLabels=new JLabel[calculates.size()]; answerFields=new JTextField[calculates.size()]; // 创建题目标签 for (int i=0;i<questionLabels.length;i++) { questionLabels[i] = new JLabel("题目"+i+": " + calculates.get(i).getFirst() + calculates.get(i).getStr() + calculates.get(i).getSecond() + "= ?"); } } private void initUI() { // 创建文本框和提交按钮 for (int i=0;i<calculates.size();i++){ answerFields[i] = new JTextField(); } submitButton = new JButton("提交"); resButton = new JButton("再来一套"); addButton=new JButton("写入mysql"); readButton=new JButton("读出MySQL"); // 添加按钮点击事件监听器 submitButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { checkAnswer(); } }); resButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Test1 quiz = new Test1(); quiz.setVisible(true); } }); readButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { Test1 quiz = new Test1(1); quiz.setVisible(true); } }); addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JDBC.add(calculates); } }); for (int i=0;i<calculates.size();i++){ // 将组件添加到窗口中 add(questionLabels[i]); add(answerFields[i]); } add(submitButton); add(resButton); add(addButton); add(readButton); } private void checkAnswer() { try { int num=0; int error=0; for (int i=0;i<calculates.size();i++){ // 获取用户输入的答案 int userAnswer = Integer.parseInt(answerFields[i].getText()); // 检查答案并给出反馈 if (userAnswer == calculates.get(i).getResult()) { num++; answerFields[i].setBackground(Color.GREEN); //JOptionPane.showMessageDialog(this, "Correct! Well done!"); } else { error++; answerFields[i].setBackground(Color.RED); //JOptionPane.showMessageDialog(this, "Incorrect. The correct answer is ."); } } JOptionPane.showMessageDialog(this, "你答对了"+num+"题,"+"你答错了"+error+"题"); } catch (NumberFormatException ex) { JOptionPane.showMessageDialog(this, "请输入数字"); } } public static void main(String[] args) { // 在事件分发线程中创建和显示GUI SwingUtilities.invokeLater(new Runnable() { @Override public void run() { Test1 quiz = new Test1(); quiz.setVisible(true); } }); } }
标签:总结,java,每日,private,new,BoxLayout,import,calculates From: https://www.cnblogs.com/syhxx/p/17858185.html