JTextArea中的密码 [英] Password in JTextArea

查看:113
本文介绍了JTextArea中的密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户键入时是否可以使用JTextArea隐藏文本?

Is there any way using JTextArea to hide the text when user types??

类似的密码.

.

密码:

以及该行中的任何用户类型都不应该可见.

in last line and whatever user types in that line shouldn't be visible.

我尝试使用setForeground方法将字体颜色设置为textarea颜色,该颜色使文本不可见,但允许用户复制和粘贴.

I have tried with setForeground method to set font colour to textarea colour which makes text invisible but allows user to copy and paste.

有没有解决方法,或者我该如何实现?

is there any workaround or how can i achieve this??

请帮助

推荐答案

这就是我在评论中谈论的内容.请注意,这只是一个简单的示例(可能想安全地存储密码,等等),但是应该让您入门.

Here's what I was talking about in comments. Do note that this is just a quick example (might wanna store the password securely, etc.), but should get you started.

此代码仅将DocumentFilter放在JTextArea上,并且永远不允许在其Document中放入密码字符.而是将它们重新路由到其他地方.这与期望敏感输入的控制台的行为类似.

This code simply puts a DocumentFilter on the JTextArea and never allows password characters to be put into it's Document. Instead they are re-routed somewhere else. This is similar behavior to that of a console expecting sensitive input.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.PlainDocument;

public class CapturePassword extends JFrame {

    private JScrollPane scroll;
    private JTextArea textArea;
    private JToggleButton expectPassword;
    private StringBuilder password; // you would of course use something else

    public CapturePassword() {
        setLayout(new BorderLayout());

        password = new StringBuilder();

        textArea = new JTextArea();
        scroll = new JScrollPane(textArea);
        add(scroll);

        expectPassword = new JToggleButton("Capture password");
        add(expectPassword, BorderLayout.PAGE_END);

        expectPassword.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (expectPassword.isSelected()) {
                    capture(true);
                } else {
                    capture(false);
                    JOptionPane.showConfirmDialog(
                            CapturePassword.this, 
                            "Captured password: " + password.toString(), 
                            "Password!", JOptionPane.DEFAULT_OPTION, 
                            JOptionPane.INFORMATION_MESSAGE);
                    password.setLength(0); // reset
                }
                textArea.requestFocusInWindow();
            }
        });

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(200, 400);
        setLocationRelativeTo(null);
    }

    private void capture(boolean start) {
        PlainDocument document = (PlainDocument)textArea.getDocument();
        DocumentFilter filter = new DocumentFilter() {

            private void doAppend(String text) {
                if (text.endsWith("\n")) {
                    expectPassword.doClick();
                } else {
                    password.append(text);
                }
            }

            @Override
            public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
                // you would have to handle multi-line pastes here also
                doAppend(text);
            }

            @Override
            public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException {
                // cannot remove while filtering
            }

            @Override
            public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
                doAppend(text);
            }          
        };
        document.setDocumentFilter(start ? filter : null);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                new CapturePassword().setVisible(true);
            }
        });
    }

}

这篇关于JTextArea中的密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆