Browse Source
1. 做的是这个迭代任务中的子任务,功能为:密码输入时可显示长度,再次打开时隐藏密码长度,统一显示为8位的加密字符,点击密码输入框做输入或编辑密码动作时,先清空密码,再输入内容,保存后生效 2. 写了一个UIPasswordField的子类,相比UIPasswordField增加了上述功能 3. 将目标场景中的密码框替换为新的UIPasswordFieldWithFixedLength 4. 修改一些导包问题feature/big-screen
Yvan
4 years ago
3 changed files with 134 additions and 7 deletions
@ -0,0 +1,106 @@
|
||||
package com.fr.design.gui.ipasswordfield; |
||||
|
||||
import com.fr.stable.StringUtils; |
||||
import org.jetbrains.annotations.NotNull; |
||||
|
||||
import javax.swing.text.Document; |
||||
import java.awt.event.KeyAdapter; |
||||
import java.awt.event.KeyEvent; |
||||
import java.awt.event.MouseAdapter; |
||||
import java.awt.event.MouseEvent; |
||||
|
||||
/** |
||||
* @author Yvan |
||||
* @version 10.0 |
||||
* Created by Yvan on 2020-08-11 |
||||
* 有固定长度的"*"回显的密码框,避免泄露密码长度 |
||||
*/ |
||||
public class UIPasswordFieldWithFixedLength extends UIPassWordField { |
||||
/** |
||||
* 展示密码,为固定8位长度的特殊字符"*"组成 |
||||
*/ |
||||
private static final String DISPLAY_PASSWORD = "********"; |
||||
|
||||
/** |
||||
* 实际密码 |
||||
*/ |
||||
private String realPassword; |
||||
|
||||
/** |
||||
* 用于判断是否清空密码 |
||||
*/ |
||||
private boolean clearPassword; |
||||
|
||||
public UIPasswordFieldWithFixedLength() { |
||||
this(null, null, 0); |
||||
} |
||||
|
||||
public UIPasswordFieldWithFixedLength(String text) { |
||||
this(null, text, 0); |
||||
} |
||||
|
||||
public UIPasswordFieldWithFixedLength(int columns) { |
||||
this(null, null, columns); |
||||
} |
||||
|
||||
public UIPasswordFieldWithFixedLength(String text, int columns) { |
||||
this(null, text, columns); |
||||
} |
||||
|
||||
public UIPasswordFieldWithFixedLength(Document doc, String txt, int columns) { |
||||
super(doc, txt, columns); |
||||
initRealPassword(txt); |
||||
} |
||||
|
||||
/** |
||||
* 为realPassword赋初值并添加一个鼠标单击事件 |
||||
*/ |
||||
public void initRealPassword(String text) { |
||||
this.realPassword = text == null ? StringUtils.EMPTY : text; |
||||
this.clearPassword = true; |
||||
addShowFixedLengthPasswordListener(); |
||||
} |
||||
|
||||
/** |
||||
* 当鼠标点击密码框,第一次做出键入动作时,清空显示密码与实际密码,用户需要重新输入密码 |
||||
*/ |
||||
private void addShowFixedLengthPasswordListener() { |
||||
this.addMouseListener(new MouseAdapter() { |
||||
@Override |
||||
public void mouseClicked(MouseEvent e) { |
||||
UIPasswordFieldWithFixedLength.this.clearPassword = true; |
||||
} |
||||
}); |
||||
this.addKeyListener(new KeyAdapter() { |
||||
@Override |
||||
public void keyPressed(KeyEvent e) { |
||||
if (clearPassword) { |
||||
UIPasswordFieldWithFixedLength.this.setText(StringUtils.EMPTY); |
||||
UIPasswordFieldWithFixedLength.this.clearPassword = false; |
||||
UIPasswordFieldWithFixedLength.this.updateUI(); |
||||
} |
||||
} |
||||
}); |
||||
} |
||||
|
||||
@Override |
||||
public void setText(@NotNull String t) { |
||||
this.realPassword = t; |
||||
// 看到代码中有些场景是将密码置为空字符串的,所以在这里加个判断
|
||||
if (StringUtils.isEmpty(t)) { |
||||
super.setText(t); |
||||
} else { |
||||
super.setText(DISPLAY_PASSWORD); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public char[] getPassword() { |
||||
//如果用户刚清空密码框,并输入了新密码,则返回输入内容,否则返回realPassword
|
||||
String text = new String(super.getPassword()); |
||||
if (!StringUtils.isEmpty(text) && StringUtils.isEmpty(realPassword)) { |
||||
return text.toCharArray(); |
||||
} |
||||
return realPassword.toCharArray(); |
||||
} |
||||
} |
Loading…
Reference in new issue