11.19每日总结

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
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 @   漏网鲨鱼  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示