From 00f4e4fc6e53a26cf3dc67d57d8b00536634d707 Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期五, 26 十二月 2025 17:36:11 +0800
Subject: [PATCH] 优化了注册账号
---
src/denglu/RegistrationFrame.java | 370 ++++++++++++++++++++++++++++++++++++++--------------
1 files changed, 271 insertions(+), 99 deletions(-)
diff --git a/src/denglu/RegistrationFrame.java b/src/denglu/RegistrationFrame.java
index ace2dbf..919877d 100644
--- a/src/denglu/RegistrationFrame.java
+++ b/src/denglu/RegistrationFrame.java
@@ -1,10 +1,16 @@
package denglu;
+import ui.UIConfig;
+import login.EmailCodeSender;
+import login.UserRegister;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;
+import javax.swing.event.DocumentEvent;
+import javax.swing.event.DocumentListener;
+import java.util.Arrays;
public class RegistrationFrame extends JFrame {
private JTextField userField;
@@ -16,6 +22,7 @@
private JButton cancelButton;
private JButton sendCodeButton;
private JLabel userLabel, emailLabel, verificationCodeLabel, passLabel, confirmPassLabel;
+ private JLabel passwordHintLabel, matchStatusLabel, emailHintLabel;
// 涓婚棰滆壊
private final Color THEME_COLOR = new Color(46, 139, 87);
@@ -29,7 +36,6 @@
// 楠岃瘉鐮佺浉鍏�
private Timer countdownTimer;
private int countdownSeconds = 60;
- private String generatedVerificationCode;
public RegistrationFrame(Denglu parent, String languageCode) {
this.parentFrame = parent;
@@ -44,7 +50,8 @@
// 涓枃缈昏瘧
Map<String, String> zh = new HashMap<>();
- zh.put("title", "鐢ㄦ埛娉ㄥ唽");
+ zh.put("title", "鐢ㄦ埛娉ㄥ唽");
+ zh.put("window_title", "鏅鸿兘鍓茶崏绯荤粺 - 鐢ㄦ埛娉ㄥ唽");
zh.put("username_label", "鐢ㄦ埛鍚�");
zh.put("email_label", "閭鍦板潃");
zh.put("verification_code_label", "楠岃瘉鐮�");
@@ -66,7 +73,8 @@
// 鑻辨枃缈昏瘧
Map<String, String> en = new HashMap<>();
- en.put("title", "User Registration");
+ en.put("title", "User Registration");
+ en.put("window_title", "Smart Mowing System - User Registration");
en.put("username_label", "Username");
en.put("email_label", "Email Address");
en.put("verification_code_label", "Verification Code");
@@ -88,7 +96,8 @@
// 瑗跨彮鐗欒缈昏瘧
Map<String, String> es = new HashMap<>();
- es.put("title", "Registro de Usuario");
+ es.put("title", "Registro de Usuario");
+ es.put("window_title", "Sistema de Corte Inteligente - Registro de Usuario");
es.put("username_label", "Usuario");
es.put("email_label", "Correo Electr贸nico");
es.put("verification_code_label", "C贸digo de Verificaci贸n");
@@ -110,7 +119,8 @@
// 娉曡缈昏瘧
Map<String, String> fr = new HashMap<>();
- fr.put("title", "Inscription Utilisateur");
+ fr.put("title", "Inscription Utilisateur");
+ fr.put("window_title", "Syst猫me de Tonte Intelligent - Inscription");
fr.put("username_label", "Nom d'utilisateur");
fr.put("email_label", "Adresse Email");
fr.put("verification_code_label", "Code de V茅rification");
@@ -132,7 +142,8 @@
// 寰疯缈昏瘧
Map<String, String> de = new HashMap<>();
- de.put("title", "Benutzerregistrierung");
+ de.put("title", "Benutzerregistrierung");
+ de.put("window_title", "Intelligentes M盲hsystem - Registrierung");
de.put("username_label", "Benutzername");
de.put("email_label", "E-Mail-Adresse");
de.put("verification_code_label", "Verifizierungscode");
@@ -153,10 +164,22 @@
translations.put("de", de);
}
+ private String getTranslationValue(String languageCode, String key, String defaultValue) {
+ Map<String, String> translation = translations.get(languageCode);
+ if (translation != null) {
+ String value = translation.get(key);
+ if (value != null && !value.trim().isEmpty()) {
+ return value;
+ }
+ }
+ return defaultValue;
+ }
+
private void initializeUI() {
- setTitle("AutoMow - 鐢ㄦ埛娉ㄥ唽");
+ setTitle(getTranslationValue(currentLanguageCode, "window_title", "鐢ㄦ埛娉ㄥ唽"));
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- setSize(350, 500);
+ setSize(UIConfig.DIALOG_WIDTH, UIConfig.DIALOG_HEIGHT);
+ setMinimumSize(new Dimension(UIConfig.DIALOG_WIDTH, UIConfig.DIALOG_HEIGHT));
setLocationRelativeTo(parentFrame);
setResizable(false);
@@ -207,13 +230,23 @@
emailLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
emailField = new JTextField();
- emailField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 35));
emailField.setFont(new Font("PingFang SC", Font.PLAIN, 14));
emailField.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(new Color(200, 200, 200)),
BorderFactory.createEmptyBorder(5, 8, 5, 8)
));
- emailField.setAlignmentX(Component.LEFT_ALIGNMENT);
+
+ JPanel emailContainer = new JPanel(new BorderLayout());
+ emailContainer.setBackground(Color.WHITE);
+ emailContainer.setMaximumSize(new Dimension(Integer.MAX_VALUE, 35));
+ emailContainer.setAlignmentX(Component.LEFT_ALIGNMENT);
+ emailContainer.add(emailField, BorderLayout.CENTER);
+
+ emailHintLabel = new JLabel("");
+ emailHintLabel.setFont(new Font("PingFang SC", Font.PLAIN, 12));
+ emailHintLabel.setForeground(Color.RED);
+ emailHintLabel.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
+ emailContainer.add(emailHintLabel, BorderLayout.EAST);
// 楠岃瘉鐮� - 鏍囩宸﹀榻�
verificationCodeLabel = new JLabel("楠岃瘉鐮�");
@@ -263,13 +296,8 @@
passLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
passField = new JPasswordField();
- passField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 35));
passField.setFont(new Font("PingFang SC", Font.PLAIN, 14));
- passField.setBorder(BorderFactory.createCompoundBorder(
- BorderFactory.createLineBorder(new Color(200, 200, 200)),
- BorderFactory.createEmptyBorder(5, 8, 5, 8)
- ));
- passField.setAlignmentX(Component.LEFT_ALIGNMENT);
+ JPanel passPanel = createPasswordPanel(passField);
// 纭瀵嗙爜 - 鏍囩宸﹀榻�
confirmPassLabel = new JLabel("纭瀵嗙爜");
@@ -278,13 +306,20 @@
confirmPassLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
confirmPassField = new JPasswordField();
- confirmPassField.setMaximumSize(new Dimension(Integer.MAX_VALUE, 35));
confirmPassField.setFont(new Font("PingFang SC", Font.PLAIN, 14));
- confirmPassField.setBorder(BorderFactory.createCompoundBorder(
- BorderFactory.createLineBorder(new Color(200, 200, 200)),
- BorderFactory.createEmptyBorder(5, 8, 5, 8)
- ));
- confirmPassField.setAlignmentX(Component.LEFT_ALIGNMENT);
+ JPanel confirmPassPanel = createPasswordPanel(confirmPassField);
+
+ // 瀵嗙爜涓嶄竴鑷存彁绀�
+ matchStatusLabel = new JLabel(" ");
+ matchStatusLabel.setFont(new Font("PingFang SC", Font.PLAIN, 12));
+ matchStatusLabel.setForeground(Color.RED);
+ matchStatusLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
+
+ // 瀵嗙爜闀垮害鎻愮ず
+ passwordHintLabel = new JLabel(" ");
+ passwordHintLabel.setFont(new Font("PingFang SC", Font.PLAIN, 12));
+ passwordHintLabel.setForeground(Color.RED);
+ passwordHintLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
// 娉ㄥ唽鎸夐挳 - 鍗曠嫭涓�琛岋紝闀垮害涓庢枃鏈鐩稿悓
registerButton = new JButton("娉ㄥ唽");
@@ -325,7 +360,7 @@
formPanel.add(Box.createRigidArea(new Dimension(0, 10)));
formPanel.add(emailLabel);
formPanel.add(Box.createRigidArea(new Dimension(0, 3)));
- formPanel.add(emailField);
+ formPanel.add(emailContainer);
formPanel.add(Box.createRigidArea(new Dimension(0, 10)));
formPanel.add(verificationCodeLabel);
formPanel.add(Box.createRigidArea(new Dimension(0, 3)));
@@ -335,12 +370,16 @@
formPanel.add(Box.createRigidArea(new Dimension(0, 10)));
formPanel.add(passLabel);
formPanel.add(Box.createRigidArea(new Dimension(0, 3)));
- formPanel.add(passField);
+ formPanel.add(passPanel);
formPanel.add(Box.createRigidArea(new Dimension(0, 10)));
formPanel.add(confirmPassLabel);
formPanel.add(Box.createRigidArea(new Dimension(0, 3)));
- formPanel.add(confirmPassField);
- formPanel.add(Box.createRigidArea(new Dimension(0, 20)));
+ formPanel.add(confirmPassPanel);
+ formPanel.add(Box.createRigidArea(new Dimension(0, 5)));
+ formPanel.add(matchStatusLabel);
+ formPanel.add(Box.createRigidArea(new Dimension(0, 5)));
+ formPanel.add(passwordHintLabel);
+ formPanel.add(Box.createRigidArea(new Dimension(0, 5)));
formPanel.add(registerButton);
formPanel.add(Box.createRigidArea(new Dimension(0, 8)));
formPanel.add(cancelButton);
@@ -365,6 +404,61 @@
// 鍥炶溅閿敞鍐�
confirmPassField.addActionListener(e -> performRegistration());
+
+ // 瀵嗙爜涓�鑷存�ф鏌�
+ DocumentListener matchListener = new DocumentListener() {
+ private void checkMatch() {
+ char[] p1 = passField.getPassword();
+ char[] p2 = confirmPassField.getPassword();
+ if (p2.length > 0 && p2.length == p1.length) {
+ if (!Arrays.equals(p1, p2)) {
+ matchStatusLabel.setText(getTranslationValue(currentLanguageCode, "password_mismatch_error", "涓ゆ杈撳叆鐨勫瘑鐮佷笉涓�鑷�"));
+ } else {
+ matchStatusLabel.setText(" ");
+ }
+ } else {
+ matchStatusLabel.setText(" ");
+ }
+ }
+ @Override
+ public void insertUpdate(DocumentEvent e) { checkMatch(); }
+ @Override
+ public void removeUpdate(DocumentEvent e) { checkMatch(); }
+ @Override
+ public void changedUpdate(DocumentEvent e) { checkMatch(); }
+ };
+ confirmPassField.getDocument().addDocumentListener(matchListener);
+ passField.getDocument().addDocumentListener(matchListener);
+
+ // 瀵嗙爜闀垮害妫�鏌�
+ passField.getDocument().addDocumentListener(new DocumentListener() {
+ private void checkLength() {
+ char[] password = passField.getPassword();
+ if (password.length > 0 && password.length < 6) {
+ passwordHintLabel.setText(getTranslationValue(currentLanguageCode, "password_length_error", "瀵嗙爜闀垮害涓嶈兘灏戜簬6浣�"));
+ } else {
+ passwordHintLabel.setText(" ");
+ }
+ }
+ public void insertUpdate(DocumentEvent e) { checkLength(); }
+ public void removeUpdate(DocumentEvent e) { checkLength(); }
+ public void changedUpdate(DocumentEvent e) { checkLength(); }
+ });
+
+ // 閭鏍煎紡妫�鏌�
+ emailField.getDocument().addDocumentListener(new DocumentListener() {
+ private void validate() {
+ String email = emailField.getText().trim();
+ if (!email.isEmpty() && !isValidEmail(email)) {
+ emailHintLabel.setText(getTranslationValue(currentLanguageCode, "invalid_email_error", "閭鏍煎紡閿欒"));
+ } else {
+ emailHintLabel.setText("");
+ }
+ }
+ public void insertUpdate(DocumentEvent e) { validate(); }
+ public void removeUpdate(DocumentEvent e) { validate(); }
+ public void changedUpdate(DocumentEvent e) { validate(); }
+ });
}
private void updateLanguage(String languageCode) {
@@ -372,15 +466,18 @@
if (translation != null) {
// 鏇存柊鎵�鏈夌晫闈㈡枃鏈�
- setTitle("AutoMow - " + translation.get("title"));
- userLabel.setText(translation.get("username_label"));
- emailLabel.setText(translation.get("email_label"));
- verificationCodeLabel.setText(translation.get("verification_code_label"));
- passLabel.setText(translation.get("password_label"));
- confirmPassLabel.setText(translation.get("confirm_password_label"));
- registerButton.setText(translation.get("register_button"));
- cancelButton.setText(translation.get("cancel_button"));
- sendCodeButton.setText(translation.get("send_code_button"));
+ setTitle(getTranslationValue(languageCode, "window_title", getTitle()));
+ userLabel.setText(getTranslationValue(languageCode, "username_label", userLabel.getText()));
+ emailLabel.setText(getTranslationValue(languageCode, "email_label", emailLabel.getText()));
+ verificationCodeLabel.setText(getTranslationValue(languageCode, "verification_code_label", verificationCodeLabel.getText()));
+ passLabel.setText(getTranslationValue(languageCode, "password_label", passLabel.getText()));
+ confirmPassLabel.setText(getTranslationValue(languageCode, "confirm_password_label", confirmPassLabel.getText()));
+ registerButton.setText(getTranslationValue(languageCode, "register_button", registerButton.getText()));
+ cancelButton.setText(getTranslationValue(languageCode, "cancel_button", cancelButton.getText()));
+ sendCodeButton.setText(getTranslationValue(languageCode, "send_code_button", sendCodeButton.getText()));
+ if (passwordHintLabel != null) {
+ passwordHintLabel.setText(getTranslationValue(languageCode, "password_length_error", passwordHintLabel.getText()));
+ }
}
}
@@ -419,35 +516,51 @@
return;
}
- // 鐢熸垚闅忔満楠岃瘉鐮侊紙瀹為檯搴旂敤涓簲璇ラ�氳繃閭欢鍙戦�侊級
- generatedVerificationCode = generateRandomCode();
+ // 绂佺敤鎸夐挳锛岄槻姝㈤噸澶嶇偣鍑�
+ sendCodeButton.setEnabled(false);
- // 鍦ㄥ疄闄呭簲鐢ㄤ腑锛岃繖閲屽簲璇ヨ皟鐢ㄩ偖浠跺彂閫佹湇鍔�
- // 杩欓噷浠呮ā鎷熷彂閫佽繃绋�
- System.out.println("楠岃瘉鐮� " + generatedVerificationCode + " 宸插彂閫佸埌 " + email);
-
- // 鑾峰彇褰撳墠璇█鐨勭炕璇�
- Map<String, String> translationMap = translations.get(currentLanguageCode);
- if (translationMap != null) {
- JOptionPane.showMessageDialog(this,
- translationMap.get("verification_code_sent"),
- "鎻愮ず",
- JOptionPane.INFORMATION_MESSAGE);
- } else {
- JOptionPane.showMessageDialog(this,
- "楠岃瘉鐮佸凡鍙戦�佸埌鎮ㄧ殑閭",
- "鎻愮ず",
- JOptionPane.INFORMATION_MESSAGE);
- }
-
- // 寮�濮嬪�掕鏃�
- startCountdown();
- }
-
- // 鐢熸垚闅忔満楠岃瘉鐮�
- private String generateRandomCode() {
- // 鐢熸垚6浣嶉殢鏈烘暟瀛�
- return String.valueOf((int)((Math.random() * 9 + 1) * 100000));
+ // 鍦ㄦ柊绾跨▼涓皟鐢ˋPI锛岄伩鍏嶉樆濉濽I
+ new Thread(() -> {
+ try {
+ // 璋冪敤閭欢楠岃瘉鐮佸彂閫丄PI
+ EmailCodeSender.EmailCodeResponse response = EmailCodeSender.sendEmailCode(email);
+
+ // 鍦‥DT绾跨▼涓洿鏂癠I
+ SwingUtilities.invokeLater(() -> {
+ if (response.isSuccess()) {
+ // 鍙戦�佹垚鍔燂紝鏄剧ず鎻愮ず骞跺紑濮嬪�掕鏃�
+ Map<String, String> translationMap = translations.get(currentLanguageCode);
+ String successMessage = translationMap != null ?
+ translationMap.get("verification_code_sent") : "楠岃瘉鐮佸凡鍙戦�佸埌鎮ㄧ殑閭";
+ JOptionPane.showMessageDialog(this,
+ successMessage,
+ "鎻愮ず",
+ JOptionPane.INFORMATION_MESSAGE);
+
+ // 寮�濮�60绉掑�掕鏃�
+ startCountdown();
+ } else {
+ // 鍙戦�佸け璐ワ紝鏄剧ず閿欒淇℃伅骞舵仮澶嶆寜閽�
+ sendCodeButton.setEnabled(true);
+ String errorMessage = response.getMessage() != null ?
+ response.getMessage() : "楠岃瘉鐮佸彂閫佸け璐�";
+ JOptionPane.showMessageDialog(this,
+ errorMessage,
+ "閿欒",
+ JOptionPane.ERROR_MESSAGE);
+ }
+ });
+ } catch (Exception e) {
+ // 寮傚父澶勭悊锛屾仮澶嶆寜閽苟鏄剧ず閿欒
+ SwingUtilities.invokeLater(() -> {
+ sendCodeButton.setEnabled(true);
+ JOptionPane.showMessageDialog(this,
+ "鍙戦�侀獙璇佺爜鏃跺彂鐢熼敊璇�: " + e.getMessage(),
+ "閿欒",
+ JOptionPane.ERROR_MESSAGE);
+ });
+ }
+ }).start();
}
// 寮�濮嬪�掕鏃�
@@ -488,17 +601,19 @@
}
private void performRegistration() {
- String username = userField.getText().trim();
+ String nickname = userField.getText().trim();
String email = emailField.getText().trim();
- String verificationCode = verificationCodeField.getText().trim();
- String password = new String(passField.getPassword());
- String confirmPassword = new String(confirmPassField.getPassword());
+ String code = verificationCodeField.getText().trim();
+
+ // 鑾峰彇瀵嗙爜瀛楁鐨勫�硷紙涓庣櫥褰曢〉闈繚鎸佷竴鑷寸殑澶勭悊鏂瑰紡锛�
+ String password = new String(passField.getPassword()).trim();
+ String confirmPassword = new String(confirmPassField.getPassword()).trim();
// 鑾峰彇褰撳墠璇█鐨勭炕璇�
Map<String, String> translationMap = translations.get(currentLanguageCode);
// 杈撳叆楠岃瘉
- if (username.isEmpty() || email.isEmpty() || verificationCode.isEmpty() ||
+ if (nickname.isEmpty() || email.isEmpty() || code.isEmpty() ||
password.isEmpty() || confirmPassword.isEmpty()) {
String errorMessage = translationMap != null ?
translationMap.get("empty_fields_error") : "璇峰~鍐欐墍鏈夊瓧娈�";
@@ -535,19 +650,7 @@
return;
}
- // 楠岃瘉楠岃瘉鐮�
- if (!verificationCode.equals(generatedVerificationCode)) {
- String errorMessage = translationMap != null ?
- translationMap.get("invalid_verification_code") : "楠岃瘉鐮侀敊璇�";
- JOptionPane.showMessageDialog(this,
- errorMessage,
- "杈撳叆閿欒",
- JOptionPane.WARNING_MESSAGE);
- verificationCodeField.setText("");
- verificationCodeField.requestFocus();
- return;
- }
-
+ // 楠岃瘉涓ゆ杈撳叆鐨勫瘑鐮佹槸鍚︿竴鑷�
if (!password.equals(confirmPassword)) {
String errorMessage = translationMap != null ?
translationMap.get("password_mismatch_error") : "涓ゆ杈撳叆鐨勫瘑鐮佷笉涓�鑷�";
@@ -561,25 +664,51 @@
return;
}
- // 淇濆瓨鐢ㄦ埛淇℃伅
- UserChuShiHua.updateProperty("userName", username);
- UserChuShiHua.updateProperty("email", email);
- UserChuShiHua.updateProperty("password", password);
+ // 绂佺敤娉ㄥ唽鎸夐挳锛岄槻姝㈤噸澶嶆彁浜�
+ registerButton.setEnabled(false);
- String successMessage = translationMap != null ?
- translationMap.get("success_message") : "娉ㄥ唽鎴愬姛锛�";
- JOptionPane.showMessageDialog(this,
- successMessage,
- "鎴愬姛",
- JOptionPane.INFORMATION_MESSAGE);
-
- dispose();
-
- // 鑷姩濉厖鐧诲綍琛ㄥ崟
- if (parentFrame != null) {
- // 杩欓噷闇�瑕佽闂埗绐楀彛鐨剈sernameField锛屼絾鐢变簬灏佽鎬э紝鎴戜滑鍙兘闇�瑕佹坊鍔犲叕鍏辨柟娉�
- // 鏆傛椂鐣欑┖锛屽彲浠ユ牴鎹渶瑕佸疄鐜�
- }
+ // 鍦ㄦ柊绾跨▼涓皟鐢ˋPI锛岄伩鍏嶉樆濉濽I
+ new Thread(() -> {
+ try {
+ // 璋冪敤鐢ㄦ埛娉ㄥ唽API
+ UserRegister.RegisterResponse response = UserRegister.register(email, password, code, nickname,confirmPassword);
+
+ // 鍦‥DT绾跨▼涓洿鏂癠I
+ SwingUtilities.invokeLater(() -> {
+ registerButton.setEnabled(true);
+
+ if (response.isSuccess()) {
+ // 娉ㄥ唽鎴愬姛锛屾樉绀烘垚鍔熸彁绀�
+ String successMessage = translationMap != null ?
+ translationMap.get("success_message") : "娉ㄥ唽鎴愬姛锛�";
+ JOptionPane.showMessageDialog(this,
+ successMessage,
+ "鎴愬姛",
+ JOptionPane.INFORMATION_MESSAGE);
+
+ // 鍏抽棴娉ㄥ唽绐楀彛
+ dispose();
+ } else {
+ // 娉ㄥ唽澶辫触锛屾樉绀洪敊璇俊鎭�
+ String errorMessage = response.getMessage() != null ?
+ response.getMessage() : "娉ㄥ唽澶辫触";
+ JOptionPane.showMessageDialog(this,
+ errorMessage,
+ "娉ㄥ唽澶辫触",
+ JOptionPane.ERROR_MESSAGE);
+ }
+ });
+ } catch (Exception e) {
+ // 寮傚父澶勭悊锛屾仮澶嶆寜閽苟鏄剧ず閿欒
+ SwingUtilities.invokeLater(() -> {
+ registerButton.setEnabled(true);
+ JOptionPane.showMessageDialog(this,
+ "娉ㄥ唽鏃跺彂鐢熼敊璇�: " + e.getMessage(),
+ "閿欒",
+ JOptionPane.ERROR_MESSAGE);
+ });
+ }
+ }).start();
}
@Override
@@ -590,4 +719,47 @@
}
super.dispose();
}
+
+ private JPanel createPasswordPanel(JPasswordField passField) {
+ JPanel panel = new JPanel(new BorderLayout());
+ panel.setBackground(Color.WHITE);
+ panel.setBorder(BorderFactory.createCompoundBorder(
+ BorderFactory.createLineBorder(new Color(200, 200, 200)),
+ BorderFactory.createEmptyBorder(1, 1, 1, 1)
+ ));
+ panel.setMaximumSize(new Dimension(Integer.MAX_VALUE, 35));
+ panel.setAlignmentX(Component.LEFT_ALIGNMENT);
+
+ passField.setBorder(BorderFactory.createEmptyBorder(4, 8, 4, 8));
+ panel.add(passField, BorderLayout.CENTER);
+
+ JButton toggleBtn = new JButton();
+ try {
+ ImageIcon icon = new ImageIcon("image/look.png");
+ if (icon.getIconWidth() > 0) {
+ Image img = icon.getImage().getScaledInstance(20, 20, Image.SCALE_SMOOTH);
+ toggleBtn.setIcon(new ImageIcon(img));
+ } else {
+ toggleBtn.setText("馃憗");
+ }
+ } catch (Exception e) {
+ toggleBtn.setText("馃憗");
+ }
+
+ toggleBtn.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
+ toggleBtn.setContentAreaFilled(false);
+ toggleBtn.setFocusPainted(false);
+ toggleBtn.setCursor(new Cursor(Cursor.HAND_CURSOR));
+
+ toggleBtn.addActionListener(e -> {
+ if (passField.getEchoChar() != 0) {
+ passField.setEchoChar((char) 0);
+ } else {
+ passField.setEchoChar('鈥�');
+ }
+ });
+
+ panel.add(toggleBtn, BorderLayout.EAST);
+ return panel;
+ }
}
\ No newline at end of file
--
Gitblit v1.10.0