?? passwordfield.java
字號:
package jp.co.ntl.swing;
import javax.swing.JTextField;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
public class PasswordField extends JTextField {
/**
*
*/
private static final long serialVersionUID = 1L;
public PasswordField(int figure) {
super(figure);
Document doc = new PasswordDisplayDocument(figure);
setDocument(doc);
enableInputMethods(false);
}
public PasswordField() {
super();
Document doc = new PasswordDisplayDocument(-1);
setDocument(doc);
enableInputMethods(false);
}
public String getText() {
try {
PasswordDisplayDocument doc = (PasswordDisplayDocument)getDocument();
return doc.getText(super.getText().length());
} catch (BadLocationException e) {
return "";
}
}
private class PasswordDisplayDocument extends PlainDocument {
/**
*
*/
private static final long serialVersionUID = 1L;
private int figure;
private PasswordDocument doc;
public PasswordDisplayDocument(int figure) {
this.figure = figure;
this.doc = new PasswordDocument(figure);
}
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
String aster;
if (str == null) {
return;
}
if (figure >= 0) {
// 嵟戝寘悢偑擖椡偝傟偰偄傞偲偒
if (figure < getLength()) {
return;
}
if (figure < getLength() + str.length()) {
return;
}
}
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c < ' ' || c > 0x7f) {
return;
}
}
doc.insertString(offs, str, a);
aster = "";
for (int i = 0; i < str.length(); i++) {
aster += "*";
}
super.insertString(offs, aster, a);
}
public String getText(int len) throws BadLocationException {
return doc.getText(len);
}
}
private class PasswordDocument extends PlainDocument {
/**
*
*/
private static final long serialVersionUID = 1L;
public PasswordDocument(int figure) {
}
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
super.insertString(offs, str, a);
}
public String getText(int len) throws BadLocationException {
return getText(0, len);
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -