📄 jtextpassworddemo.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JTextPasswordDemo extends JFrame
{
private String username,password;
JTextField textField;
JPasswordField passwordField;
public JTextPasswordDemo()
{
super( "JTextPasswordDemo" );
// 获取content pane并设置布局管理器
Container container = getContentPane();
container.setLayout( new FlowLayout() );
// 创建text label和text field
JLabel textLabel = new JLabel( "Username: " );
textField = new JTextField( 10 );
// 创建password label和password field
JLabel passwordLabel = new JLabel( "Password: " );
passwordField = new JPasswordField( 10 );
passwordField.setEchoChar( '*' );
// 注册时间处理器
EventHandler handler = new EventHandler();
textField.addActionListener( handler );
passwordField.addActionListener( handler );
container.add( textLabel );
container.add( textField );
container.add( passwordLabel );
container.add( passwordField );
setSize( 260,110 );
setVisible( true );
}
// 用于事件处理的内部类
private class EventHandler implements ActionListener
{
public void actionPerformed( ActionEvent event )
{
String output = "";
// 在textField处按回车键
if ( event.getSource() == textField )
output = "Username:" + event.getActionCommand();
// 在passwordField处按回车键
else if ( event.getSource() == passwordField )
output = "Password:" + new String( passwordField.getPassword() );
JOptionPane.showMessageDialog( null,output );
}
}
public static void main( String args[] )
{
JTextPasswordDemo jtp = new JTextPasswordDemo();
jtp.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -