| ¶Ô±ÈÐÂÎļþ |
| | |
| | | package user; |
| | | |
| | | import javax.swing.*; |
| | | import java.awt.*; |
| | | import java.awt.event.ActionEvent; |
| | | import java.awt.event.ActionListener; |
| | | import javax.swing.border.EmptyBorder; |
| | | import publicway.buttonset; |
| | | import javax.swing.event.DocumentEvent; |
| | | import javax.swing.event.DocumentListener; |
| | | |
| | | public class ZhaohuiMima extends JDialog { |
| | | |
| | | private JTextField emailField; |
| | | private JTextField codeField; |
| | | private JPasswordField passwordField; |
| | | private JPasswordField confirmPasswordField; |
| | | private JButton getCodeButton; |
| | | private JButton saveButton; |
| | | private JLabel tipLabel; |
| | | private Timer timer; |
| | | private int countdown = 60; |
| | | private String generatedCode; // åå¨çæçéªè¯ç |
| | | |
| | | // 主é¢é¢è² (åè Denglu.java) |
| | | private final Color THEME_COLOR = new Color(46, 139, 87); |
| | | private final Color THEME_HOVER_COLOR = new Color(30, 107, 69); |
| | | |
| | | public ZhaohuiMima(Frame owner) { |
| | | super(owner, "æ¾åå¯ç ", true); |
| | | setSize(400, 350); |
| | | setLocationRelativeTo(owner); |
| | | setResizable(false); |
| | | |
| | | initializeUI(); |
| | | } |
| | | |
| | | private void initializeUI() { |
| | | JPanel mainPanel = new JPanel(); |
| | | mainPanel.setLayout(new GridBagLayout()); |
| | | mainPanel.setBorder(new EmptyBorder(20, 20, 20, 20)); |
| | | mainPanel.setBackground(Color.WHITE); |
| | | setContentPane(mainPanel); |
| | | |
| | | GridBagConstraints gbc = new GridBagConstraints(); |
| | | gbc.insets = new Insets(5, 5, 5, 5); |
| | | gbc.fill = GridBagConstraints.HORIZONTAL; |
| | | |
| | | // é®ç®± |
| | | JLabel emailLabel = new JLabel("é®ç®±:"); |
| | | gbc.gridx = 0; |
| | | gbc.gridy = 0; |
| | | gbc.weightx = 0.1; |
| | | mainPanel.add(emailLabel, gbc); |
| | | |
| | | emailField = new JTextField(); |
| | | styleTextField(emailField); |
| | | gbc.gridx = 1; |
| | | gbc.gridy = 0; |
| | | gbc.weightx = 0.9; |
| | | gbc.gridwidth = 2; |
| | | mainPanel.add(emailField, gbc); |
| | | |
| | | // éªè¯ç |
| | | JLabel codeLabel = new JLabel("éªè¯ç :"); |
| | | gbc.gridx = 0; |
| | | gbc.gridy = 1; |
| | | gbc.gridwidth = 1; |
| | | gbc.weightx = 0.1; |
| | | mainPanel.add(codeLabel, gbc); |
| | | |
| | | codeField = new JTextField(); |
| | | styleTextField(codeField); |
| | | gbc.gridx = 1; |
| | | gbc.gridy = 1; |
| | | gbc.weightx = 0.6; |
| | | mainPanel.add(codeField, gbc); |
| | | |
| | | getCodeButton = new JButton("è·åéªè¯ç "); |
| | | styleButton(getCodeButton); |
| | | getCodeButton.setFont(new Font("微软é
é»", Font.PLAIN, 12)); |
| | | // è°æ´æé®é«åº¦ä»¥å¹é
ææ¬æ¡ |
| | | getCodeButton.setPreferredSize(new Dimension(100, 38)); |
| | | gbc.gridx = 2; |
| | | gbc.gridy = 1; |
| | | gbc.weightx = 0.3; |
| | | mainPanel.add(getCodeButton, gbc); |
| | | |
| | | // æ°å¯ç |
| | | JLabel passwordLabel = new JLabel("æ°å¯ç :"); |
| | | gbc.gridx = 0; |
| | | gbc.gridy = 2; |
| | | gbc.weightx = 0.1; |
| | | mainPanel.add(passwordLabel, gbc); |
| | | |
| | | JPanel passwordPanel = createPasswordPanel(); |
| | | passwordField = (JPasswordField) passwordPanel.getComponent(0); |
| | | gbc.gridx = 1; |
| | | gbc.gridy = 2; |
| | | gbc.gridwidth = 2; |
| | | gbc.weightx = 0.9; |
| | | mainPanel.add(passwordPanel, gbc); |
| | | |
| | | // 确认å¯ç |
| | | JLabel confirmPasswordLabel = new JLabel("确认å¯ç :"); |
| | | gbc.gridx = 0; |
| | | gbc.gridy = 3; |
| | | gbc.gridwidth = 1; |
| | | gbc.weightx = 0.1; |
| | | mainPanel.add(confirmPasswordLabel, gbc); |
| | | |
| | | JPanel confirmPasswordPanel = createPasswordPanel(); |
| | | confirmPasswordField = (JPasswordField) confirmPasswordPanel.getComponent(0); |
| | | gbc.gridx = 1; |
| | | gbc.gridy = 3; |
| | | gbc.gridwidth = 2; |
| | | gbc.weightx = 0.9; |
| | | mainPanel.add(confirmPasswordPanel, gbc); |
| | | |
| | | // æç¤ºä¿¡æ¯ |
| | | tipLabel = new JLabel("å¯ç 6-25个å符"); |
| | | tipLabel.setForeground(Color.GRAY); |
| | | tipLabel.setFont(new Font("微软é
é»", Font.PLAIN, 10)); |
| | | gbc.gridx = 1; |
| | | gbc.gridy = 4; |
| | | gbc.gridwidth = 2; |
| | | mainPanel.add(tipLabel, gbc); |
| | | |
| | | // ä¿åæé® |
| | | saveButton = buttonset.createStyledButton("ä¿åå¯ç ", THEME_COLOR); |
| | | gbc.gridx = 0; |
| | | gbc.gridy = 5; |
| | | gbc.gridwidth = 3; |
| | | gbc.insets = new Insets(20, 5, 5, 5); |
| | | mainPanel.add(saveButton, gbc); |
| | | |
| | | // äºä»¶çå¬ |
| | | setupEvents(); |
| | | } |
| | | |
| | | private void styleTextField(JTextField field) { |
| | | field.setPreferredSize(new Dimension(0, 38)); // 宽度ç±GridBagLayoutæ§å¶ï¼é«åº¦è®¾ä¸º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)); |
| | | } |
| | | |
| | | private void styleButton(JButton button) { |
| | | button.setBackground(THEME_COLOR); |
| | | button.setForeground(Color.WHITE); |
| | | button.setFocusPainted(false); |
| | | button.setBorderPainted(false); |
| | | button.setFont(new Font("微软é
é»", Font.BOLD, 14)); |
| | | button.setCursor(new Cursor(Cursor.HAND_CURSOR)); |
| | | |
| | | button.addMouseListener(new java.awt.event.MouseAdapter() { |
| | | public void mouseEntered(java.awt.event.MouseEvent evt) { |
| | | if (button.isEnabled()) { |
| | | button.setBackground(THEME_HOVER_COLOR); |
| | | } |
| | | } |
| | | public void mouseExited(java.awt.event.MouseEvent evt) { |
| | | if (button.isEnabled()) { |
| | | button.setBackground(THEME_COLOR); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | private void setupEvents() { |
| | | getCodeButton.addActionListener(new ActionListener() { |
| | | @Override |
| | | public void actionPerformed(ActionEvent e) { |
| | | String email = emailField.getText().trim(); |
| | | if (email.isEmpty()) { |
| | | JOptionPane.showMessageDialog(ZhaohuiMima.this, "请è¾å
¥é®ç®±", "æç¤º", JOptionPane.WARNING_MESSAGE); |
| | | return; |
| | | } |
| | | |
| | | // è¿éåºè¯¥æ·»å åééªè¯ç çé»è¾ |
| | | // 模æçæéªè¯ç |
| | | generatedCode = String.valueOf((int)((Math.random() * 9 + 1) * 100000)); |
| | | |
| | | startCountdown(); |
| | | JOptionPane.showMessageDialog(ZhaohuiMima.this, "éªè¯ç å·²åé: " + generatedCode, "æç¤º", JOptionPane.INFORMATION_MESSAGE); |
| | | } |
| | | }); |
| | | |
| | | // 宿¶éªè¯å¯ç ä¸è´æ§ |
| | | DocumentListener passwordListener = new DocumentListener() { |
| | | @Override |
| | | public void insertUpdate(DocumentEvent e) { checkPasswordMatch(); } |
| | | @Override |
| | | public void removeUpdate(DocumentEvent e) { checkPasswordMatch(); } |
| | | @Override |
| | | public void changedUpdate(DocumentEvent e) { checkPasswordMatch(); } |
| | | }; |
| | | |
| | | passwordField.getDocument().addDocumentListener(passwordListener); |
| | | confirmPasswordField.getDocument().addDocumentListener(passwordListener); |
| | | |
| | | saveButton.addActionListener(new ActionListener() { |
| | | @Override |
| | | public void actionPerformed(ActionEvent e) { |
| | | savePassword(); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | private void checkPasswordMatch() { |
| | | String pass = new String(passwordField.getPassword()); |
| | | String confirm = new String(confirmPasswordField.getPassword()); |
| | | |
| | | if (pass.isEmpty()) { |
| | | tipLabel.setText("å¯ç 6-25个å符"); |
| | | tipLabel.setForeground(Color.GRAY); |
| | | return; |
| | | } |
| | | |
| | | // å
夿æ°å¯ç é¿åº¦ |
| | | if (pass.length() < 6) { |
| | | tipLabel.setText("å¯ç é¿åº¦ä¸è½å°äº6个å符"); |
| | | tipLabel.setForeground(Color.RED); |
| | | return; |
| | | } else if (pass.length() > 25) { |
| | | tipLabel.setText("å¯ç é¿åº¦ä¸è½è¶
è¿25个å符"); |
| | | tipLabel.setForeground(Color.RED); |
| | | return; |
| | | } |
| | | |
| | | // å¦æç¡®è®¤å¯ç ä¸ºç©ºï¼æç¤ºè¾å
¥ |
| | | if (confirm.isEmpty()) { |
| | | tipLabel.setText("å¯ç 6-25个å符"); |
| | | tipLabel.setForeground(Color.GRAY); |
| | | return; |
| | | } |
| | | |
| | | // åªæå½ç¡®è®¤å¯ç é¿åº¦åæ°å¯ç é¿åº¦ä¸è´æ¶ï¼æè¿è¡æ¯å¯¹ |
| | | if (pass.length() == confirm.length()) { |
| | | if (!pass.equals(confirm)) { |
| | | tipLabel.setText("两次è¾å
¥çå¯ç ä¸ä¸è´"); |
| | | tipLabel.setForeground(Color.RED); |
| | | } else { |
| | | tipLabel.setText("å¯ç ä¸è´"); |
| | | tipLabel.setForeground(new Color(46, 139, 87)); // Green |
| | | } |
| | | } else { |
| | | // é¿åº¦ä¸ä¸è´æ¶ï¼æ¢å¤é»è®¤æç¤ºï¼é¿å
å¨è¾å
¥è¿ç¨ä¸ä¸ç´æç¤ºä¸ä¸è´ |
| | | tipLabel.setText("å¯ç 6-25个å符"); |
| | | tipLabel.setForeground(Color.GRAY); |
| | | } |
| | | } |
| | | |
| | | private void startCountdown() { |
| | | getCodeButton.setEnabled(false); |
| | | getCodeButton.setBackground(Color.GRAY); |
| | | countdown = 60; |
| | | getCodeButton.setText(countdown + "ç§"); |
| | | |
| | | timer = new Timer(1000, new ActionListener() { |
| | | @Override |
| | | public void actionPerformed(ActionEvent e) { |
| | | countdown--; |
| | | if (countdown > 0) { |
| | | getCodeButton.setText(countdown + "ç§"); |
| | | } else { |
| | | timer.stop(); |
| | | getCodeButton.setText("è·åéªè¯ç "); |
| | | getCodeButton.setEnabled(true); |
| | | getCodeButton.setBackground(THEME_COLOR); |
| | | } |
| | | } |
| | | }); |
| | | timer.start(); |
| | | } |
| | | |
| | | private void savePassword() { |
| | | String email = emailField.getText().trim(); |
| | | String code = codeField.getText().trim(); |
| | | String password = new String(passwordField.getPassword()); |
| | | String confirmPassword = new String(confirmPasswordField.getPassword()); |
| | | |
| | | if (email.isEmpty()) { |
| | | JOptionPane.showMessageDialog(this, "请è¾å
¥é®ç®±", "é误", JOptionPane.ERROR_MESSAGE); |
| | | return; |
| | | } |
| | | if (code.isEmpty()) { |
| | | JOptionPane.showMessageDialog(this, "请è¾å
¥éªè¯ç ", "é误", JOptionPane.ERROR_MESSAGE); |
| | | return; |
| | | } |
| | | |
| | | // éªè¯éªè¯ç |
| | | if (generatedCode == null || !generatedCode.equals(code)) { |
| | | JOptionPane.showMessageDialog(this, "éªè¯ç é误", "é误", JOptionPane.ERROR_MESSAGE); |
| | | return; |
| | | } |
| | | |
| | | if (password.length() < 6) { |
| | | JOptionPane.showMessageDialog(this, "å¯ç é¿åº¦ä¸è½å°äº6个å符", "é误", JOptionPane.ERROR_MESSAGE); |
| | | return; |
| | | } |
| | | if (password.length() > 25) { |
| | | JOptionPane.showMessageDialog(this, "å¯ç é¿åº¦ä¸è½è¶
è¿25个å符", "é误", JOptionPane.ERROR_MESSAGE); |
| | | return; |
| | | } |
| | | if (!password.equals(confirmPassword)) { |
| | | JOptionPane.showMessageDialog(this, "两次è¾å
¥çå¯ç ä¸ä¸è´", "é误", JOptionPane.ERROR_MESSAGE); |
| | | return; |
| | | } |
| | | |
| | | // éªè¯é®ç®±æ¯å¦å¹é
(å¯éï¼æ ¹æ®éæ±) |
| | | String savedEmail = Usrdell.getProperty("email"); |
| | | if (savedEmail != null && !savedEmail.equals(email)) { |
| | | // è¿éå设å¿
é¡»å¹é
å·²åé®ç®±ï¼æè
è¿æ¯ä¸ä¸ªæ°åè½å
许任æé®ç®±æ¾åï¼ |
| | | // é常æ¾åå¯ç éè¦å¹é
è´¦å·ç»å®çé®ç®± |
| | | // 妿æ¬å°æ²¡æåé®ç®±ï¼æè
è¾å
¥çé®ç®±ä¸å¯¹ |
| | | // JOptionPane.showMessageDialog(this, "é®ç®±ä¸å¹é
", "é误", JOptionPane.ERROR_MESSAGE); |
| | | // return; |
| | | } |
| | | |
| | | // è¿éåºè¯¥éªè¯éªè¯ç æ¯å¦æ£ç¡® |
| | | // ... |
| | | |
| | | // ä¿åå¯ç |
| | | try { |
| | | Usrdell.updateProperty("password", password); |
| | | JOptionPane.showMessageDialog(this, "å¯ç ä¿®æ¹æå", "æå", JOptionPane.INFORMATION_MESSAGE); |
| | | dispose(); |
| | | } catch (Exception ex) { |
| | | JOptionPane.showMessageDialog(this, "ä¿å失败: " + ex.getMessage(), "é误", JOptionPane.ERROR_MESSAGE); |
| | | } |
| | | } |
| | | |
| | | private JPanel createPasswordPanel() { |
| | | JPanel panel = new JPanel(new BorderLayout()); |
| | | panel.setBackground(Color.WHITE); |
| | | |
| | | JPasswordField pf = new JPasswordField(); |
| | | styleTextField(pf); |
| | | // ç§»é¤å³è¾¹æ¡ï¼ä¸ºäºåç¼çæé®æ ç¼è¿æ¥ |
| | | pf.setBorder(BorderFactory.createCompoundBorder( |
| | | BorderFactory.createMatteBorder(1, 1, 1, 0, new Color(200, 200, 200)), |
| | | BorderFactory.createEmptyBorder(8, 10, 8, 5) |
| | | )); |
| | | |
| | | JToggleButton eyeButton = new JToggleButton("ð"); |
| | | eyeButton.setPreferredSize(new Dimension(40, 38)); |
| | | eyeButton.setBackground(Color.WHITE); |
| | | eyeButton.setBorder(BorderFactory.createMatteBorder(1, 0, 1, 1, new Color(200, 200, 200))); |
| | | eyeButton.setFocusPainted(false); |
| | | eyeButton.setCursor(new Cursor(Cursor.HAND_CURSOR)); |
| | | eyeButton.setFont(new Font("Segoe UI Symbol", Font.PLAIN, 16)); // ç¡®ä¿æ¯æUnicodeå符 |
| | | |
| | | eyeButton.addActionListener(e -> { |
| | | if (eyeButton.isSelected()) { |
| | | pf.setEchoChar((char) 0); // æ¾ç¤ºå¯ç |
| | | } else { |
| | | pf.setEchoChar('â¢'); // éèå¯ç (é»è®¤å符) |
| | | } |
| | | }); |
| | | |
| | | panel.add(pf, BorderLayout.CENTER); |
| | | panel.add(eyeButton, BorderLayout.EAST); |
| | | |
| | | return panel; |
| | | } |
| | | } |