|
|
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 ProfileFrame extends JFrame {
|
|
|
private User user;
|
|
|
private UserService userService;
|
|
|
private JPanel mainPanel;
|
|
|
private JLabel usernameValue;
|
|
|
|
|
|
public ProfileFrame(User user, UserService userService) {
|
|
|
this.user = user;
|
|
|
this.userService = userService;
|
|
|
addWindowListener(new java.awt.event.WindowAdapter() {
|
|
|
@Override
|
|
|
public void windowClosing(java.awt.event.WindowEvent windowEvent) {
|
|
|
MainFrame mainFrame = new MainFrame(user, userService);
|
|
|
mainFrame.setVisible(true);
|
|
|
}
|
|
|
});
|
|
|
InitUI();
|
|
|
}
|
|
|
|
|
|
private void InitUI() {
|
|
|
setTitle("个人资料");
|
|
|
setSize(400, 300);
|
|
|
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
|
|
|
setLocationRelativeTo(null);
|
|
|
setResizable(false);
|
|
|
|
|
|
// 创建主面板
|
|
|
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(3, 2, 10, 10));
|
|
|
JLabel usernameLabel = new JLabel("用户名:");
|
|
|
usernameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
|
|
|
usernameValue = new JLabel(user.getUsername());
|
|
|
usernameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
|
|
|
infoPanel.add(usernameLabel);
|
|
|
infoPanel.add(usernameValue);
|
|
|
JLabel mailLabel = new JLabel("QQ邮箱:");
|
|
|
mailLabel.setFont(new Font("微软雅黑", Font.PLAIN, 14));
|
|
|
JLabel mailValue = new JLabel(user.getEmail());
|
|
|
infoPanel.add(mailLabel);
|
|
|
infoPanel.add(mailValue);
|
|
|
mainPanel.add(infoPanel, BorderLayout.CENTER);
|
|
|
|
|
|
//三个按钮
|
|
|
JPanel buttonPanel = new JPanel(new FlowLayout());
|
|
|
JButton returnToMainButton = new JButton("返回");
|
|
|
returnToMainButton.addActionListener(new returnToMainButtonListener());
|
|
|
|
|
|
JButton changePasswordButton = new JButton("修改密码");
|
|
|
changePasswordButton.addActionListener(e -> openChangePasswordFrame());
|
|
|
|
|
|
JButton changeUsernameButton = new JButton("更改用户名");
|
|
|
changeUsernameButton.addActionListener(new ChangeUsernameButtonListener());
|
|
|
|
|
|
buttonPanel.add(returnToMainButton);
|
|
|
buttonPanel.add(changePasswordButton);
|
|
|
buttonPanel.add(changeUsernameButton);
|
|
|
|
|
|
mainPanel.add(buttonPanel, BorderLayout.SOUTH);
|
|
|
|
|
|
add(mainPanel);
|
|
|
}
|
|
|
|
|
|
private class returnToMainButtonListener implements ActionListener {
|
|
|
@Override
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
MainFrame mainFrame = new MainFrame(user, userService);
|
|
|
mainFrame.setVisible(true);
|
|
|
dispose();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void openChangePasswordFrame() {
|
|
|
ChangePasswordFrame changePasswordFrame = new ChangePasswordFrame(user, userService, this);
|
|
|
changePasswordFrame.setVisible(true);
|
|
|
this.setVisible(false);
|
|
|
}
|
|
|
|
|
|
private class ChangeUsernameButtonListener implements ActionListener {
|
|
|
@Override
|
|
|
public void actionPerformed(ActionEvent e ) {
|
|
|
String newUsername = JOptionPane.showInputDialog(ProfileFrame.this, "请输入您的新用户名:", "修改用户名", JOptionPane.QUESTION_MESSAGE);
|
|
|
|
|
|
if (newUsername == null ) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
newUsername = newUsername.trim();
|
|
|
if (newUsername.isEmpty()) {
|
|
|
JOptionPane.showMessageDialog( ProfileFrame.this, "用户名不能为空!", "错误", JOptionPane.ERROR_MESSAGE);
|
|
|
return;
|
|
|
}
|
|
|
if (newUsername.equals(user.getUsername())) {
|
|
|
JOptionPane.showMessageDialog( ProfileFrame.this, "新用户名与原用户名相同!", "提示", JOptionPane.ERROR_MESSAGE);
|
|
|
return;
|
|
|
}
|
|
|
int confirm = JOptionPane.showConfirmDialog(ProfileFrame.this,"确定要修改用户名为\"" + newUsername + "\"吗?", "修改用户名", JOptionPane.YES_NO_OPTION);
|
|
|
if (confirm == JOptionPane.YES_OPTION) {
|
|
|
String oldUsername = user.getUsername();
|
|
|
|
|
|
boolean updated = userService.updateUsername(user.getEmail(), newUsername);
|
|
|
if (updated) {
|
|
|
usernameValue.setText(newUsername);
|
|
|
JOptionPane.showMessageDialog(ProfileFrame.this, "修改成功!", "成功", JOptionPane.INFORMATION_MESSAGE);
|
|
|
}
|
|
|
else {
|
|
|
user.setUsername(oldUsername);
|
|
|
JOptionPane.showMessageDialog(ProfileFrame.this, "修改失败!", "错误", JOptionPane.ERROR_MESSAGE);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|