码迷,mamicode.com
首页 > 编程语言 > 详细

java实现的简单程序登录界面

时间:2015-04-12 10:49:01      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

这是我写的简单代码:
简单,没什么嚼头,作业贴,直接上代码。文件保存用户名和密码,输入密码错误3次退出程序。

[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实现的简单程序登录界面

标签:

原文地址:http://my.oschina.net/bigfool007139/blog/399125

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!