Here is the simple code I wrote:
Simple, no chewing stickers, directly on the code. File save user name and password, enter password error 3 times to exit the program.
[Java] View plaincopy
01.public Login () throws ioexception{
-
GetKeys();//获取文件登陆信息
-
User = new JTextField(15);
-
PassWord = new JPasswordField(15);
-
PassWord.setEchoChar(‘*‘);
-
JPanel p1 = new JPanel();
-
p1.add(new JLabel("姓名:"));
-
p1.add(User);
-
p1.add(new JLabel("密码:"));
-
p1.add(PassWord);
-
add(p1,BorderLayout.CENTER);
-
JPanel p2 = new JPanel();
-
JButton button = new JButton("确定");
-
button.addActionListener(new ActionListener()//登录监听器
-
{
-
@SuppressWarnings("deprecation")
-
public void actionPerformed(ActionEvent e)
-
{
-
String name = User.getText();
-
String password = PassWord.getText();
-
if(message.containsKey(name))//姓名正确
-
{
-
num = 3;//一个用户名有3次输入密码机会
-
if(message.get(name).equals(password))//密码正确
-
{
-
JOptionPane.showMessageDialog(Login.this, "登陆成功!");
-
System.exit(0);
-
}
-
else
-
{
-
num--;
-
if(num > 0)
-
{
-
JOptionPane.showMessageDialog(Login.this, "密码错误!还有"+num+"次机会");
-
PassWord.setText("");
-
}
-
else if(num == 0)
-
{
-
JOptionPane.showMessageDialog(Login.this, "登录3次失败程序关闭!");
-
System.exit(0);
-
}
-
}
-
}
-
else
-
{
-
JOptionPane.showMessageDialog(Login.this, "不存在该用户名");
-
User.setText("");
-
PassWord.setText("");
-
}
-
}
-
});
-
p2.add(button);
-
button = new JButton("取消");
-
button.addActionListener(new ActionListener()
-
{
-
public void actionPerformed(ActionEvent e)
-
{
-
System.exit(0);
-
}
-
});
-
p2.add(button);
-
add(p2,BorderLayout.SOUTH);
-
setLocation(400,200);
-
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
pack();
-
setVisible(true);
- }
End
Java implementation of the simple program login interface