package set;
|
|
import javax.swing.*;
|
import java.awt.*;
|
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionListener;
|
import java.awt.event.MouseAdapter;
|
import java.awt.event.MouseEvent;
|
import javax.swing.event.DocumentEvent;
|
import javax.swing.event.DocumentListener;
|
import user.Usrdell;
|
|
public class xiugaimima extends JDialog {
|
private static final long serialVersionUID = 1L;
|
private JPasswordField oldPasswordField;
|
private JPasswordField newPasswordField;
|
private JPasswordField confirmPasswordField;
|
private JButton saveButton;
|
private JButton cancelButton;
|
private JLabel errorLabel;
|
private final Color THEME_COLOR = new Color(46, 139, 87);
|
|
public xiugaimima(Frame owner) {
|
super(owner, "修改密码", true);
|
initializeUI();
|
}
|
|
private void initializeUI() {
|
setLayout(new BorderLayout());
|
setSize(400, 350);
|
setLocationRelativeTo(getOwner());
|
setResizable(false);
|
|
JPanel mainPanel = new JPanel(new GridBagLayout());
|
mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
|
mainPanel.setBackground(Color.WHITE);
|
|
GridBagConstraints gbc = new GridBagConstraints();
|
gbc.insets = new Insets(10, 10, 10, 10);
|
gbc.fill = GridBagConstraints.HORIZONTAL;
|
|
// 原密码
|
gbc.gridx = 0;
|
gbc.gridy = 0;
|
gbc.weightx = 0;
|
mainPanel.add(new JLabel("原密码:"), gbc);
|
|
oldPasswordField = createStyledPasswordField();
|
gbc.gridx = 1;
|
gbc.gridy = 0;
|
gbc.weightx = 1.0;
|
mainPanel.add(oldPasswordField, gbc);
|
|
// 新密码
|
gbc.gridx = 0;
|
gbc.gridy = 1;
|
gbc.weightx = 0;
|
mainPanel.add(new JLabel("新密码:"), gbc);
|
|
JPanel newPasswordPanel = createPasswordPanelWithEye(newPasswordField = createStyledPasswordField());
|
gbc.gridx = 1;
|
gbc.gridy = 1;
|
gbc.weightx = 1.0;
|
mainPanel.add(newPasswordPanel, gbc);
|
|
// 确认密码
|
gbc.gridx = 0;
|
gbc.gridy = 2;
|
gbc.weightx = 0;
|
mainPanel.add(new JLabel("确认密码:"), gbc);
|
|
JPanel confirmPasswordPanel = createPasswordPanelWithEye(confirmPasswordField = createStyledPasswordField());
|
gbc.gridx = 1;
|
gbc.gridy = 2;
|
gbc.weightx = 1.0;
|
mainPanel.add(confirmPasswordPanel, gbc);
|
|
// 错误提示信息
|
errorLabel = new JLabel("密码长度不能小于6个字符");
|
errorLabel.setForeground(Color.GRAY);
|
errorLabel.setFont(new Font("PingFang SC", Font.PLAIN, 12));
|
gbc.gridx = 1;
|
gbc.gridy = 3;
|
gbc.gridwidth = 2;
|
gbc.insets = new Insets(0, 10, 10, 10);
|
mainPanel.add(errorLabel, gbc);
|
|
// 添加密码输入监听
|
DocumentListener passwordListener = new DocumentListener() {
|
@Override
|
public void insertUpdate(DocumentEvent e) { checkPasswords(); }
|
@Override
|
public void removeUpdate(DocumentEvent e) { checkPasswords(); }
|
@Override
|
public void changedUpdate(DocumentEvent e) { checkPasswords(); }
|
};
|
newPasswordField.getDocument().addDocumentListener(passwordListener);
|
confirmPasswordField.getDocument().addDocumentListener(passwordListener);
|
|
add(mainPanel, BorderLayout.CENTER);
|
|
// 按钮面板
|
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10));
|
buttonPanel.setBackground(Color.WHITE);
|
|
saveButton = new JButton("保存");
|
saveButton.setBackground(THEME_COLOR);
|
saveButton.setForeground(Color.WHITE);
|
saveButton.setFocusPainted(false);
|
saveButton.setPreferredSize(new Dimension(100, 35));
|
saveButton.addActionListener(new ActionListener() {
|
@Override
|
public void actionPerformed(ActionEvent e) {
|
handleSave();
|
}
|
});
|
|
cancelButton = new JButton("取消");
|
cancelButton.setBackground(new Color(240, 240, 240));
|
cancelButton.setForeground(Color.BLACK);
|
cancelButton.setFocusPainted(false);
|
cancelButton.setPreferredSize(new Dimension(100, 35));
|
cancelButton.addActionListener(e -> dispose());
|
|
buttonPanel.add(saveButton);
|
buttonPanel.add(cancelButton);
|
|
add(buttonPanel, BorderLayout.SOUTH);
|
}
|
|
private JPasswordField createStyledPasswordField() {
|
JPasswordField field = new JPasswordField(15);
|
field.setPreferredSize(new Dimension(200, 38)); // 设置高度为38,与登录页面一致
|
field.setFont(new Font("PingFang SC", Font.PLAIN, 14));
|
field.setBorder(BorderFactory.createCompoundBorder(
|
BorderFactory.createLineBorder(new Color(200, 200, 200)),
|
BorderFactory.createEmptyBorder(8, 10, 8, 10)
|
));
|
field.setForeground(new Color(60, 60, 60));
|
return field;
|
}
|
|
private JPanel createPasswordPanelWithEye(JPasswordField passwordField) {
|
JPanel panel = new JPanel(new BorderLayout());
|
panel.setBackground(Color.WHITE);
|
// 将边框移动到 Panel 上,模拟文本框外观
|
panel.setBorder(BorderFactory.createCompoundBorder(
|
BorderFactory.createLineBorder(new Color(200, 200, 200)),
|
BorderFactory.createEmptyBorder(0, 0, 0, 5)
|
));
|
panel.setPreferredSize(new Dimension(200, 38));
|
|
// 移除 Field 的边框,使其融入 Panel
|
passwordField.setBorder(BorderFactory.createEmptyBorder(8, 10, 8, 0));
|
passwordField.setPreferredSize(null); // 让 BorderLayout 管理大小
|
|
panel.add(passwordField, BorderLayout.CENTER);
|
|
JLabel eyeLabel = new JLabel();
|
eyeLabel.setPreferredSize(new Dimension(30, 38));
|
eyeLabel.setHorizontalAlignment(SwingConstants.CENTER);
|
eyeLabel.setCursor(new Cursor(Cursor.HAND_CURSOR));
|
|
// 默认闭眼图标
|
eyeLabel.setText("👁");
|
eyeLabel.setForeground(Color.GRAY);
|
|
eyeLabel.addMouseListener(new MouseAdapter() {
|
private boolean isVisible = false;
|
|
@Override
|
public void mouseClicked(MouseEvent e) {
|
isVisible = !isVisible;
|
if (isVisible) {
|
passwordField.setEchoChar((char) 0);
|
eyeLabel.setForeground(THEME_COLOR);
|
} else {
|
passwordField.setEchoChar('•');
|
eyeLabel.setForeground(Color.GRAY);
|
}
|
}
|
});
|
|
panel.add(eyeLabel, BorderLayout.EAST);
|
return panel;
|
}
|
|
private void checkPasswords() {
|
String newPass = new String(newPasswordField.getPassword());
|
String confirmPass = new String(confirmPasswordField.getPassword());
|
|
// 默认提示
|
if (newPass.isEmpty() && confirmPass.isEmpty()) {
|
errorLabel.setText("密码长度不能小于6个字符");
|
errorLabel.setForeground(Color.GRAY);
|
return;
|
}
|
|
// 长度检查
|
if (newPass.length() > 0 && newPass.length() < 6) {
|
errorLabel.setText("密码长度不能小于6个字符");
|
errorLabel.setForeground(Color.RED);
|
return;
|
}
|
|
// 一致性检查
|
if (confirmPass.length() > 0) {
|
if (confirmPass.length() == newPass.length()) {
|
if (!newPass.equals(confirmPass)) {
|
errorLabel.setText("两次输入的新密码不一致");
|
errorLabel.setForeground(Color.RED);
|
} else {
|
errorLabel.setText(" "); // 密码一致且长度符合要求
|
}
|
} else if (confirmPass.length() < newPass.length()) {
|
// 正在输入中,如果之前有错误提示,可以清除或恢复默认
|
if (newPass.length() >= 6) {
|
errorLabel.setText(" ");
|
}
|
} else {
|
// 确认密码比新密码长,肯定不一致
|
errorLabel.setText("两次输入的新密码不一致");
|
errorLabel.setForeground(Color.RED);
|
}
|
} else {
|
// 确认密码为空,如果新密码符合长度,清除错误(或者显示默认提示)
|
if (newPass.length() >= 6) {
|
errorLabel.setText(" ");
|
}
|
}
|
}
|
|
private void handleSave() {
|
// 清除之前的错误信息
|
// errorLabel.setText(" "); // 不再强制清除,依赖 checkPasswords 的状态,或者重新检查
|
|
String oldPass = new String(oldPasswordField.getPassword());
|
String newPass = new String(newPasswordField.getPassword());
|
String confirmPass = new String(confirmPasswordField.getPassword());
|
|
if (oldPass.isEmpty() || newPass.isEmpty() || confirmPass.isEmpty()) {
|
errorLabel.setText("请填写所有字段");
|
errorLabel.setForeground(Color.RED);
|
return;
|
}
|
|
if (newPass.length() < 6) {
|
errorLabel.setText("新密码长度不能小于6个字符");
|
errorLabel.setForeground(Color.RED);
|
return;
|
}
|
|
String currentStoredPassword = Usrdell.getProperty("password");
|
if (currentStoredPassword == null) {
|
currentStoredPassword = "";
|
}
|
|
if (!oldPass.equals(currentStoredPassword)) {
|
errorLabel.setText("原密码错误");
|
errorLabel.setForeground(Color.RED);
|
return;
|
}
|
|
if (!newPass.equals(confirmPass)) {
|
errorLabel.setText("两次输入的新密码不一致");
|
errorLabel.setForeground(Color.RED);
|
return;
|
}
|
|
// 更新密码
|
Usrdell.updateProperty("password", newPass);
|
JOptionPane.showMessageDialog(this, "密码修改成功", "提示", JOptionPane.INFORMATION_MESSAGE);
|
dispose();
|
}
|
}
|