【问题标题】:Java - Password Field character counterJava - 密码字段字符计数器
【发布时间】:2013-03-31 00:20:37
【问题描述】:

我有一个作业问题,要求我创建一个 JPasswordField。需要两个按钮,一个用于在另一个文本字段中显示实际密码,另一个仅在文本字段中显示字符数。这是我到目前为止所要做的,但我无法编译它,因为Method setText in class javax.swing.text.JTextComponent cannot be applied to given types. 当我希望编译器自行读取密码时,编译器会在bt1 下停止。

谁能帮忙?

谢谢。 代码:

public class JavaPasswordCount {  
    public JavaPasswordCount() {
        JFrame window = new JFrame("Password Character Count");
        window.setSize(50, 50);
        JButton bt1 = new JButton("Show Count");
        JButton bt2 = new JButton("Show Password");
        final JPasswordField pwd = new JPasswordField();
        final JTextField tf = new JTextField(); 
        final int counter;

        JPanel panel = new JPanel(); 

        bt1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                tf.setText(pwd.getPassword());
            }
        });

        bt2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                counter = pwd.length();
                tf.setText(counter);
            }
        });

        panel.setLayout(new FlowLayout()); // Add buttons and TextField to the panel
        panel.add(tf);
        panel.add(pwd);
        panel.add(bt1);
        panel.add(bt2);

        window.getContentPane().add(panel, BorderLayout.CENTER);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.pack();
        window.setVisible(true);
    }
    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        } catch (Exception e) {
        }

        JavaPasswordCount application = new JavaPasswordCount();
    }
}

【问题讨论】:

    标签: java swing gettext jpasswordfield


    【解决方案1】:

    更改此行:

    counter = pwd.length();
    tf.setText(counter);
    

    int counter = pwd.getPassword().length;
    tf.setText(String.valueOf((counter)));
    

    还有这个

    tf.setText(pwd.getPassword());
    

    tf.setText(pwd.getPassword().toString());
    

    【讨论】:

    • 谢谢@MrD。但我仍然遇到同样的错误 - 当我希望它读取实际密码时,它会停在“bt1”下的部分。
    • 效果很好,@MrD。谢谢你的帮助。接受。
    • 没问题,伙计。记得检查一下:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多