最近想自己写一个jPasswordField,但是看了jdk里面关于jPasswordField里面的代码还是不得要领,怎么使输入的字符变成*号?我是想知道jPasswordField里面是怎么处理的。

解决方案 »

  1.   

    监听KeyPressed函数,对每个键入的字母用setText显示为“*”,然后用StringBuffer记录实际输入的内容
      

  2.   

    可以使用这个方法public synchronized void setEchoChar(char c)
    直接显示回显字符是你所定义的C字符
      

  3.   

    也可以定义一个PlainDocument类(继承它),复写insertString方法显示"*"
      

  4.   

    这个是JPasswordField的源代码,可是我找不到什么地方是规定只显示'*'的。
    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;
            }
        }
    }
      

  5.   

    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 void setEchoChar(char c) {
            echoChar = c;
            repaint();
            revalidate();
        }