@ -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,24 @@
|
||||
|
||||
package model;
|
||||
|
||||
public class User {
|
||||
private String email;
|
||||
private String password;
|
||||
private String username;
|
||||
|
||||
// 修改构造函数,添加 username 参数
|
||||
public User(String email, String password, String username) {
|
||||
this.email = email;
|
||||
this.password = password;
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
// 保留原有无参数构造函数,用于旧数据兼容
|
||||
public User(String email, String password) {
|
||||
this(email, password, email); // 默认用户名为邮箱
|
||||
}
|
||||
|
||||
public String getEmail() { return email; }
|
||||
public String getPassword() { return password; }
|
||||
public String getUsername() { return username; }
|
||||
}
|
||||
@ -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,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