张世豪
15 小时以前 2eea735fd4ddf0ae047687780271ef3962d256cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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();
    }
}