【问题标题】:Password in JTextAreaJTextArea 中的密码
【发布时间】:2014-05-25 14:14:41
【问题描述】:

有什么方法可以在用户输入时使用JTextArea隐藏文本??

有点像密码..

JTextArea 我有..

密码:

在最后一行,并且该行中的任何用户类型都不应该是可见的。

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

有什么解决方法或者我怎样才能做到这一点??

请帮忙

【问题讨论】:

  • 您是否正在寻找 JTextArea 的密码而不是 JTextField ?你到底为什么想要那个......?
  • @ryvantage 它是一个类似 teraterm 的终端工具,其中我使用 JtextArea 进行所有 GUI 显示。
  • 如果您可以检测到何时需要输入密码,您可以暂时将DocumentFilter 应用到您的JTextAreaDocument 并捕获所有输入,而不会成为文本的一部分区域正在显示。

标签: java swing jtextarea


【解决方案1】:

这就是我在 cmets 中所说的内容。请注意,这只是一个简单的示例(可能想要安全地存储密码等),但应该可以帮助您入门。

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

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);
            }
        });
    }

}

【讨论】:

    【解决方案2】:

    使用 JPasswordField 类,它是 JTextField 的子类,为密码输入提供专门的文本字段。

    参考链接如下: http://docs.oracle.com/javase/tutorial/uiswing/components/passwordfield.html

    【讨论】:

      【解决方案3】:

      只需使用JPasswordField 的子类JTextFieldjava doc 中讨论的非常清楚和简单,所以我避免重复。

      【讨论】:

      • 不是JPasswordField是JTextField的子类吗?
      • 是 JTextComponents 的子类
      • 我认为 OP 正在寻找 JTextArea 密码字段,而不是 JTextField 密码字段,JPasswordField 是。
      • @ryvantage> 我刚刚发布了最有意义的答案。正如您在 cmets JTextArea 中所说的那样,密码不是一个好的选择(即使可能,您是否见过这样的事情?)。
      【解决方案4】:

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

      如果你想为文本的不同部分使用不同的属性,你需要使用 JTextPane。

      阅读 Text Component Features 上的 Swing 教程部分,了解更多信息和工作示例。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-14
        • 2017-06-28
        • 2015-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-13
        相关资源
        最近更新 更多