验证码-java界面版

实现登录界面,支持检查验证码是否正确

界面:

 

 

 (登录名,密码没弄,输啥都行,主要是验证码)

验证码正确界面:

 

验证码错误界面:

 

点击"换"按钮可以更换验证码

 

 

 

 

 

 

 

总结:

主要用了swing 类、awt类、awt.event类,运用监听器来实现验证功能。

学习了大概两天,收获很多。

 

 源代码:

  1 package testui;
  2 
  3 import javax.swing.*;
  4 import java.awt.*;
  5 import java.awt.event.*;
  6 
  7 public class TestListener {
  8     
  9     private static JFrame frame;         //实例
 10     private static JPanel panel;         //面板
 11     private static JLabel[] lable;        //
 12     private static JButton[] button;    //按钮
 13     private static JTextField[] text;    //文本框
 14     private static JPasswordField password;//密码框
 15     public static String code = makeCode();;
 16     public TestListener()  //构造 初始化
 17     {
 18     
 19         panel = new JPanel();    
 20         
 21         lable = new JLabel[4];
 22         lable[0] = new JLabel("登录名:");
 23         lable[1] = new JLabel("密码:");
 24         lable[2] = new JLabel("验证码:");
 25         lable[3] = new JLabel(code);
 26         
 27         text = new JTextField[2];
 28         text[0] = new JTextField(20);//登录名
 29         text[1] = new JTextField(20);//验证码
 30         
 31         password = new JPasswordField(20);
 32         
 33         button = new JButton[3];
 34         button[0] = new JButton("登录");
 35         button[1] = new JButton("快速注册");
 36         button[2] = new JButton("换");
 37         
 38         //监听器
 39         button[0].addActionListener(//登录
 40                 new ActionListener() 
 41                 {                
 42                     public void actionPerformed(ActionEvent e) 
 43                     {
 44                         
 45                         if((text[1].getText()).equals(code)) {
 46                             JOptionPane.showMessageDialog(frame, "登录成功");
 47                         }
 48                         else {
 49                             JOptionPane.showMessageDialog(frame, "验证码不正确");
 50                         }
 51                     }
 52                 }
 53         );
 54         button[1].addActionListener(//注册
 55                 new ActionListener() 
 56                 {                
 57                     public void actionPerformed(ActionEvent e) 
 58                     {
 59                         JOptionPane.showMessageDialog(frame, "功能未开发");    
 60                     }
 61                 }
 62         );
 63         button[2].addActionListener(//换验证码
 64                 new ActionListener() 
 65                 {                
 66                     public void actionPerformed(ActionEvent e) 
 67                     {
 68                         lable[3].setText(makeCode());
 69                         code = lable[3].getText();
 70                     }
 71                 }
 72         );
 73         //添加到面板
 74         setplane(panel);
 75 
 76     }
 77     public static void setplane(JPanel panel)
 78     {
 79         panel.setLayout(null);
 80         lable[0].setBounds(10,20,80,25);      
 81         lable[1].setBounds(10,50,80,25);
 82         lable[2].setBounds(10,80,80,25);
 83         lable[3].setBounds(145,80,80,25);
 84         text[0].setBounds(70,20,165,25);
 85         text[1].setBounds(70,80,60,25); 
 86         password.setBounds(70,50,165,25);
 87         button[0].setBounds(80, 110, 140, 25);       
 88         button[1].setBounds(80, 145, 140, 25);
 89         button[2].setBounds(185,80,47,25);
 90         for(int i=0;i<3;i++)
 91         {
 92             panel.add(button[i]);
 93         }
 94         for(int i=0;i<4;i++)
 95         {
 96             panel.add(lable[i]);
 97         }
 98         panel.add(text[0]);
 99         panel.add(text[1]);
100         panel.add(password);
101         
102     }
103     
104     public static String makeCode()//生成验证码
105     {
106         String code="";
107         for(int i = 0 ; i < 4 ; i ++)
108         {
109             int intVal = (int)(Math.random() * 26 + 97);
110             code = code + (char)intVal;
111         }
112         return code;
113     }
114     
115     public static void main(String[] args) {
116         
117         TestListener t = new TestListener();
118         frame = new JFrame("请登录");
119         frame.setSize(330, 230);
120         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
121         frame.getContentPane().add(panel);
122             //frame.pack();//自动设置适合大小
123             frame.setVisible(true);
124         
125     }
126     
127 }

 

posted @ 2020-10-07 21:59  学习中_1  阅读(162)  评论(0编辑  收藏  举报