在JAVA控制台上, 如何象输入密码一样输入一个CHAR而看到的是星号呢?

帮忙啊yyf7777 能给出整个程序的完整的代码吗? 谢谢了
2024-11-19 04:43:56
推荐回答(4个)
回答(1):

 
 
 
如果你指的是字符界面的密码掩盖,可以参考我以前回答过的一题:
http://zhidao.baidu.com/question/2250434.html

至于图形界面的例子,我刚写了一个利用 JPasswordField 和 TextField 的:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class C {
    public static void main( String[] args ) {
        JFrame f = new JFrame( );
        f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        f.setSize( 300, 140 );
        f.setLocation( 250, 250 );

        Container c = f.getContentPane( );
        c.setLayout( new FlowLayout( ) );

        // JPasswordField
        c.add( new JLabel( "Enter your password into this JPasswordField: " ) );
        JPasswordField jpf = new JPasswordField( 10 );
        jpf.addActionListener(
            new ActionListener( ) {
                public void actionPerformed( ActionEvent e ) {
                    Object pwd =
                        new String( ( ( JPasswordField ) e.getSource( ) ).getPassword( ) );
                    JOptionPane.showMessageDialog( null, "Your password is " + pwd );
                }
            }
        );
        c.add( jpf );

        // TextField
        c.add( new JLabel( "Or enter your password into this TextField: " ) );
        TextField tf = new TextField( 12 );
        tf.setEchoChar( '*' );
        tf.addActionListener(
            new ActionListener( ) {
                public void actionPerformed( ActionEvent e ) {
                    Object pwd = ( ( ( TextField ) e.getSource( ) ).getText( ) );
                    JOptionPane.showMessageDialog( null, "Your password is " + pwd );
                }
            }
        );
        c.add( tf );

        f.setVisible( true );
    }
}
 
 
 

回答(2):

SWING包中就用JPasswordField
JPasswordField txtpwd=new JPasswordField(20);

AWT包中就用TEXTFIELD的setEchoChar(‘*’)的属性!
TextField txtpwd=new TextField(20);
txtpwd.setEchoChar('*');

回答(3):

password里面的就是

回答(4):

可以看看 String类,其中就有这个的操作方法.