1、创建和初始化
passwordField = new JPasswordField(10);
passwordField.setActionCommand(OK);
passwordField.addActionListener(this);
如想改变密码框的占位符,调用setEchoChar
2、在ActionListener中获得密码
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (OK.equals(cmd)) { //Process the password.
char[] input = passwordField.getPassword();
if (isPasswordCorrect(input)) {
JOptionPane.showMessageDialog(controllingFrame,
"Success! You typed the right password.");
} else {
JOptionPane.showMessageDialog(controllingFrame,
"Invalid password. Try again.",
"Error Message",
JOptionPane.ERROR_MESSAGE);
}
//Zero out the possible password, for security.
Arrays.fill(input, '0');
passwordField.selectAll();
resetFocus();
} else ...//handle the Help button...
}
注意:不要说和getText方法获得密码串,而是使用getPassword,不仅出于安全因素考虑,而且getText可能返回类似“***”的无效字符串。
分享到:
相关推荐
主要介绍了Java Swing JPasswordField密码框的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
//定义密码框组件 JPasswordField jPasswordField; JLabel jLabel1,jLabel2,jLabel3; JPanel jp1,jp2,jp3,jp4; JButton jb1,jb2; //创建按钮 Socket s = null; DataOutputStream dos = null; ...
创建一个密码输入对话框,用于输入明显隐藏的密码。 必须启用 Java 才能使此功能正常工作。... 密码框是使用 Java Swing 组件 JPasswordField 创建的。 注意:密码只是可见隐藏,可以在内存中访问。
考虑到数据的安全性,密码以JPasswordField组件的形式输入,并在相应的按钮监听器中进行比较和存储。程序的基本框架包括创建账户、查看账户、存款、取款和查询余额等功能。每个按钮点击事件都会在对应的监听器方法中...
JTextField及JPasswordField的空值处理,思路是采用图层Lable,并利用lable的无点击特性,类似ios文本框的空值默认显示。
JPasswordField(密码框) ;JCheckBox(复选框);JRadioButton(单选按钮);组合框JComboBox;组合框JComboBox—事件;public class Test4 extends JFrame implements ActionListener { String[] items = { "篮球", ...
① // 添加“用户名”、“密码”,使用标签,和文本框、密码框(隐藏文本字段区域中的字符) cont.add(new JLabel("username")); username = new JTextField(10); cont.add(username); cont.add(new JLabel...
//创建一个密码框对象 JButton JB1 =new JButton("登录"); //创建按钮对象 JButton JB2 =new JButton("取消"); JLabel JL1 =new JLabel("身 份:"); //使用文本创建一个标签对象 JComboBox JC =new JComboBox();...
//密码标签及其密码框 JLabel userpwdLabel = new JLabel("密码:"); userpwdLabel.setFont(new Font("宋体",Font.BOLD,30));//字体大小设置 userpwdLabel.setForeground(Color.decode("#000000"));//字体颜色...
//创建密码框 jpf=new JPasswordField(10); //设置布局管理 this.setLayout(new GridLayout(3, 1));//网格式布局 //加入各个组件 jp1.add(jlb1); jp1.add(jtf); jp2.add(jlb2); jp2.add(jpf)...
登录名使用特殊的Swing对象作为密码字段JPasswordField然后,登录名检查用户名和pw是否正确。FOR SAMPLE用户名和pw都被硬编码为:root 三明治,土豆,饮料的选择 -菜单控件被编码为使用菜单项按钮添加到订单中 每...
// 密码框隐藏 private JLabel verCodeLa = new JLabel("验证码:"); // 验证码 private JTextField inputCode = new JTextField(); // 验证码框 private ValidCode vcode = new ValidCode(); // 验证码内容 ...
final JPasswordField text2=new JPasswordField(15); label1.setBounds(80,60,60,30); text1.setBounds(160, 60, 150,30); label2.setBounds(80,120,60,30); text2.setBounds( 160, 120, 150, 30); ...
第一篇 起步篇 第1章 初识Java 3 1.1 Java简介 3 1.1.1 Java的不同平台 3 1.1.2 Java发展的历程 3 1.1.3 Java的特点 4 1.2 安装开发工具包 5 1.2.1 下载JDK 5 1.2.2 安装JDK 6 ...
14.8.3文本框(JTextField)和密码框(JPasswordField)使用示例492 14.8.4文本区(JTextArea)使用示例496 14.8.5复选框(JcheckBox)使用示例498 14.8.6单选按钮(JradioButton)使用示例500 14.8.7列表框...