|
|
|
|
@ -0,0 +1,120 @@
|
|
|
|
|
package mathlearning.ui;
|
|
|
|
|
|
|
|
|
|
import mathlearning.model.User;
|
|
|
|
|
import mathlearning.service.UserService;
|
|
|
|
|
|
|
|
|
|
import javax.swing.*;
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
|
|
|
|
|
|
public class ChangePasswordFrame extends JFrame {
|
|
|
|
|
private User user;
|
|
|
|
|
private UserService userService;
|
|
|
|
|
private ProfileFrame profileFrame;
|
|
|
|
|
private JPasswordField oldPasswordField;
|
|
|
|
|
private JPasswordField newPasswordField;
|
|
|
|
|
private JPasswordField confirmPasswordField;
|
|
|
|
|
|
|
|
|
|
public ChangePasswordFrame(User user, UserService userService, ProfileFrame profileFrame) {
|
|
|
|
|
this.user = user;
|
|
|
|
|
this.userService = userService;
|
|
|
|
|
this.profileFrame = profileFrame;
|
|
|
|
|
addWindowListener(new java.awt.event.WindowAdapter() {
|
|
|
|
|
@Override
|
|
|
|
|
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
|
|
|
|
|
profileFrame.setVisible(true);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
InitUI();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InitUI() {
|
|
|
|
|
setTitle("修改密码");
|
|
|
|
|
setSize(400, 300);
|
|
|
|
|
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
|
|
|
|
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 infoPanel = new JPanel(new GridLayout(4, 2, 10, 10));
|
|
|
|
|
JLabel oldPasswordLabel = new JLabel("请输入旧密码:");
|
|
|
|
|
oldPasswordLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
|
|
|
|
|
oldPasswordField = new JPasswordField();
|
|
|
|
|
infoPanel.add(oldPasswordLabel);
|
|
|
|
|
infoPanel.add(oldPasswordField);
|
|
|
|
|
JLabel newPasswordLabel = new JLabel("请输入新密码:");
|
|
|
|
|
newPasswordLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
|
|
|
|
|
newPasswordField = new JPasswordField();
|
|
|
|
|
infoPanel.add(newPasswordLabel);
|
|
|
|
|
infoPanel.add(newPasswordField);
|
|
|
|
|
JLabel confirmPasswordLabel = new JLabel("请再次输入新密码:");
|
|
|
|
|
confirmPasswordLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
|
|
|
|
|
confirmPasswordField = new JPasswordField();
|
|
|
|
|
infoPanel.add(confirmPasswordLabel);
|
|
|
|
|
infoPanel.add(confirmPasswordField);
|
|
|
|
|
mainPanel.add(infoPanel, BorderLayout.CENTER);
|
|
|
|
|
|
|
|
|
|
//按钮
|
|
|
|
|
JPanel buttonPanel = new JPanel(new FlowLayout());
|
|
|
|
|
JButton changeButton = new JButton("修改");
|
|
|
|
|
changeButton.addActionListener(new ChangeButtonListener());
|
|
|
|
|
JButton cancelButton = new JButton("取消并返回");
|
|
|
|
|
cancelButton.addActionListener(e -> returnToProfileFrame());
|
|
|
|
|
|
|
|
|
|
buttonPanel.add(changeButton);
|
|
|
|
|
buttonPanel.add(cancelButton);
|
|
|
|
|
|
|
|
|
|
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
|
|
|
|
|
|
|
|
|
|
add(mainPanel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void returnToProfileFrame() {
|
|
|
|
|
profileFrame.setVisible(true);
|
|
|
|
|
dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class ChangeButtonListener implements ActionListener {
|
|
|
|
|
@Override
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
String oldPassword = new String(oldPasswordField.getPassword());
|
|
|
|
|
String newPassword = new String(newPasswordField.getPassword());
|
|
|
|
|
String confirmPassword = new String(confirmPasswordField.getPassword());
|
|
|
|
|
|
|
|
|
|
if (!newPassword.equals(confirmPassword)) {
|
|
|
|
|
JOptionPane.showMessageDialog(ChangePasswordFrame.this, "两次输入的密码不相同!", "错误", JOptionPane.ERROR_MESSAGE );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int changed = userService.changePassword(user.getEmail(), oldPassword, newPassword);
|
|
|
|
|
|
|
|
|
|
if (changed == 1) {
|
|
|
|
|
JOptionPane.showMessageDialog(ChangePasswordFrame.this, "修改失败!用户账户异常!", "错误", JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
else if (changed == 2) {
|
|
|
|
|
JOptionPane.showMessageDialog(ChangePasswordFrame.this, "修改失败!旧密码输入有误!", "错误", JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
return;
|
|
|
|
|
} else if (changed == 3) {
|
|
|
|
|
JOptionPane.showMessageDialog(ChangePasswordFrame.this, "修改失败!新密码的格式有误!密码必须为6-10位,且包含大小写字母和数字", "错误", JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
return;
|
|
|
|
|
} else if (changed == 4) {
|
|
|
|
|
JOptionPane.showMessageDialog(ChangePasswordFrame.this, "修改失败!旧密码与新密码一致!", "错误", JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
JOptionPane.showMessageDialog(ChangePasswordFrame.this, "修改成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
|
|
|
|
|
}
|
|
|
|
|
returnToProfileFrame();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|