java中提供密码输入的方法是什么?

来源:百度知道 编辑:UC知道 时间:2024/05/17 02:03:48
就是有个对话框,输入的时候显示的是星号。谢谢

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 pa