1

JPasswordField を作成する必要がある割り当ての質問があります。2 つのボタンが必要です。1 つは別のテキスト フィールドに実際のパスワードを表示するためのもので、もう 1 つはテキスト フィールドの文字数を表示するだけのものです。これが私がしなければならないことですが、パスワード自体を読み取らせたいときに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();
    }
}
4

1 に答える 1

2

この行を変更します。

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());
于 2013-03-31T00:26:54.037 に答える