大作业—JAVAGUI实现数据库管理系统(5)

这次把登录注册界面都完善好了(我举得蛮可爱hh)(跟第一次的界面完全不一样了)
增添了验证码功能、密码MD5加密和上传照片功能
大作业—JAVAGUI实现数据库管理系统(5)_第1张图片
大作业—JAVAGUI实现数据库管理系统(5)_第2张图片
大作业—JAVAGUI实现数据库管理系统(5)_第3张图片
大作业—JAVAGUI实现数据库管理系统(5)_第4张图片
验证码单独写了个类,这个大小写也得区别(emmm虽然是问的度娘但是我也研究了半天)

import java.awt.Color;  
import java.awt.Dimension;  
import java.awt.Font;  
import java.awt.FontMetrics;  
import java.awt.Graphics;  
import java.awt.Graphics2D;  
import java.awt.event.MouseEvent;  
import java.awt.event.MouseListener;  
 
import java.util.Random;  

import javax.swing.JComponent;  

public class ValidCode extends JComponent implements MouseListener {  	  
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String code;  
  
    private int width, height = 40;  
  
    private int codeLength = 4;  
  
    private Random random = new Random();  
  
    public ValidCode() {  
        width = this.codeLength * 16 + (this.codeLength - 1) * 10;  
        setPreferredSize(new Dimension(width, height));  
        setSize(width, height);  
        this.addMouseListener(this);  
        setToolTipText("点击可以更换验证码");  
    }  
  
    public int getCodeLength() {  
        return codeLength;  
    }  
  
    /* 
    设置验证码文字的长度 
    */  
    public void setCodeLength(int codeLength) {  
        if(codeLength < 4) {  
            this.codeLength = 4;  
        } else {  
            this.codeLength = codeLength;  
        }  
          
    }  
  
    public String getCode() {  
        return code;  
    }  
  
    /* 
        产生随机的颜色 
    */  
    public Color getRandColor(int min, int max) {  
  
        if (min > 255)  
            min = 255;  
        if (max > 255)  
            max = 255;  
        int red = random.nextInt(max - min) + min;  
        int green = random.nextInt(max - min) + min;  
        int blue = random.nextInt(max - min) + min;  
        return new Color(red, green, blue);  
    }  
    /* 
        设置验证码具体的字母是什么 
    */  
    protected String generateCode() {  
        char[] codes = new char[this.codeLength];  
        for (int i = 0, len = codes.length; i < len; i++) {  
            if (random.nextBoolean()) {  
                codes[i] = (char) (random.nextInt(26) + 65);  
            } else {  
                codes[i] = (char) (random.nextInt(26) + 97);  
            }  
        }  
        this.code = new String(codes);  
        return this.code;  
    }  
  
    @Override  
    protected void paintComponent(Graphics g) {  
        super.paintComponent(g);  
        if(this.code == null || this.code.length() != this.codeLength) {  
            this.code = generateCode();  
        }  
        width = this.codeLength * 16 + (this.codeLength - 1) * 10;  
        super.setSize(width, height);  
        super.setPreferredSize(new Dimension(width, height));  
        Font mFont = new Font("Arial", Font.BOLD | Font.ITALIC, 25);  
        g.setFont(mFont);  
        //绘制出验证码的背景的矩形轮廓  
        Graphics2D g2d = (Graphics2D) g;  
        g2d.setColor(getRandColor(200, 250));  
        g2d.fillRect(0, 0, width, height);  
        g2d.setColor(getRandColor(180, 200));  
        g2d.drawRect(0, 0, width - 1, height - 1);  
        //绘制出验证码背景的线  
        int i = 0, len = 150;  
        for (; i < len; i++) {  
            int x = random.nextInt(width - 1);  
            int y = random.nextInt(height - 1);  
            int x1 = random.nextInt(width - 10) + 10;  
            int y1 = random.nextInt(height - 4) + 4;  
            g2d.setColor(getRandColor(180, 200));  
            g2d.drawLine(x, y, x1, y1);  
        }  
          
      
  
        //绘制出验证码的具体字母  
        i = 0; len = this.codeLength;  
        FontMetrics fm = g2d.getFontMetrics();  
        int base = (height - fm.getHeight())/2 + fm.getAscent();  
        for(;i<len;i++) {  
            int b = random.nextBoolean() ? 1 : -1;  
            g2d.rotate(random.nextInt(10)*0.01*b);  
            g2d.setColor(getRandColor(20, 130));  
            g2d.drawString(code.charAt(i)+"", 16 * i + 10, base);  
        }  
    }  
  
    //下一个验证码  
    public void nextCode() {  
        generateCode();  
        repaint();  
    }  
  
    @Override  
    public void mouseClicked(MouseEvent e) {  
          
        nextCode();  
    }  
  
    @Override  
    public void mousePressed(MouseEvent e) {  
        // TODO Auto-generated method stub  
          
    }  
  
    @Override  
    public void mouseReleased(MouseEvent e) {  
        // TODO Auto-generated method stub  
          
    }  
  
    @Override  
    public void mouseEntered(MouseEvent e) {  
        // TODO Auto-generated method stub  
          
    }  
  
    @Override  
    public void mouseExited(MouseEvent e) {  
        // TODO Auto-generated method stub  
          
    }  
}  

MD5加密方法

public void encrypByMd5(String context) {
	        try {
	            MessageDigest md = MessageDigest.getInstance("MD5");
	            md.update(context.getBytes());//update处理  
	            byte [] encryContext = md.digest();//调用该方法完成计算  
	 
	            int i;  
	            StringBuffer buf = new StringBuffer("");  
	            for (int offset = 0; offset < encryContext.length; offset++) {//做相应的转化(十六进制)
	                i = encryContext[offset];  
	                if (i < 0) i += 256;  
	                if (i < 16) buf.append("0");  
	                buf.append(Integer.toHexString(i));  
	           }  
	           System.out.println("32result: " + buf.toString());// 32位的加密
	        } catch (NoSuchAlgorithmException e) {  
	            // TODO Auto-generated catch block  
	            e.printStackTrace();  
	        }  
	    } 

那个管理员和学生界面还没写好呢明天接着写!
我觉得我下周就可以完成了嘎嘎嘎嘎(上周的flag这周存档了,下周再读档)

你可能感兴趣的:(大作业—JAVAGUI实现数据库管理系统(5))