public static void main(String[] args) {
JFrame jf=new JFrame();
jf.setSize(300,300);
jf.setLocationRelativeTo(null);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel=new JPanel();
JLabel lbl = new JLabel("请输入密码");
panel.add(lbl);
//创建密码框
final JPasswordField txtPwd=new JPasswordField(10);
//设置密码框样式
txtPwd.setEchoChar('@');
//设置水平放行居中
txtPwd.setHorizontalAlignment(SwingConstants.CENTER);
//设置字体颜色
txtPwd.setForeground(Color.GREEN);
//设置字体加粗
txtPwd.setFont(new Font("楷体",Font.BOLD,18));
//设置样式
txtPwd.setPreferredSize(new Dimension(200,50));
panel.add(txtPwd);
JButton btn=new JButton("登录");
panel.add(btn);
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
char[]pwd=txtPwd.getPassword();
String pwdString=new String(pwd);
System.out.println("收到密码++"+pwdString);
}
});
jf.setContentPane(panel);
jf.setVisible(true);
}
Swing测试密码
最新推荐文章于 2022-04-26 09:22:50 发布