📄 jpasswordfielddemo.java
字号:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class JPasswordFieldDemo extends JFrame implements ActionListener {
JLabel jl1,jl2,jl3;
JPasswordField jp1,jp2;
JButton jb1,jb2;
public JPasswordFieldDemo()
{
Container content=this.getContentPane();
jl1=new JLabel("<html><h3>请输入你得密码:",JLabel.CENTER);
jl2=new JLabel("<html><h3>你再次输入密码:",JLabel.CENTER);
jl3=new JLabel();
jp1=new JPasswordField(8);
jp2=new JPasswordField(8);
jb1=new JButton("<html><h3>提交");
jb2=new JButton("<html><h3>取消");
content.setLayout(new GridLayout(4,2));
content.add(jl1);
content.add(jp1);
content.add(jl2);
content.add(jp2);
content.add(jb1);
content.add(jb2);
content.add(jl3);
jb1.addActionListener(this);
jb2.addActionListener(this);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==jb1)
{
if(jp1.getPassword().length>0)
{
if(String.valueOf(jp1.getPassword()).equals(String.valueOf(jp2.getPassword())))
{
jl3.setText("两次密码输入相同");
}
else
{
jl3.setText("两次输入不相同");
jp1.setText("");
jp2.setText("");
}
}
else
{
jl3.setText("密码不能为空");
//jp1.setText("");
//jp2.setText("");
}
}
else if(e.getSource()==jb2)
{
jp1.setText("");
jp2.setText("");
jl3.setText("");
//this.show();
}
}
public static void main(String args[])
{
new JPasswordFieldDemo();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -