最近想自己写一个jPasswordField,但是看了jdk里面关于jPasswordField里面的代码还是不得要领,怎么使输入的字符变成*号?我是想知道jPasswordField里面是怎么处理的。
解决方案 »
- 提交过去的数据,怎么获取哦?
- Java使用Windows风格后字体大小问题
- 关于socket通信的问题,客户端发完一个请求,得到相应的回应后,再发另外一个请求时,结果服务器端读不到
- 【不是广告】帮朋友发,欲转让《Java思想》第三版,9成新,4折,最好是南京朋友!mail:[email protected]
- 一道关于垃圾收集器的面试题
- URLNAME,谢谢
- 怎样让用户直接下载?(在线等待各位大虾的帮助)
- 请教一条Sql语句!! 在线等候!!
- 请问那位大侠有JPadPro的正式版及注册码
- 构造函数 main函数 静态函数 有什么区别啊
- 一个框架结构中的2个页面之间可以相互传参数吗?
- 数据库连接在程序中能够成功,但是在jsp中却不行的问题?
直接显示回显字符是你所定义的C字符
package javax.swing;import javax.swing.text.*;
import javax.swing.plaf.*;
import javax.accessibility.*;import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
public class JPasswordField extends JTextField { public JPasswordField() {
this(null,null,0);
} public JPasswordField(String text) {
this(null, text, 0);
} public JPasswordField(int columns) {
this(null, null, columns);
} public JPasswordField(String text, int columns) {
this(null, text, columns);
} public JPasswordField(Document doc, String txt, int columns) {
super(doc, txt, columns);
echoChar = '*';
// We could either leave this on, which wouldn't be secure,
// or obscure the composted text, which essentially makes displaying
// it useless. Therefore, we turn off input methods.
enableInputMethods(false);
} public String getUIClassID() {
return uiClassID;
} public char getEchoChar() {
return echoChar;
} public void setEchoChar(char c) {
echoChar = c;
repaint();
revalidate();
} public boolean echoCharIsSet() {
return echoChar != 0;
} public void cut() {
if (getClientProperty("JPasswordField.cutCopyAllowed") != Boolean.TRUE) {
UIManager.getLookAndFeel().provideErrorFeedback(this);
} else {
super.cut();
}
} public void copy() {
if (getClientProperty("JPasswordField.cutCopyAllowed") != Boolean.TRUE) {
UIManager.getLookAndFeel().provideErrorFeedback(this);
} else {
super.copy();
}
} @Deprecated
public String getText() {
return super.getText();
} @Deprecated
public String getText(int offs, int len) throws BadLocationException {
return super.getText(offs, len);
} public char[] getPassword() {
Document doc = getDocument();
Segment txt = new Segment();
try {
doc.getText(0, doc.getLength(), txt); // use the non-String API
} catch (BadLocationException e) {
return null;
}
char[] retValue = new char[txt.count];
System.arraycopy(txt.array, txt.offset, retValue, 0, txt.count);
return retValue;
} private void writeObject(ObjectOutputStream s) throws IOException {
s.defaultWriteObject();
if (getUIClassID().equals(uiClassID)) {
byte count = JComponent.getWriteObjCounter(this);
JComponent.setWriteObjCounter(this, --count);
if (count == 0 && ui != null) {
ui.installUI(this);
}
}
} private static final String uiClassID = "PasswordFieldUI"; private char echoChar; protected String paramString() {
return super.paramString() +
",echoChar=" + echoChar;
} public AccessibleContext getAccessibleContext() {
if (accessibleContext == null) {
accessibleContext = new AccessibleJPasswordField();
}
return accessibleContext;
} protected class AccessibleJPasswordField extends AccessibleJTextField {
public AccessibleRole getAccessibleRole() {
return AccessibleRole.PASSWORD_TEXT;
}
}
}
super(doc, txt, columns);
echoChar = '*';
// We could either leave this on, which wouldn't be secure,
// or obscure the composted text, which essentially makes displaying
// it useless. Therefore, we turn off input methods.
enableInputMethods(false);
}
public void setEchoChar(char c) {
echoChar = c;
repaint();
revalidate();
}