如果你指的是字符界面的密码掩盖,可以参考我以前回答过的一题:
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 );
}
}
SWING包中就用JPasswordField
JPasswordField txtpwd=new JPasswordField(20);
AWT包中就用TEXTFIELD的setEchoChar(‘*’)的属性!
TextField txtpwd=new TextField(20);
txtpwd.setEchoChar('*');
password里面的就是
可以看看 String类,其中就有这个的操作方法.