一.登录界面
1.程序代码
1 import java.awt.*;//导入awt包
2 import javax.swing.*;//导入swing包
3 import java.awt.event.ActionListener;//导入awt包中的监听器事件包
4 import java.awt.event.ActionEvent;//导入awt包中的ActionEvent事件包
5
6 public class EnterScreen extends JFrame {
7 static int s=0;
8 public EnterScreen() {
9 Yanzhencode vcode = new Yanzhencode();
10 setSize(300,290);//设计窗体的大小
11 setTitle("请登录");
12 setBackground(Color.RED);//设置背景颜色
13 JLabel a=new JLabel("登录名"); //实例化JLabel对象
14 JLabel b=new JLabel("密 码");
15 JLabel g=new JLabel("忘记用户名/密码?");
16 JLabel h=new JLabel("验证码");
17 g.setForeground(Color.BLUE);
18 JTextField c=new JTextField(15);//实例化用户名文本框
19 JPasswordField d=new JPasswordField(15);//实例化密码框
20 JTextField k=new JTextField(4);//实例化验证码框
21 d.setEchoChar('*');//将输入密码框中的密码以*显示出来
22 JButton e=new JButton("登录");
23 JButton f=new JButton("快速注册");
24 e.setBackground(Color.YELLOW);//设置登录按钮字体颜色
25 f.setForeground(Color.GRAY);//设置快速登录按钮填充色
26 setVisible(true);//使窗体可视化
27 Container m=getContentPane();//获取一个容器
28 getContentPane().setBackground(Color.WHITE);//设置窗体填充色
29 // 将用户名、密码的Jlabel和用户名JTextField文本框、密码JPasswordField密码框以及确定JButton、快速注册JButton添加到container容器里面 //
30 m.add(a);
31 m.add(b);
32 m.add(c);
33 m.add(d);
34 m.add(e);
35 m.add(f);
36 m.add(g);
37 m.add(h);
38 m.add(k);
39 m.add(vcode);
40 setBounds(300,300,300,300);//设置窗体的长宽各为300、300 让其显示在左上方的300、300处
41 m.setLayout(null);
42 // a、b、c、d、e、f显示在container容器中的位置坐标
43 a.setBounds(10,40,50,18);
44 b.setBounds(10,80,50,18);
45 c.setBounds(60,40,130,18);
46 d.setBounds(60,80,130,18);
47 h.setBounds(10,120,50,18);
48 k.setBounds(60,120,80,18);
49 e.setBounds(90,180,100,30);
50 f.setBounds(90,220,100,30);
51 g.setBounds(190,75,100,30);
52 vcode.setBounds(140,110,80,30);
53 e.addActionListener(new ActionListener() {//对登录按钮添加监听事件
54
55 @SuppressWarnings("deprecation")
56 @Override
57 public void actionPerformed(ActionEvent arg0) {
58 // TODO Auto-generated method stub
59
60 if(c.getText().trim().equals("xiaoyang")&&new String(d.getPassword()).equals("123456")&&s==1) {//equals函数进行用户名和密码的匹配
61 JOptionPane.showMessageDialog(null,"登录成功");
62
63 new NewFrame();//进入到NewFrame这个窗体中
64 }else if(c.getText().trim().equals("xiaoyang")&&new String(d.getPassword()).equals("123456")&&s==0) {
65 JOptionPane.showMessageDialog(null,"验证码输入错误");
66 }else {
67 JOptionPane.showMessageDialog(null, "登录失败,用户名、密码或验证码输入错误");
68 }
69 }
70
71 });
72 f.addActionListener(new ActionListener(){//对快速注册按钮添加监听事件
73 @SuppressWarnings("deprecation")
74 @Override
75 public void actionPerformed(ActionEvent arg0) {
76 // TODO Auto-generated method stub
77 new zhuce();//进入都到zhuce窗体中
78 }
79
80 });
81 //判断输入验证码是否正确
82 if(k.getText()== null) {
83 s=0;
84 }else if(vcode.getCode() == null) {
85 s=1;
86 }else if(vcode.getCode() .equals(k.getText())) {
87 s=1;
88 }else {
89 s=0;
90 }
91 }
92
93 public static void main(String[] args) {
94 new EnterScreen();
95
96 }
97
98 }
2.登录界面截图
二.快速注册界面
1.程序代码
1 import java.awt.*;
2 import javax.swing.*;
3 import java.awt.event.ActionListener;
4 import java.awt.event.ActionEvent;
5 import java.sql.*;
6 import java.awt.Container;
7
8 import javax.swing.JButton;
9 import javax.swing.JLabel;
10 import javax.swing.JPasswordField;
11 import javax.swing.JTextField;
12
13 public class zhuce extends JFrame {
14 public zhuce() {
15 setSize(300,250);//设计窗体的大小
16 JLabel a=new JLabel("用户名"); //实例化JLabel对象
17 JLabel b=new JLabel("密 码");
18 JLabel h=new JLabel("再次确认密码");
19 JTextField c=new JTextField(15);//实例化用户名文本框
20 JPasswordField d=new JPasswordField(15);//实例化密码文本框
21 JPasswordField hd=new JPasswordField(15);//实例化密码文本框
22 d.setEchoChar('*');//设置密码隐藏制度
23 JButton e=new JButton("确定");
24 JButton f=new JButton("重置");
25 JButton g=new JButton("返回");
26 setVisible(true);
27 //获取一个容器
28 Container m=getContentPane();
29 // 将用户名、密码的Jlabel和用户名JTextField文本框、密码JPasswordField密码框以及确定JButton、快速注册JButton添加到container容器里面
30 m.add(a);
31 m.add(b);
32 m.add(h);
33 m.add(hd);
34 m.add(c);
35 m.add(d);
36 m.add(e);
37 m.add(f);
38 m.add(g);
39 //设置窗体的长宽各为300、250 让其显示在左上方的300、250处
40 setBounds(300,250,300,250);
41 m.setLayout(null);
42 // a、b、c、d、e、f显示在container容器中的位置坐标
43 a.setBounds(10,40,50,18);
44 b.setBounds(10,80,50,18);
45 h.setBounds(5,120,80,18);
46 c.setBounds(60,40,200,18);
47 d.setBounds(60,80,200,18);
48 hd.setBounds(90,120,180,18);
49 e.setBounds(110,160,60,30);
50 f.setBounds(30,160,60,30);
51 g.setBounds(190,160,60,30);
52 g.addActionListener(new ActionListener() {//对返回按钮添加监听事件
53 @SuppressWarnings("deprecation")
54 @Override
55 public void actionPerformed(ActionEvent arg0) {
56 // TODO Auto-generated method stub
57 new EnterScreen();
58
59 }
60 });
61 f.addActionListener(new ActionListener() {//对确认按钮添加监听事件
62 @SuppressWarnings("deprecation")
63 @Override
64 public void actionPerformed(ActionEvent arg0) {
65 // TODO Auto-generated method stub
66 new EnterScreen();
67
68 }
69 });
70 e.addActionListener(new ActionListener() {//对确认按钮添加监听事件
71 @SuppressWarnings("deprecation")
72 @Override
73 public void actionPerformed(ActionEvent arg0) {
74 // TODO Auto-generated method stub
75 new zhuce();
76
77 }
78 });
79 }
80 }
2.快速注册界面截图
三.验证码部分
1.验证码程序
1 import java.awt.Color;
2 import java.awt.Dimension;
3 import java.awt.Font;
4 import java.awt.FontMetrics;
5 import java.awt.Graphics;
6 import java.awt.Graphics2D;
7 import java.awt.event.MouseEvent;
8 import java.awt.event.MouseListener;
9
10 import java.util.Random;
11
12 import javax.swing.JComponent;
13
14 public class Yanzhencode extends JComponent implements MouseListener {
15
16 private String codes; //自动生成的验证码
17
18 private int width, height = 40; //设置验证码高度、宽度
19
20 private int codesLength = 4; //设置代码长度
21
22 private Random random = new Random(); //生成数字的方法
23
24 public Yanzhencode() {
25 width = this.codesLength * 16 + (this.codesLength - 1) * 10; //根据验证码长度设置宽度
26 setPreferredSize(new Dimension(width, height)); //设置背景大小
27 setSize(width, height); //设置验证码长度和宽度
28 this.addMouseListener(this);
29 setToolTipText("点击可更换验证码");
30 }
31 //得到生成的验证码
32 public int getCodesLength() {
33 return codesLength;
34 }
35
36
37 //设置验证码的长度
38 public void setCodesLength(int codeLength) {
39 if(codesLength < 4) {
40 this.codesLength = 4;
41 } else {
42 this.codesLength = codeLength;
43 }
44
45 }
46
47 public String getCode() {
48 return codes;
49 }
50
51
52 //让验证码产生随机的颜色
53 public Color getRandColor(int min, int max) {
54
55 if (min > 255)
56 min = 255;
57 if (max > 255)
58 max = 255;
59 int red = random.nextInt(max - min) + min;
60 int green = random.nextInt(max - min) + min;
61 int blue = random.nextInt(max - min) + min;
62 return new Color(red, green, blue);
63 }
64 // 设置验证码具体的数字或字母是什么
65 protected String generateCode() {
66 char[] codes = new char[this.codesLength];
67 for (int i = 0, len = codes.length; i < len; i++) {
68 if (random.nextBoolean()) {
69 codes[i] = (char) (random.nextInt(10) + 48);
70 } else {
71 codes[i] = (char) (random.nextInt(26) + 97);
72 }
73 }
74 this.codes = new String(codes);
75 return this.codes;
76 }
77
78
79 @Override
80 protected void paintComponent(Graphics g) {
81 super.paintComponent(g);
82 if(this.codes == null || this.codes.length() != this.codesLength) { //判断生成的验证码是否为空或超出长度
83 this.codes = generateCode();
84 }
85 width = this.codesLength * 16 + (this.codesLength - 1) * 10;
86 super.setSize(width, height); //接口使用,验证码字体大小
87 super.setPreferredSize(new Dimension(width, height));//接口使用,验证码背景大小
88 Font mFont = new Font("Arial", Font.BOLD | Font.ITALIC, 25); //设置字体和字体大小
89 g.setFont(mFont); //设置对象
90 //绘制出验证码的背景的矩形轮廓
91 Graphics2D g2d = (Graphics2D) g;
92 g2d.setColor(getRandColor(200, 250));
93 g2d.fillRect(0, 0, width, height);
94 g2d.setColor(getRandColor(180, 200));
95 g2d.drawRect(0, 0, width - 1, height - 1);
96 //绘制出验证码背景的线
97 int i = 0, len = 150;
98 for (; i < len; i++) {
99 int x = random.nextInt(width - 1);
100 int y = random.nextInt(height - 1);
101 int x1 = random.nextInt(width - 10) + 10;
102 int y1 = random.nextInt(height - 4) + 4;
103 g2d.setColor(getRandColor(180, 200));
104 g2d.drawLine(x, y, x1, y1);
105 }
106
107
108
109 //绘制出验证码的具体字母
110 i = 0; len = this.codesLength;
111 FontMetrics fm = g2d.getFontMetrics();
112 int base = (height - fm.getHeight())/2 + fm.getAscent();
113 for(;i
114 int b = random.nextBoolean() ? 1 : -1;
115 g2d.rotate(random.nextInt(10)*0.01*b);
116 g2d.setColor(getRandColor(20, 130));
117 g2d.drawString(codes.charAt(i)+"", 16 * i + 10, base);
118 }
119 }
120
121 //下一个验证码
122 public void nextCode() {
123 generateCode();
124 repaint();
125 }
126
127 @Override
128 public void mouseClicked(MouseEvent e) {
129
130 nextCode();
131 }
132
133 @Override
134 public void mousePressed(MouseEvent e) {
135 // TODO Auto-generated method stub
136
137 }
138
139 @Override
140 public void mouseReleased(MouseEvent e) {
141 // TODO Auto-generated method stub
142
143 }
144
145 @Override
146 public void mouseEntered(MouseEvent e) {
147 // TODO Auto-generated method stub
148
149 }
150
151 @Override
152 public void mouseExited(MouseEvent e) {
153 // TODO Auto-generated method stub
154
155 }
156 }
2.验证码实现效果
点击验证码图片后,验证码会更换