Java Swing实现登陆窗口验证码

Java Swing实现登陆窗口验证码_第1张图片
Java Swing实现如图窗口界面,点击右侧【看不清,换一张】可更换验证码。

Code.java

package com.A;


import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.Border;


public class Code extends JFrame {

	public class RoundBorder implements Border {
		 
		 public Insets getBorderInsets(Component c) {
		  return new Insets(0,0,0,0);
		 }
		 public boolean isBorderOpaque() {
		  return false;
		 }
		 //绘制圆角按钮
		 public void paintBorder(Component c, Graphics g, int x, int y,
		   int width, int height) {
		  //使用黑色在组件的外边缘绘制一个圆角矩形
		  g.setColor(Color.BLACK);
		  g.drawRoundRect(0, 0, c.getWidth()-1, c.getHeight()-1, 12, 12);
		 }
		}
	
	public void CreateJFrame() {
	//界面背景图片
		ImageIcon icon=new ImageIcon("src//images//background.jpg");
		//Image im=new Image(icon);
		//将图片放入label中
		JLabel label=new JLabel(icon);
		
		//设置label的大小
		label.setBounds(0,0,icon.getIconWidth(),icon.getIconHeight());
		//窗口名称
		JFrame jf = new JFrame("用户登陆");
		jf.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
		JPanel j=(JPanel)jf.getContentPane();
		j.setOpaque(false);
		
		JPanel panel=new JPanel();
		panel.setOpaque(false);

		
		placeComponents(panel);
		
		jf.add(panel);
		jf.setVisible(true);
		jf.setSize(icon.getIconWidth(),icon.getIconHeight());
		jf.setResizable(false);
		//在屏幕中设置显示的位置
		jf.setLocation(700,500);
		jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
	}
	//设置窗口内各种组件
	public void placeComponents(JPanel panel) {
		panel.setLayout(null);
		JLabel label1 = new JLabel("用户名");
		Font zi=new Font("黑体",Font.TYPE1_FONT,20);
		label1.setFont(zi);
		label1.setBounds(155, 179, 70, 40);
		panel.add(label1);
		
		JTextField text1 = new JTextField(20);
		text1.setBounds(240,187,175,30);
		//监听,当鼠标点击按钮会变色
		text1.addMouseListener(new MouseListener () {
		    private Border oldBorder;  
		    @Override
		    public void mouseClicked(MouseEvent e) {
		    }
		    @Override
		    public void mousePressed(MouseEvent e) {
		    }
		    @Override
		    public void mouseReleased(MouseEvent e) {
		    }
		    @Override
		    public void mouseEntered(MouseEvent e) {	                 
		        // 保存旧边框
		        oldBorder = text1.getBorder(); 
		        // 设置一个新边框
		        text1.setBorder(BorderFactory.createLineBorder(new Color(0,128,255)));
		    }
		    @Override
		    public void mouseExited(MouseEvent e) {
		        // 还原旧边框
		        text1.setBorder(oldBorder);
		    }
		});
		panel.add(text1);
		
		JLabel label2 = new JLabel("密码");
		Font zi1=new Font("黑体",Font.TYPE1_FONT,20);
		label2.setFont(zi1);
		label2.setBounds(155, 237, 70, 40);
		panel.add(label2);
		
		JTextField text2 = new JTextField(20);
		text2.setBounds(240,242,175,30);
		text2.addMouseListener(new MouseListener () {
		    private Border oldBorder;  
		    @Override
		    public void mouseClicked(MouseEvent e) {
		    }
		    @Override
		    public void mousePressed(MouseEvent e) {
		    }
		    @Override
		    public void mouseReleased(MouseEvent e) {
		    }
		    @Override
		    public void mouseEntered(MouseEvent e) {	                 
		        // 保存旧边框
		        oldBorder = text2.getBorder(); 
		        // 设置一个新边框
		        text2.setBorder(BorderFactory.createLineBorder(new Color(0,128,255)));
		    }
		    @Override
		    public void mouseExited(MouseEvent e) {
		        // 还原旧边框
		        text2.setBorder(oldBorder);
		    }
		});
		panel.add(text2);
		
		JLabel label3 = new JLabel("验证码");
		Font zi2=new Font("黑体",Font.TYPE1_FONT,20);
		label3.setFont(zi2);
		label3.setBounds(155, 290, 70, 40);
		panel.add(label3);
		
		JTextField text3 = new JTextField(20);
		text3.setBounds(240,295,175,30);
		text3.addMouseListener(new MouseListener () {
		    private Border oldBorder;  
		    @Override
		    public void mouseClicked(MouseEvent e) {
		    }
		    @Override
		    public void mousePressed(MouseEvent e) {
		    }
		    @Override
		    public void mouseReleased(MouseEvent e) {
		    }
		    @Override
		    public void mouseEntered(MouseEvent e) {	                 
		        // 保存旧边框
		        oldBorder = text3.getBorder(); 
		        // 设置一个新边框
		        text3.setBorder(BorderFactory.createLineBorder(new Color(0,128,255)));
		    }
		    @Override
		    public void mouseExited(MouseEvent e) {
		        // 还原旧边框
		        text3.setBorder(oldBorder);
		    }
		});
		panel.add(text3);
		//设置单选按钮
		JRadioButton rb1=new JRadioButton("教师"); 
		ButtonGroup group=new ButtonGroup();
		Font zi3=new Font("黑体",Font.TYPE1_FONT,16);
		rb1.setFont(zi3);
		group.add(rb1);
		rb1.setSelected(true);
		rb1.setBounds(180, 360, 60, 25);
		rb1.setBackground(new Color(204,204,255));
		panel.add(rb1);
		//设置单选按钮
		JRadioButton rb2=new JRadioButton("学生"); 
		rb2.setFont(zi3);
		group.add(rb2);
		rb2.setSelected(true);
		rb2.setBounds(280, 360, 60, 25);
		rb2.setBackground(new Color(204,204,255));
		panel.add(rb2);
		//设置单选按钮
		JRadioButton rb3=new JRadioButton("访客"); 
		rb3.setFont(zi3);
		group.add(rb3);
		rb3.setSelected(true);
		rb3.setBounds(380, 360, 60, 25);
		rb3.setBackground(new Color(204,204,255));
		panel.add(rb3);
		
		JButton button1 = new JButton("登陆");
		button1.setFont(zi3);
		button1.setForeground(new Color(0,0,0));
		button1.setBounds(220, 400, 60, 30);
		button1.setBorder(new RoundBorder());
		panel.add(button1);
		
		JButton button2 = new JButton("重置");
		button2.setFont(zi3);
		button2.setForeground(new Color(0,0,0));
		button2.setBounds(340, 400, 60, 30);
		button2.setBorder(new RoundBorder());
		panel.add(button2);
		
		JLabel code = new JLabel();
		Object[] obj = CodePicture.createImage();
		ImageIcon img = new ImageIcon((BufferedImage)obj[1]);//创建图片对象
		code.setIcon((Icon)img);
		code.setBounds(450, 290,150, 50);
		panel.add(code);
		
		String   strMsg1   =   "看不清";   
		String   strMsg2   =   "换一张";   
		String   strMsg = ""+strMsg1+"
"+strMsg2+""; JLabel change = new JLabel(strMsg); //监听,鼠标进入文字就变成小手形状 change.addMouseListener(new MouseAdapter() { public void mouseEntered(MouseEvent e) //鼠标进入 { change.setForeground(Color.blue); change.setCursor(new Cursor(Cursor.HAND_CURSOR)); } public void mouseExited(MouseEvent e) //鼠标移除 { change.setForeground(Color.black); setCursor(Cursor.getDefaultCursor()); } }); change.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e){ if(e.getClickCount() == 1){ //重新获取验证码 Object[] obj1 = CodePicture.createImage(); ImageIcon img1 = new ImageIcon((BufferedImage)obj1[1]);//创建图片对象 code.setIcon((Icon)img1); code.setBounds(450, 290,150, 50); panel.add(code); } } }); change.setBounds(550,290,100,40); panel.add(change); } public static void main(String[] args) { new Code().CreateJFrame(); } }

CodePicture.java

package com.A;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;

//绘制验证码
public class CodePicture {
// 验证码字符集
private static final char[] chars = (“0123456789abcdefghijklmnopqrstuvwxyz”
+ “ABCDEFGHIJKLMNOPQRSTUVWXYZ”).toCharArray();
// 字符数量
private static final int SIZE = 4;
// 干扰线数量
private static final int LINES = 10;
// 宽度
private static final int WIDTH = 80;
// 高度
private static final int HEIGHT = 40;
// 字体大小
private static final int FONT_SIZE = 30;//30

/**
 * 生成随机验证码及图片
 */
public static  Object[] createImage() {
	StringBuffer sb = new StringBuffer();
	// 1.创建空白图片
	BufferedImage image = new BufferedImage(
		WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
	// 2.获取图片画笔
	Graphics graphic = image.getGraphics();
	// 3.设置画笔颜色
	graphic.setColor(Color.LIGHT_GRAY);
	// 4.绘制矩形背景
	graphic.fillRect(0, 0, WIDTH, HEIGHT);
	// 5.画随机字符
	Random ran = new Random();
	for (int i = 0; i 

}

附背景图片:

Java Swing实现登陆窗口验证码_第2张图片

你可能感兴趣的:(Java)