簡體   English   中英

Java-密碼字段字符計數器

[英]Java - Password Field character counter

我有一個分配問題,要求我創建一個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();
    }
}

更改此行:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM