java学习中随机验证码的实现

        先说一下主要遇到的问题,对知识的不熟练,在添加涮新按钮时出现不能移动位置的情况。

然后说一下思路

/*运用绘图方法,创建窗口,将面板放入窗口,在面板上绘制矩形调用随机颜色填充矩形,写四位随机数,把四位随机数画
入矩形,设置干扰线。在窗口李添加按钮,在面板里触发按钮调用repaint();方法重写四位验证码。
*/

       现在看一下搞好的代码;

 1 package yyy;
 2 import java.awt.*;  
 3 import java.awt.event.*;  
 4 import java.util.Random;
 5 
 6 import javax.swing.JButton;
 7 import javax.swing.JFrame;  
 8 public class YY extends JFrame{ 
 9      static JButton bn = new JButton("刷新");
10      static Frame frame = new Frame("验证码"); // 创建Frame对象  
11      static Panel panel = new MyPanel();
12     public static void main(String[] args) {  
13          
14          frame.setLayout(null); //窗体布局不能忘
15          frame.setLocationRelativeTo(null);//该方法设置窗口相对于指定组件的位置。如果组件当前未显示,或者 c 为 null,则此窗口将置于屏幕的中央
16         frame.setSize(400, 400);  
17         frame.setVisible(true);  
18         
19         frame.add(bn);//在窗体中添加按钮,设置坐标大小
20         bn.setBounds(120,260,150,70);
21         
22           frame.add(panel); //将面板添加进窗体
23           panel.setLayout(null);
24           panel.setBounds(0,1,400,200);
25         //窗口关闭事件  
26         frame.addWindowListener(new WindowAdapter() {  
27             public void windowClosing(WindowEvent e){  
28                 Window w=(Window)e.getComponent();  
29                 w.dispose();  
30             }  
31         });     
32     }  
33 
34  static class MyPanel extends Panel {  
35      
36      MyPanel() {//按钮触发调用paint方法
37         bn.addActionListener(new ActionListener() {
38             public void actionPerformed(ActionEvent e) {
39                 repaint();//自动调用paint()
40                 
41             }
42         });
43     
44     }
45      
46 
47     public void paint(Graphics g) { 
48         
49         int width = 400; // 定义验证码图片的宽度  
50         int height = 300; // 定义验证码图片的高度  
51         g.setColor(this.getRandomColor());                // 设置颜色  
52         g.fillRect(0, 0, width, height);            // 填充验证码背景  
53         g.setColor(Color.BLACK);                     // 设置上下文颜色  
54         g.drawRect(0, 0, width - 1, height - 1); // 绘制边框  
55         
56         // 绘制干扰线
57         Random r = new Random();
58         g.setColor(this.getRandomColor());    
59         for(int i = 0; i < 10; i++){    
60             int x = r.nextInt(width/2);    
61             int y = r.nextInt(height/2);    
62             int x1 = r.nextInt(width+1);    
63             int y1 = r.nextInt(height);    
64             g.drawLine(x, y,  x1, y1);  
65         }  
66         g.setFont(new Font("黑体", Font.BOLD, 80)); // 设置验证码字体  
67         g.setColor(this.getRandomColor());                        // 设置验证码颜色  
68         // 产生随机验证码  
69         char[] chars = ("0123456789abcdefghijkmnopqrstuvwxyzABCDEFG"  
70                 + "HIJKLMNPQRSTUVWXYZ").toCharArray();  
71         StringBuilder sb = new StringBuilder();  
72         for (int i = 0; i < 4; i++) {  
73             int pos = r.nextInt(chars.length);  
74             char c = chars[pos];  
75             sb.append(c + " ");  
76         }  
77         g.drawString(sb.toString(), 40, 150); // 写入验证码  
78     }
79 
80     private Color getRandomColor() {
81         Random r = new Random();  
82         int R=r.nextInt(255),G=r.nextInt(255),B=r.nextInt(255); 
83         return new Color(R,G,B); 
84     }  
85     
86 }  
87 }

        额,第一次写,我把所有东西都写进paint();里了。

        其实这个改的时候我有一个疑问,为啥写了窗体布局和面板布局界面会不一样呢?而按钮不能移动的情况到底是为啥?改完还是不太明白。。。有大佬可以解答一下小菜鸟的疑问吗??

         这是没改之前的关于按钮的代码;

 1 public class YY extends JFrame{ 
 2      static JButton bn = new JButton("刷新");
 3      
 4     public static void main(String[] args) {  
 5         final Frame frame = new Frame("验证码"); // 创建Frame对象  
 6         final Panel panel = new MyPanel();       // 创建Canvas对象  
 7        frame.add(panel);  
 8         frame.setSize(400, 300);  
 9         // 将Frame窗口居中  
10         frame.setLocationRelativeTo(null);  
11         frame.setVisible(true);  
12         //  frame.add(bn);  如果放在窗口里,按钮会盖在画板下面
13           frame.addWindowListener(new WindowAdapter() {  
14             public void windowClosing(WindowEvent e){  
15                 Window w=(Window)e.getComponent();  
16                 w.dispose();  
17             }  
18         });     
19     }
20 
21 
22 
23  MyPanel() {
24         this.add(bn);//在这里的话按钮不能移动位置也改变不了大小
25         bn.setBounds(0, 0,100,700);
26         bn.addActionListener(new ActionListener() {
27             public void actionPerformed(ActionEvent e) {
28                 repaint();//自动调用paint()
29                 
30             }
31         });
32     
33     }
34   

求解  求解                                                                                                  2018-05-14        20:59:19

猜你喜欢

转载自www.cnblogs.com/lhj-www-01/p/9038133.html