@ -0,0 +1,7 @@
|
||||
import controller.AppController;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
new AppController(); // 启动控制器
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,14 @@
|
||||
package model;
|
||||
|
||||
public class Question {
|
||||
private String text;
|
||||
private String userAnswer;
|
||||
|
||||
public Question(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public String getText() { return text; }
|
||||
public String getUserAnswer() { return userAnswer; }
|
||||
public void setUserAnswer(String ans) { this.userAnswer = ans; }
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
package model;
|
||||
|
||||
public class User {
|
||||
private String email;
|
||||
private String password;
|
||||
|
||||
public User(String email, String password) {
|
||||
this.email = email;
|
||||
this.password = password;
|
||||
}
|
||||
public String getEmail() { return email; }
|
||||
public String getPassword() { return password; }
|
||||
}
|
||||
@ -0,0 +1,39 @@
|
||||
package ui;
|
||||
|
||||
import controller.AppController;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
|
||||
public class DifficultySelectionPanel extends JPanel {
|
||||
private AppController controller;
|
||||
|
||||
public DifficultySelectionPanel(AppController controller) {
|
||||
this.controller = controller;
|
||||
setLayout(new GridLayout(3, 1, 10, 10));
|
||||
setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
|
||||
|
||||
// 创建难度按钮
|
||||
JButton elementaryBtn = createDifficultyButton("小学", "小学");
|
||||
JButton middleBtn = createDifficultyButton("初中", "初中");
|
||||
JButton highBtn = createDifficultyButton("高中", "高中");
|
||||
|
||||
add(elementaryBtn);
|
||||
add(middleBtn);
|
||||
add(highBtn);
|
||||
}
|
||||
|
||||
private JButton createDifficultyButton(String text, String level) {
|
||||
JButton btn = new JButton(text);
|
||||
btn.setFont(new Font("微软雅黑", Font.BOLD, 16));
|
||||
btn.setPreferredSize(new Dimension(200, 40));
|
||||
btn.addActionListener(new ActionListener() {
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
controller.handleDifficultySelection(level);
|
||||
}
|
||||
});
|
||||
return btn;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,57 @@
|
||||
package ui;
|
||||
|
||||
import controller.AppController;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* 登录界面
|
||||
*/
|
||||
public class LoginPanel extends JPanel {
|
||||
private JTextField emailField;
|
||||
private JPasswordField passwordField;
|
||||
|
||||
public LoginPanel(AppController controller) {
|
||||
setLayout(new GridBagLayout());
|
||||
GridBagConstraints gbc = new GridBagConstraints();
|
||||
gbc.insets = new Insets(8, 8, 8, 8);
|
||||
gbc.fill = GridBagConstraints.HORIZONTAL;
|
||||
|
||||
JLabel title = new JLabel("学生数学学习系统", JLabel.CENTER);
|
||||
title.setFont(new Font("微软雅黑", Font.BOLD, 22));
|
||||
gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 2;
|
||||
add(title, gbc);
|
||||
|
||||
gbc.gridwidth = 1;
|
||||
gbc.gridx = 0; gbc.gridy = 1;
|
||||
add(new JLabel("邮箱:"), gbc);
|
||||
|
||||
gbc.gridx = 1;
|
||||
emailField = new JTextField(20);
|
||||
add(emailField, gbc);
|
||||
|
||||
gbc.gridx = 0; gbc.gridy = 2;
|
||||
add(new JLabel("密码:"), gbc);
|
||||
|
||||
gbc.gridx = 1;
|
||||
passwordField = new JPasswordField(20);
|
||||
add(passwordField, gbc);
|
||||
|
||||
JButton loginButton = new JButton("登录");
|
||||
gbc.gridx = 0; gbc.gridy = 3;
|
||||
add(loginButton, gbc);
|
||||
|
||||
JButton registerButton = new JButton("注册");
|
||||
gbc.gridx = 1;
|
||||
add(registerButton, gbc);
|
||||
|
||||
loginButton.addActionListener(e -> {
|
||||
String email = emailField.getText().trim();
|
||||
String password = new String(passwordField.getPassword());
|
||||
controller.handleLogin(email, password);
|
||||
});
|
||||
|
||||
registerButton.addActionListener(e -> controller.showRegisterPanel());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
package util;
|
||||
|
||||
import model.User;
|
||||
import java.io.*;
|
||||
import java.util.*;
|
||||
|
||||
public class FileUtil {
|
||||
public static Map<String, User> loadUsers(String fileName) {
|
||||
Map<String, User> users = new HashMap<>();
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
String[] parts = line.split(",");
|
||||
users.put(parts[0], new User(parts[0], parts[1]));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// 首次运行时文件可能不存在
|
||||
}
|
||||
return users;
|
||||
}
|
||||
|
||||
public static void saveUsers(Map<String, User> users, String fileName) {
|
||||
try (PrintWriter pw = new PrintWriter(new FileWriter(fileName))) {
|
||||
for (User u : users.values()) {
|
||||
pw.println(u.getEmail() + "," + u.getPassword());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,88 @@
|
||||
package view;
|
||||
|
||||
import controller.AppController;
|
||||
import model.QuestionGenerator;
|
||||
import model.QuestionGenerator.Question;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.util.List;
|
||||
|
||||
public class QuizPanel extends JPanel {
|
||||
private List<Question> questions;
|
||||
private int currentIndex = 0;
|
||||
private int correctCount = 0;
|
||||
private ButtonGroup group;
|
||||
private JLabel questionLabel;
|
||||
private JRadioButton[] options;
|
||||
private JButton nextButton;
|
||||
private AppController controller;
|
||||
|
||||
public QuizPanel(AppController controller, String level, int count) {
|
||||
this.controller = controller;
|
||||
this.questions = QuestionGenerator.generateQuestions(level, count);
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
questionLabel = new JLabel("", JLabel.CENTER);
|
||||
questionLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
|
||||
add(questionLabel, BorderLayout.NORTH);
|
||||
|
||||
JPanel optionsPanel = new JPanel(new GridLayout(4, 1, 5, 5));
|
||||
group = new ButtonGroup();
|
||||
options = new JRadioButton[4];
|
||||
for (int i = 0; i < 4; i++) {
|
||||
options[i] = new JRadioButton();
|
||||
group.add(options[i]);
|
||||
optionsPanel.add(options[i]);
|
||||
}
|
||||
add(optionsPanel, BorderLayout.CENTER);
|
||||
|
||||
nextButton = new JButton("下一题");
|
||||
add(nextButton, BorderLayout.SOUTH);
|
||||
|
||||
nextButton.addActionListener(e -> checkAndNext());
|
||||
showQuestion();
|
||||
}
|
||||
|
||||
private void showQuestion() {
|
||||
if (currentIndex >= questions.size()) {
|
||||
showResult();
|
||||
return;
|
||||
}
|
||||
Question q = questions.get(currentIndex);
|
||||
questionLabel.setText("第 " + (currentIndex + 1) + " 题:" + q.questionText);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
options[i].setText(q.options[i]);
|
||||
options[i].setSelected(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkAndNext() {
|
||||
for (JRadioButton r : options) {
|
||||
if (r.isSelected() && r.getText().equals(questions.get(currentIndex).options[0])) {
|
||||
correctCount++;
|
||||
}
|
||||
}
|
||||
currentIndex++;
|
||||
showQuestion();
|
||||
}
|
||||
|
||||
private void showResult() {
|
||||
removeAll();
|
||||
setLayout(new GridLayout(3, 1, 10, 10));
|
||||
JLabel result = new JLabel("你的得分:" + (100 * correctCount / questions.size()) + " 分", JLabel.CENTER);
|
||||
result.setFont(new Font("微软雅黑", Font.BOLD, 22));
|
||||
add(result);
|
||||
|
||||
JButton retry = new JButton("再做一份");
|
||||
JButton exit = new JButton("退出");
|
||||
add(retry);
|
||||
add(exit);
|
||||
|
||||
retry.addActionListener(e -> controller.showLoginPanel());
|
||||
exit.addActionListener(e -> System.exit(0));
|
||||
|
||||
revalidate();
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,19 @@
|
||||
package view;
|
||||
|
||||
import controller.AppController;
|
||||
import model.User;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class ResultPanel extends JPanel {
|
||||
public ResultPanel(AppController controller, User user, int score) {
|
||||
setLayout(new BorderLayout());
|
||||
JLabel lbl = new JLabel("用户:" + user.getEmail() + ",得分:" + score + " 分", JLabel.CENTER);
|
||||
lbl.setFont(new Font("微软雅黑", Font.BOLD, 20));
|
||||
add(lbl, BorderLayout.CENTER);
|
||||
|
||||
JButton btnBack = new JButton("返回登录");
|
||||
btnBack.addActionListener(e -> controller.showLoginPanel());
|
||||
add(btnBack, BorderLayout.SOUTH);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue