11.19每日总结

将口算题生成软件中的题目及习题保存到MySQL数据库中,并实现题目的保存和读取。提交实现效果截图及相关代码。

package Calculation;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class MainMenu extends JFrame {

   private static final long serialVersionUID = 1L;  //定义程序序列化,1L是默认定义

   protected static String s1;
   protected static String s2;
   protected static String s3;

   private JLabel jl1 = new JLabel("欢迎使用");
   private JLabel jl3 = new JLabel("    ");
   private JLabel jl4 = new JLabel("        ");
   private JButton JB1 = new JButton("开始答题");
   private JButton JE = new JButton("退出登录");
   private JPanel jp1 = new JPanel();
   private JPanel jp2 = new JPanel();
   private JPanel jp3 = new JPanel();
   private JPanel jp4 = new JPanel();
   private JPanel jp5 = new JPanel();
   private JPanel jp6 = new JPanel();
   private JPanel jp7 = new JPanel();
   private void Event() {

       JB1.addActionListener(new ActionListener() {

           @Override
           public void actionPerformed(ActionEvent arg0) {
               // TODO Auto-generated method stub

               setVisible(false);
               dispose();

               new Test();
           }

       });

       JE.addActionListener(new ActionListener() {

           @Override
           public void actionPerformed(ActionEvent arg0) {
               // TODO Auto-generated method stub
               setVisible(false);
               dispose();



           }


       });
   }

   public MainMenu() {
       jl1.setFont(new Font("黑体", Font.BOLD, 35));
       jl1.setForeground(Color.gray);
       jl1.setHorizontalAlignment(JLabel.CENTER); // 水平居中对齐
       jl1.setVerticalAlignment(JLabel.CENTER);

       JB1.setFont(new Font("宋体", Font.PLAIN, 25));
       JE.setFont(new Font("宋体", Font.PLAIN, 25));

       jp1.add(jl1);
       // ...
       jp7.setLayout(new GridLayout(2, 1, 20, 20)); // 两行一列,设置组件之间的水平和垂直间距

       jp7.setBorder(BorderFactory.createEmptyBorder(20, 0, 20, 0)); // 设置上下留白

       this.setTitle("欢迎使用口算答题系统");
       this.setSize(500, 450);
       this.setLocationRelativeTo(null);  //将此窗口置于屏幕的中央
       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       this.setLayout(new BorderLayout(20,20));

       jp1.setLayout(new GridBagLayout());
       this.add(jp1,BorderLayout.NORTH);

       jp7.add(JB1);

       jp7.add(JE);
       this.add(jp7,BorderLayout.SOUTH);

       //美化界面
       try {
           UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
       } catch (Exception e) {
           e.printStackTrace();
       }

       Event();

       this.setVisible(true);
   }
   public static void main(String[] args) {

       new MainMenu();
   }


}















package Calculation;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;


public class Test extends JFrame {

   private static final long serialVersionUID = 1L;  //定义程序序列化,1L是默认定义

   private int max = 50;
   private int a = 0;
   private int b = 0;
   private int c = 0;
   private int count = 1;

   private JLabel JLgross = new JLabel("共5页");
   private JLabel JLpage = new JLabel();  //页码控制
   private JLabel jl1 = new JLabel("  "); //插两个空格进去

   private JLabel JLhour = new JLabel();
   private JLabel JLminute = new JLabel();
   private JLabel JLseconds = new JLabel();

   private JButton JB1 = new JButton("提交");
   private JButton b1 = new JButton("首页");
   private JButton b2 = new JButton("上一页");
   private JButton b3 = new JButton("下一页");
   private JButton b4 = new JButton("尾页");
   private JButton b5 = new JButton("返回");

   private JLabel jl[] = new JLabel[50]; //50个题目标签
   private JTextField jtf[] = new JTextField[50];//50个文本框,存储答案
   private JTextField jta[] = new JTextField[50];//50个文本框,存储结果
   //private JPanel pnl1 = new JPanel();
   private JPanel pnl2 = new JPanel();
   private JPanel pnl3 = new JPanel();
   private JPanel pnl4 = new JPanel();

   static CardLayout care = new CardLayout();

   int[] answer = new int[max];
   String[] studentAnswer = new String[max];
   String[] Test = new String[50];
   int testNum = 0;

   private void Event() {

       //提交试卷按钮
       JB1.addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e) {


               int correctAnswer = 0;

               for (int i = 0, k = 0; i < max; i++, k++) {

                   studentAnswer[i] = jtf[k].getText().trim();

                   try {
                       if (Integer.parseInt(studentAnswer[i]) == answer[k]) {
                           //将string字符串类型转换为integer整数类型
                           correctAnswer++;
                       }
                   } catch (NumberFormatException u) {

                   }
               }

               int score = 100 * (int) correctAnswer / max;

               String s = "共50道题\n";
               s = s + "答对" + correctAnswer + "道题\n";
               s = s + "答错" + (max - correctAnswer) + "道题\n";
               s = s + "成绩" + String.format("%d", score) + "分\n";
               s = s + "正确率:" + correctAnswer * 100 / 50 + "%\n";
               //  Object[] options ={ "确定", "取消" };  //自定义按钮上的文字
               JOptionPane.showMessageDialog(null, s, "本次答题情况", JOptionPane.ERROR_MESSAGE);
               JOptionPane.showMessageDialog(null, "即将返回首页面", "提示", JOptionPane.ERROR_MESSAGE);
               // JOptionPane.showOptionDialog(null, "即将返回首页面", "提示",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
               // 数据库连接信息
               String url = "jdbc:mysql://localhost:3306/demo1?serverTimezone=UTC";
               String username = "root";
               String password = "123456";

               Connection connection = null;
               PreparedStatement statement = null;

               try {
                   // 注册数据库驱动
                   Class.forName("com.mysql.cj.jdbc.Driver");

                   // 建立数据库连接
                   connection = DriverManager.getConnection(url, username, password);

                   // 创建预编译的SQL语句
                   String sql = "INSERT INTO test (test, answer) VALUES (?, ?)";
                   statement = connection.prepareStatement(sql);

                   // 遍历数据,并执行插入操作
                   for (int i = 0; i < Test.length; i++) {
                       statement.setString(1, Test[i]);
                       statement.setString(2, String.valueOf(answer[i]));
                       statement.executeUpdate();
                   }

               } catch (Exception exception) {
                   exception.printStackTrace();
               } finally {
                   // 关闭资源
                   try {
                       if (statement != null) {
                           statement.close();
                       }
                       if (connection != null) {
                           connection.close();
                       }
                   } catch (SQLException exception) {
                       exception.printStackTrace();
                   }
               }
               new MainMenu();
               setVisible(false);
               dispose();
           }
       });

       //首页
       b1.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               care.first(pnl3);
               count = 1;
               JLpage.setText("第" + count + "页");
           }
       });

       //前一页
       b2.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               care.previous(pnl3);
               if (count > 1 && count <= 5) {
                   count--;
               } else {
                   count = 5;
               }
               JLpage.setText("第" + count + "页");
           }
       });

       //下一页
       b3.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {

               care.next(pnl3);
               if (count >= 1 && count < 5) {
                   count++;
               } else {
                   count = 1;
               }
               JLpage.setText("第" + count + "页");
           }
       });

       //尾页
       b4.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               care.last(pnl3);
               count = 5;
               JLpage.setText("第" + count + "页");
           }
       });
       b5.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               JOptionPane.showMessageDialog(null, "是否返回首页", "提示", JOptionPane.ERROR_MESSAGE);
               new MainMenu();
               setVisible(false);
               dispose();
           }
       });
   }


   public Test() {

       this.setTitle("开始答题");
       this.setSize(600, 500);
       this.setLocationRelativeTo(null);  //将此窗口置于屏幕的中央
       this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       this.setLayout(new BorderLayout(20, 20)); //设置 各组件间水平垂直间隔为20像素

       JLpage.setText("第" + count + "页");

       pnl2.setLayout(new FlowLayout());
       pnl2.add(JLgross);
       pnl2.add(JLpage);//页数
       pnl2.add(jl1);

       //pnl2.add(pnl1);
       pnl2.add(JB1);

       this.add(pnl2, BorderLayout.NORTH);

       pnl3.setLayout(care);
       this.add(pnl3, BorderLayout.CENTER);
       pnl3.setBorder(BorderFactory.createLoweredBevelBorder()); //边框

       for (int i = 0, k = 0; i < 5; i++) { // 总页数改为5页
           JPanel p = new JPanel();
           p.setLayout(new GridLayout(5, 2, 20, 20)); //网格布局5行2列,水平垂直间距都设为20像素
           for (int j = 0; j < 10; j++) { // 每页输出10道题目
               a = (int) (Math.random() * 100 + 1);
               b = (int) (Math.random() * 100 + 1);
               String random = "";
               String[] doc = {"+", "-"};
               int index = (int) (Math.random() * doc.length);
               random = doc[index];
               if (random == "-") {
                   if (a > b) {
                       jl[k] = new JLabel(a + random + b + "=");
                       jl[k].setFont(new Font("黑体", Font.BOLD, 20));
                       p.add(jl[k]);
                       answer[k] = a - b;
                       Test[testNum] = a + "-" + b;
                       testNum++;
                   } else {
                       jl[k] = new JLabel(b + random + a + "=");
                       jl[k].setFont(new Font("黑体", Font.BOLD, 20));
                       p.add(jl[k]);
                       answer[k] = b - a;
                       Test[testNum] = b + "-" + a;
                       testNum++;
                   }
               } else if (random == "+") {
                   jl[k] = new JLabel(a + random + b + "=");
                   jl[k].setFont(new Font("黑体", Font.BOLD, 20));
                   p.add(jl[k]);
                   answer[k] = a + b;
                   Test[testNum] = a + "+" + b;
                   testNum++;
               }
               jtf[k] = new JTextField(6);
               p.add(jtf[k]);
               jtf[k].setText(null);
               k++;
           }
           pnl3.add(p);
       }

       pnl4.setLayout(new GridLayout(1, 4));

       pnl4.add(b1);
       pnl4.add(b2);
       pnl4.add(b3);
       pnl4.add(b4);
       pnl4.add(b5);
       this.add(pnl4, BorderLayout.SOUTH);

       //美化界面
       try {
           UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
       } catch (Exception e) {
           e.printStackTrace();
       }

       Event();

       setVisible(true);

   }

}

  

posted @ 2023-11-19 18:47  漏网鲨鱼  阅读(9)  评论(0)    收藏  举报