zoukankan      html  css  js  c++  java
  • 创建登录界面及简易验证码

    .程序设计思路: 1.显示登录,注册页面,以及主页面

       2.可以在表里存储数据,即注册用户

       3.可以在图形框中输入数据

       4.在后端检索所有数据,查看是否存在数据

       5.随机生成验证码,并且要求输入的验证码与显示的对应

       6.完成登录

    二. 流程图

    三.源代码

    /*本类将作为上网时的登录页面

      需要实现的功能:

      1.显示登录,注册页面,以及主页面

      2.可以在表里存储数据,即注册用户

      3.可以在图形框中输入数据

      4.在后端检索所有数据,查看是否存在数据

      5.随机生成验证码,并且要求输入的验证码与显示的对应

      6.完成登录

     * */

    package class3Login;

     

     

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    import javax.swing.JButton;

    import javax.swing.JFrame;

    import javax.swing.JLabel;

    import javax.swing.JOptionPane;

    import javax.swing.JPanel;

    import javax.swing.JPasswordField;

    import javax.swing.JTextField;

     

     

     

    public class Interface extends JFrame

    {

     

    private static final long serialVersionUID = 1L;

     

    private static String record;

     

    public static void main(String[] args)

    {

    new Interface();

    }

     

    public Interface()

    {

    // 创建 JFrame 实例

            JFrame frame = new JFrame("请登录:");

            

            // 设置frame大小

            frame.setLocation(500,300);

            frame.setSize(400, 200);

            frame.setResizable(false);

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            

            //创建面板

            JPanel panel = new JPanel();

            

            // 添加面板

            frame.add(panel);

            

            //根据需要设置面板的具体形式

            placeComponents(panel);

            

            // 设置界面可见

            frame.setVisible(true);

            

            

    }

    public  void placeComponents(JPanel panel)

    {

     

    //清空面板

     panel.setLayout(null);

     

     // 创建 用户使用的组件

         JLabel userLabel = new JLabel("用户名:");

         userLabel.setBounds(10,20,80,25);

         panel.add(userLabel);

     

         // 创建文本域用于用户输入

          JTextField userText = new JTextField(20);

          userText.setBounds(100,20,165,25);

          panel.add(userText);

         

          // 创建 密码使用的组件

          JLabel passwordLabel = new JLabel("密码:");

          passwordLabel.setBounds(10,50,80,25);

          panel.add(passwordLabel);

     

          // 创建密码的文本域

          JPasswordField passwordText = new JPasswordField(20);

          passwordText.setBounds(100,50,165,25);

          panel.add(passwordText);

          

          // 创建 验证码使用的组件

          JLabel verificationLabel = new JLabel("验证码:");

          verificationLabel.setBounds(10,80,80,25);

          panel.add(verificationLabel);

     

          // 创建验证码的文本域

          JTextField verificationText = new JTextField(20);

          verificationText.setBounds(100,80,80,25);

          panel.add(verificationText);

          

          //创建一个随机字符串

          String  result = "";

          for(int i = 0 ; i < 6 ; i ++)

          {

           int intVal = (int)(Math.random() * 26 + 97);

           result = result + (char)intVal;

          }

          record = result;

          

          // 创建验证码使用的显示的组件

          JLabel verificationShowLabel = new JLabel(result);

          verificationShowLabel.setBounds(200,80,80,25);

          panel.add(verificationShowLabel);

          

          // 创建登录按钮

          JButton loginButton = new JButton("登录");

          loginButton.setBounds(10, 120, 80, 25);

          panel.add(loginButton);

         

          //对按钮进行监视

          ActionListener ourListener1 = new ActionListener()

          {

           public void actionPerformed(ActionEvent e)

           {

           if (e.getSource() == loginButton) //判断是否点击登录按钮

           {

           if(record.equalsIgnoreCase(verificationText.getText()))//判断验证码是否正确

           {

           if(StoreroomManager.datelist.size()==0)//判断存储空间是否有数据

           {

           JOptionPane.showMessageDialog(null, "你还没有注册");

           userText.setText("");

           passwordText.setText("");

           

           }

           else//对现有的date进行赋值,并且检索是否存在注册过的用户

           {

           Storeroom date= new Storeroom();

           date.setUser(userText.getText());//获取用户输入的用户名

           date.setPassword(passwordText.getText());//获取用户输入的密码

           StoreroomManager.confirmDate(date);

           passwordText.setText("");

           verificationText.setText("");

           }

           }

           else // 验证码不正确

           {

           passwordText.setText("");

           verificationText.setText("");

           JOptionPane.showMessageDialog(null, "验证码错误,请重新输入");

           //创建一个随机字符串

               String  result = "";

               for(int i = 0 ; i < 6 ; i ++)

               {

                int intVal = (int)(Math.random() * 26 + 97);

                result = result + (char)intVal;

               }

               record = result;

               verificationShowLabel.setText(result);

            

           }

           }

           }

          };

          loginButton.addActionListener(ourListener1);

          

          

          // 创建注册按钮

          JButton registerButton = new JButton("注册");

          registerButton.setBounds(100, 120, 80, 25);

          panel.add(registerButton);

          

          

          //对按钮进行监视

          ActionListener ourListener2 = new ActionListener()

          {

           public void actionPerformed(ActionEvent e)

    {

           if (e.getSource() == registerButton)

           {

           userText.setText("");

           passwordText.setText("");

           verificationText.setText("");

           new RegisterInterface();

           }

         }

          };

          registerButton.addActionListener(ourListener2);

          

          // 创建刷新按钮

          JButton refreshButton = new JButton("刷新");

          refreshButton.setBounds(250, 80, 80, 25);

          panel.add(refreshButton);

          

          

          //对按钮进行监视

          ActionListener ourListener3 = new ActionListener()

          {

           public void actionPerformed(ActionEvent e)

    {

           if (e.getSource() == refreshButton)

           {

           passwordText.setText("");

           verificationText.setText("");

           //创建一个随机字符串

               String  result = "";

               for(int i = 0 ; i < 6 ; i ++)

               {

                int intVal = (int)(Math.random() * 26 + 97);

                result = result + (char)intVal;

               }

               record = result;

               verificationShowLabel.setText(result);

           

           }

         }

          };

          refreshButton.addActionListener(ourListener3);

          

          

          

    }

    }

     

     

    //本文件相当于数据库,用于存储数据

    package class3Login;

     

    import java.util.ArrayList;

    import java.util.List;

     

    import javax.swing.JOptionPane;

     

    //作为一个类,用来作为存储数据的单元

    class Storeroom

    {

    private String User;

    private String password;

    void setUser(String str)

    {

    User = str;

    }

    String getUser()

    {

    return User;

     

    }

    void setPassword(String str)

    {

    password = str;

    }

    String getpassword()

    {

    return password;

     

    }

     

    }

    class StoreroomManager //对数据进行操作的类

    {

    static List<Storeroom> datelist = new ArrayList<Storeroom>(0);

     

    static void confirmDate(Storeroom date)//登录,遍历数据

    {

    int flag = 0;

     

    for(int i = 0;i<datelist.size();i++)

    {

    if(date.getUser().equalsIgnoreCase(datelist.get(i).getUser()))

    {

    if(date.getpassword().equalsIgnoreCase(datelist.get(i).getpassword()))

    {

    JOptionPane.showMessageDialog(null, "登录成功");

     flag++;

    }

     

    }

    }

    System.out.println("输入的数据:");

    System.out.println("用户名:"+date.getUser());

    System.out.println("密码:"+date.getpassword());

    for(int i = 0;i<datelist.size();i++)

    {

    System.out.println("表中的"+(i+1)+"个数据:");

    System.out.println("用户名:"+datelist.get(i).getUser());

    System.out.println("密码:"+datelist.get(i).getpassword());

    }

    if(flag == 0)

    {

    JOptionPane.showMessageDialog(null, "用户名或者密码错误!");

    }

     

     

     

    }

    static boolean searchDate(Storeroom date)//登录,遍历数据

    {

    int flag = 0;

    for(int i = 0;i<datelist.size();i++)

    {

    if(date.getUser().equals(datelist.get(i).getUser()))

    if(date.getpassword().equals(datelist.get(i).getpassword()))

    flag++;

    }

    if(flag==0)

    return false;

    else

    return true;

     

     

    }

     

    }

    //本类文件与Interface类大致相同

    package class3Login;

    import java.awt.event.ActionEvent;

    import java.awt.event.ActionListener;

    import javax.swing.JButton;

    import javax.swing.JFrame;

    import javax.swing.JLabel;

    import javax.swing.JOptionPane;

    import javax.swing.JPanel;

    import javax.swing.JPasswordField;

    import javax.swing.JTextField;

    class RegisterInterface //作为注册页面

    {

    private static String record;

    public RegisterInterface()

    {

    // 创建 JFrame 实例

             JFrame frame = new JFrame("欢迎来到注册页面:");

            

            // 设置frame大小

            frame.setLocation(500,300);

            frame.setSize(400, 200);

            frame.setResizable(false);

            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

            

            //创建面板

            JPanel panel = new JPanel();

            

            // 添加面板

            frame.add(panel);

            

            //根据需要设置面板的具体形式

            placeComponents(panel,frame);

            

            // 设置界面可见

            frame.setVisible(true);

            

            

    }

    public void placeComponents(JPanel panel,JFrame frame)

    {

    //清空面板

     panel.setLayout(null);

     // 创建 用户使用的组件

         JLabel userLabel = new JLabel("用户名:");

         userLabel.setBounds(10,20,80,25);

         panel.add(userLabel);

         // 创建文本域用于用户输入

          JTextField userText = new JTextField(20);

          userText.setBounds(100,20,165,25);

          panel.add(userText);

          

          // 创建密码使用的组件

          JLabel passwordLabel = new JLabel("密码:");

          passwordLabel.setBounds(10,50,80,25);

          panel.add(passwordLabel);

          // 创建密码的文本域

          JPasswordField passwordText = new JPasswordField(20);

          passwordText.setBounds(100,50,165,25);

          panel.add(passwordText);

          

          // 创建 验证码使用的组件

          JLabel verificationLabel = new JLabel("验证码:");

          verificationLabel.setBounds(10,80,80,25);

          panel.add(verificationLabel);

          // 创建验证码的文本域

          JTextField verificationText = new JTextField(20);

          verificationText.setBounds(100,80,80,25);

          panel.add(verificationText);

          

          //创建一个随机字符串

          String  result = "";

          for(int i = 0 ; i < 6 ; i ++)

          {

           int intVal = (int)(Math.random() * 26 + 97);

           result = result + (char)intVal;

          }

          record = result;

          

          

          // 创建验证码使用的显示的组件

          JLabel verificationShowLabel = new JLabel(result);

          verificationShowLabel.setBounds(200,80,80,25);

          panel.add(verificationShowLabel);

          

          // 创建确认按钮

          JButton confirmButton = new JButton("确定");

          confirmButton.setBounds(10, 120, 80, 25);

          panel.add(confirmButton);

          

          

          //对按钮进行监视(确认,即注册按钮)

          ActionListener ourListener1 = new ActionListener()

          {   

           public void actionPerformed(ActionEvent e)

           {  

          

           if (e.getSource() == confirmButton)

           {   

           if(record.equalsIgnoreCase(verificationText.getText()))

           {

           Storeroom date = new Storeroom();

           date.setUser(userText.getText());

           date.setPassword(passwordText.getText());

           if(StoreroomManager.searchDate(date))

           frame.dispose();

           else

           {

           StoreroomManager.datelist.add(date);

           frame.dispose();

           }

           }

           else

           {

           JOptionPane.showMessageDialog(null, "验证码错误,请重新输入");

           verificationText.setText("");

           passwordText.setText("");

           

           //创建一个随机字符串

               String  result = "";

               for(int i = 0 ; i < 6 ; i ++)

               {

                int intVal = (int)(Math.random() * 26 + 97);

                result = result + (char)intVal;

               }

               record = result;

               verificationShowLabel.setText(result);

           }

           

          

           }

           }

          };

          confirmButton.addActionListener(ourListener1);

          

          

          // 创建取消按钮

          JButton cancelButton = new JButton("取消");

          cancelButton.setBounds(100, 120, 80, 25);

          panel.add(cancelButton);

          

          

          //对按钮进行监视

          ActionListener ourListener2 = new ActionListener()

          {

           public void actionPerformed(ActionEvent e)

           {

           if (e.getSource() == cancelButton)

           {

           JOptionPane.showMessageDialog(null, "are you kidding me?!");

           frame.dispose();

           }

           }

          };

          cancelButton.addActionListener(ourListener2);

          

          // 创建刷新按钮

          JButton refreshButton = new JButton("刷新");

          refreshButton.setBounds(250, 80, 80, 25);

          panel.add(refreshButton);

          

          

          //对按钮进行监视

          ActionListener ourListener3 = new ActionListener()

          {

           public void actionPerformed(ActionEvent e)

    {

           if (e.getSource() == refreshButton)

           {

           passwordText.setText("");

           verificationText.setText("");

           //创建一个随机字符串

               String  result = "";

               for(int i = 0 ; i < 6 ; i ++)

               {

                int intVal = (int)(Math.random() * 26 + 97);

                result = result + (char)intVal;

               }

               record = result;

               verificationShowLabel.setText(result);

           

           }

         }

          };

          refreshButton.addActionListener(ourListener3);

          

    }

    }

    四. 结果截图

     

     

     

     

     

     

     

     

     

  • 相关阅读:
    Flink命令行提交job (源码分析)
    Flink 中LatencyMarks延迟监控(源码分析)
    Flink中的CEP复杂事件处理 (源码分析)
    Flink中异步AsyncIO的实现 (源码分析)
    Flink中发送端反压以及Credit机制(源码分析)
    Flink中接收端反压以及Credit机制 (源码分析)
    Flink整合oozie shell Action 提交任务 带kerberos认证
    Flink中TaskManager端执行用户逻辑过程(源码分析)
    Flink的Job启动TaskManager端(源码分析)
    Flink中Idle停滞流机制(源码分析)
  • 原文地址:https://www.cnblogs.com/tianxiayoujiu/p/7636297.html
Copyright © 2011-2022 走看看