JavaDailyReports10_11

**********************************

验证码 AWT Swing

  1 package nanshen;
  2 
  3 import java.awt.Container;
  4 import java.awt.event.ActionEvent;
  5 import java.awt.event.ActionListener;
  6 import java.util.Random;
  7 
  8 import javax.swing.JButton;
  9 import javax.swing.JFrame;
 10 import javax.swing.JLabel;
 11 import javax.swing.JOptionPane;
 12 import javax.swing.JPanel;
 13 import javax.swing.JPasswordField;
 14 import javax.swing.JTextField;
 15 
 16 public class test10 {
 17     public static void main(String[] args) {
 18         JFrame jf=new JFrame();
 19         JButton jb=new JButton("登录");
 20         /*
 21          * JLabel 用于显示一个标签信息
 22          * 
 23          */
 24         JLabel idname=new JLabel("用户名"); //实例化对象
 25         JLabel pastword=new JLabel("密    码");
 26         JLabel yzword=new JLabel("验证码");
 27         /*
 28          * 定义数组生成验证码随机数
 29          * 
 30          */
 31         int[] random=new int[6];
 32         
 33         Random r=new Random(System.currentTimeMillis());
 34         
 35         for(int i=0;i<6;i++)
 36         {
 37             random[i]=r.nextInt(10)%9+1;
 38         }
 39         /*
 40          * 把6位随机数存储为字符串形式
 41          * 
 42          */
 43         String str="";
 44         for(int i=0;i<6;i++)
 45         {
 46             str=str+random[i];
 47         }
 48         
 49         final String Str=str;
 50         
 51         JLabel yzWord=new JLabel(str);
 52         
 53         JTextField idtxt=new JTextField(15);//实例化用户名文本框
 54         
 55         JPasswordField wordtxt=new JPasswordField(15);//实例化密码框
 56         
 57         JTextField yzwordtxt=new JTextField(15);
 58         
 59         wordtxt.setEchoChar('*');//将输入密码框中的密码以*显示出来
 60         
 61         jf.setBounds(450,350,350,250);
 62         
 63         jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 64         Container c=jf.getContentPane();
 65         
 66         c.setLayout(null);
 67         c.add(jb);
 68         c.add(idname);
 69         c.add(idtxt);
 70         c.add(pastword);
 71         c.add(wordtxt);
 72         c.add(yzword);
 73         c.add(yzwordtxt);
 74         c.add(yzWord);
 75         
 76         idname.setBounds(10,40,50,18);
 77         pastword.setBounds(10,80,50,18);
 78         yzword.setBounds(10,120,50,18); //标签“验证码” 
 79         yzWord.setBounds(60,160,80,18); //生成的随机数验证码
 80         
 81         
 82         idtxt.setBounds(60,40,200,18);
 83         wordtxt.setBounds(60,80,200,18);
 84         yzwordtxt.setBounds(60,120,200,18);
 85         
 86         jb.setBounds(200,150,60,40);
 87         
 88         jb.addActionListener(new ActionListener() {
 89             
 90             public void actionPerformed(ActionEvent arg0)
 91             {
 92                 if(idtxt.getText().trim().equals("YCW000429")
 93                         &&new String(wordtxt.getPassword()).equals("123456")
 94                         &&yzwordtxt.getText().trim().equals(Str))
 95                 {
 96                     JOptionPane.showMessageDialog(null,"登录成功");
 97                 }
 98                 if(yzwordtxt.getText().trim().equals(Str)
 99                         &&(!idtxt.getText().trim().equals("YCW000429")
100                         ||!(new String(wordtxt.getPassword()).equals("123456"))))
101                 {
102                     JOptionPane.showMessageDialog(null,"用户名或密码错误");
103                 }
104                 if(idtxt.getText().trim().equals("YCW000429")
105                         &&new String(wordtxt.getPassword()).equals("123456")
106                         &&!yzwordtxt.getText().trim().equals(Str))
107                 {
108                     JOptionPane.showMessageDialog(null,"验证码错误");
109                 }
110             }
111         });
112         jf.setVisible(true);
113     }
114 }

原文地址:https://www.cnblogs.com/rainbow-1/p/13798477.html