You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Math_Learning/src/View/Main_frame.java

157 lines
5.8 KiB

package View;
import Service.Exam_service;
import Service.User_service;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main_frame extends JFrame {
private User_service userService;
private String email;
private JComboBox<String> typeComboBox;
private JTextField countField;
public Main_frame(User_service userService, String email) {
this.userService = userService;
this.email = email;
initializeUI();
}
private void initializeUI() {
setTitle("主界面");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 400);
setLocationRelativeTo(null);
setResizable(false);
// 主面板
JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
// 标题
JLabel titleLabel = new JLabel("数学学习软件", JLabel.CENTER);
titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
mainPanel.add(titleLabel, BorderLayout.NORTH);
// 设置面板
JPanel settingPanel = new JPanel(new GridLayout(2, 2, 10, 10));
settingPanel.add(new JLabel("选择难度:"));
typeComboBox = new JComboBox<>(new String[]{"小学", "初中", "高中"});
settingPanel.add(typeComboBox);
settingPanel.add(new JLabel("题目数量:"));
countField = new JTextField("10");
settingPanel.add(countField);
mainPanel.add(settingPanel, BorderLayout.CENTER);
// 按钮面板
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 0));
JButton startButton = new JButton("开始做题");
JButton changePasswordButton = new JButton("修改密码");
JButton logoutButton = new JButton("退出登录");
JButton UnregisterButton = new JButton("删除账号");
buttonPanel.add(startButton);
buttonPanel.add(changePasswordButton);
buttonPanel.add(logoutButton);
buttonPanel.add(UnregisterButton);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
// 添加事件监听器
startButton.addActionListener(new StartListener());
changePasswordButton.addActionListener(e -> openChangePasswordFrame());
logoutButton.addActionListener(e -> openLoginFrame());
UnregisterButton.addActionListener(e-> UnregisterOperation());
add(mainPanel);
}
private class StartListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String type = (String) typeComboBox.getSelectedItem();
String countText = countField.getText().trim();
if (countText.isEmpty()) {
JOptionPane.showMessageDialog(Main_frame.this,
"请输入题目数量", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
int count;
try {
count = Integer.parseInt(countText);
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(Main_frame.this,
"题目数量必须是数字", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
if (count < 10 || count > 30) {
JOptionPane.showMessageDialog(Main_frame.this,
"题目数量必须在10-30之间", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
// 创建考试服务
Exam_service examService = new Exam_service(count, type, userService.find_user(email).get_id());
openExamFrame(examService);
}
}
private void openExamFrame(Exam_service examService) {
Exam_frame examFrame = new Exam_frame(examService, userService, email);
examFrame.setVisible(true);
this.dispose();
}
private void openChangePasswordFrame() {
JPasswordField oldPasswordField = new JPasswordField();
JPasswordField newPasswordField = new JPasswordField();
JPasswordField confirmPasswordField = new JPasswordField();
JPanel panel = new JPanel(new GridLayout(3, 2));
panel.add(new JLabel("原密码:"));
panel.add(oldPasswordField);
panel.add(new JLabel("新密码:"));
panel.add(newPasswordField);
panel.add(new JLabel("确认新密码:"));
panel.add(confirmPasswordField);
int result = JOptionPane.showConfirmDialog(this, panel, "修改密码", JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
String oldPassword = new String(oldPasswordField.getPassword());
String newPassword = new String(newPasswordField.getPassword());
String confirmPassword = new String(confirmPasswordField.getPassword());
if (!newPassword.equals(confirmPassword)) {
JOptionPane.showMessageDialog(this,
"两次输入的新密码不一致", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
String changeResult = userService.change_pwd(email, oldPassword, newPassword);
JOptionPane.showMessageDialog(this, changeResult, "修改密码", JOptionPane.INFORMATION_MESSAGE);
}
}
private void openLoginFrame() {
Login_frame loginFrame = new Login_frame(userService);
loginFrame.setVisible(true);
this.dispose();
}
private void UnregisterOperation(){
String result=userService.Unregister(email);
JOptionPane.showMessageDialog(this, result, "删除账号", JOptionPane.INFORMATION_MESSAGE);
openLoginFrame();
}
}