Java implementation of the simple program login interface

Source: Internet
Author: User
Tags gettext

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{

  1.    GetKeys();//获取文件登陆信息  
  2.    User = new JTextField(15);  
  3.    PassWord = new JPasswordField(15);  
  4.    PassWord.setEchoChar(‘*‘);  
  5.    JPanel p1 = new JPanel();  
  6.    p1.add(new JLabel("姓名:"));  
  7.    p1.add(User);  
  8.    p1.add(new JLabel("密码:"));  
  9.    p1.add(PassWord);  
  10.    add(p1,BorderLayout.CENTER);  
  11.    JPanel p2 = new JPanel();  
  12.    JButton button = new JButton("确定");  
  13.    button.addActionListener(new ActionListener()//登录监听器  
  14.    {  
  15.        @SuppressWarnings("deprecation")  
  16.        public void actionPerformed(ActionEvent e)  
  17.        {  
  18.            String name = User.getText();  
  19.            String password = PassWord.getText();  
  20.            if(message.containsKey(name))//姓名正确  
  21.            {  
  22.                num = 3;//一个用户名有3次输入密码机会  
  23.                if(message.get(name).equals(password))//密码正确  
  24.                {  
  25.                    JOptionPane.showMessageDialog(Login.this, "登陆成功!");  
  26.                    System.exit(0);  
  27.                }  
  28.                else  
  29.                {  
  30.                    num--;  
  31.                    if(num > 0)  
  32.                    {  
  33.                        JOptionPane.showMessageDialog(Login.this, "密码错误!还有"+num+"次机会");  
  34.                        PassWord.setText("");  
  35.                    }  
  36.                    else if(num == 0)  
  37.                    {  
  38.                        JOptionPane.showMessageDialog(Login.this, "登录3次失败程序关闭!");  
  39.                        System.exit(0);  
  40.                    }  
  41.                }  
  42.            }  
  43.            else  
  44.            {  
  45.                JOptionPane.showMessageDialog(Login.this, "不存在该用户名");  
  46.                User.setText("");  
  47.                PassWord.setText("");  
  48.            }  
  49.        }  
  50.    });  
  51.    p2.add(button);  
  52.    button = new JButton("取消");  
  53.    button.addActionListener(new ActionListener()  
  54.    {  
  55.        public void actionPerformed(ActionEvent e)  
  56.        {  
  57.            System.exit(0);  
  58.        }  
  59.    });  
  60.    p2.add(button);  
  61.    add(p2,BorderLayout.SOUTH);  
  62.    setLocation(400,200);  
  63.    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  64.    pack();  
  65.    setVisible(true);  
  66. }
    End

Java implementation of the simple program login interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.