起因是java课程的第四次大作业,还挺想做一个功能比较高大的,完善的登陆注册小系统的,而且最近在研究如何将应用与数据库连接在一起,经过个把小时的奋斗,以及在网上东拼西凑的代码,勉强满足想法。其中验证码的部分主要采用了ColorfulCAPTCHALabel和CAPTCHALabel两个类,嗯,这是参看网上的,可以总结一下。
写的可能不太好,原谅一个第一次写博文的小姐姐。
主要采用JFrame等组件,将登陆界面划分为上中下, 用JPanel进行布局,同时为防止用其他病毒或者软件自动申请用户及自动登陆,写了一个简单的Label类生成较为简单的验证码数字和字母组合的验证码,此外,我还结合数据库MySQL进行登陆验证,若没有账号,可以在注册界面进行注册。
目录结构:
彩色验证码类(注册用):
package login;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JLabel;
import org.apache.commons.lang.math.RandomUtils;
public class ColorfulCAPTCHALabelextends JLabel {
private staticfinal longserialVersionUID = -963570191302793615L;
private String text;//用于保存生成验证图片的字符串
private Color[] colors = { Color.BLACK, Color.BLUE, Color.CYAN, Color.DARK_GRAY, Color.GRAY, Color.GREEN, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE,
Color.PINK, Color.RED, Color.WHITE, Color.YELLOW };// 定义画笔颜色数组
public ColorfulCAPTCHALabel(Stringtext) {
this.text =text;
setPreferredSize(new Dimension(60, 36));//设置标签的大小
}
@Override
public void paint(Graphicsg) {
super.paint(g);//调用父类的构造方法
g.setFont(new Font("微软雅黑", Font.PLAIN, 16));// 设置字体
for (inti = 0; i <text.length(); i++) {
g.setColor(colors[RandomUtils.nextInt(colors.length)]);
g.drawString("" +text.charAt(i), 5 +i * 13, 25);//绘制字符串
}
}
}
黑色验证码类(登陆用):
package login;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JLabel;
public class CAPTCHALabelextends JLabel {
privatestatic finallong serialVersionUID = -963570191302793615L;
private Stringtext;// 用于保存生成验证图片的字符串
public CAPTCHALabel(Stringtext) {
this.text =text;
setPreferredSize(new Dimension(60, 36));//设置标签的大小
}
@Override
publicvoid paint(Graphics g) {
super.paint(g);//调用父类的构造方法
g.setFont(new Font("微软雅黑", Font.PLAIN, 16));//设置字体
g.drawString(text, 5, 25);//绘制字符串
}
}
Regist类:
package login;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import org.apache.commons.lang.RandomStringUtils;
import login.User;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.text.AbstractDocument;
import javax.swing.text.DocumentFilter;
import org.apache.commons.lang.RandomStringUtils;
import Util.DBHelper;
import com.mysql.fabric.xmlrpc.base.Array;
import javax.swing.JOptionPane;
public class Registextends JFrame {
/**
*
*/
privatestatic finallong serialVersionUID = 2491294229716316338L;
private JPanelcontentPane;
private JTextFieldusernameTextField;
private JPasswordFieldpasswordField1;
private JPasswordFieldpasswordField2;
private JTextFieldemailTextField;
private JTextFieldvalidateTextField ;
private JLabeltipLabel = new JLabel();//显示提示信息
/**
* Launch the application.
*/
/**
* Create the frame.
*/
public Regist() {
setTitle("注册");
this.setDefaultCloseOperation(Login.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
contentPane =new JPanel();
Graphics g_content =contentPane.getGraphics();
setContentPane(contentPane);
contentPane.setLayout((LayoutManager)new BoxLayout(contentPane, BoxLayout.PAGE_AXIS));
JPanel usernamePanel =new JPanel();
Graphics g_username =usernamePanel.getGraphics();
contentPane.add(usernamePanel);
JLabel usernameLabel =new JLabel("用户:");
usernameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 15));
usernamePanel.add(usernameLabel);
usernameTextField =new JTextField();
usernameTextField.setToolTipText("输入用户名");
usernameTextField.setFont(new Font("微软雅黑", Font.PLAIN, 15));
usernamePanel.add(usernameTextField);
usernameTextField.setColumns(10);
JPanel passwordPanel1 =new JPanel();
Graphics g_password =passwordPanel1.getGraphics();
contentPane.add(passwordPanel1);
JLabel passwordLabel1 =new JLabel("密码:");
passwordLabel1.setFont(new Font("微软雅黑", Font.PLAIN, 15));
passwordPanel1.add(passwordLabel1);
passwordField1 =new JPasswordField();
passwordField1.setFont(new Font("微软雅黑", Font.PLAIN, 15));
passwordField1.setColumns(10);
passwordPanel1.add(passwordField1);
JPanel passwordPanel2 =new JPanel();
Graphics g_password2 =passwordPanel2.getGraphics();
contentPane.add(passwordPanel2);
JLabel passwordLabel2 =new JLabel("密码:");
passwordLabel2.setFont(new Font("微软雅黑", Font.PLAIN, 15));
passwordPanel2.add(passwordLabel2);
passwordField2 =new JPasswordField();
passwordField2.setFont(new Font("微软雅黑", Font.PLAIN, 15));
passwordField2.setColumns(10);
passwordPanel2.add(passwordField2);
JPanel emailPanel =new JPanel();
Graphics g_email =emailPanel.getGraphics();
contentPane.add(emailPanel);
JLabel emailLabel =new JLabel("电子邮箱");
emailLabel.setFont(new Font("微软雅黑", Font.PLAIN, 15));
emailPanel.add(emailLabel);
emailTextField =new JTextField();
emailTextField.setFont(new Font("微软雅黑", Font.PLAIN, 15));
emailPanel.add(emailTextField);
emailTextField.setColumns(10);
JPanel validatePanel =new JPanel();
Graphics g_validate =validatePanel.getGraphics();
contentPane.add(validatePanel);
JLabel validateLabel =new JLabel("验证码");
validateLabel.setFont(new Font("微软雅黑", Font.PLAIN, 15));
validatePanel.add(validateLabel);
JTextField validateTextField =new JTextField();
validateTextField.setFont(new Font("微软雅黑", Font.PLAIN, 15));
validatePanel.add(validateTextField);
validateTextField.setColumns(5);
String randomText = RandomStringUtils.randomAlphanumeric(4);
ColorfulCAPTCHALabel label = new ColorfulCAPTCHALabel(randomText);//随机验证码
validatePanel.add(label);
JPanel buttonPanel =new JPanel();
contentPane.add(buttonPanel);
JButton submitButton =new JButton("提交");
submitButton.addActionListener(new ActionListener() {
@Override
publicvoid actionPerformed(ActionEvent e) {
//TODO Auto-generated method stub
do_submitButton_actionPerformed(e);
}
privatevoid do_submitButton_actionPerformed(ActionEvente) {
//TODO Auto-generated method stub
//Array[] Arrays=new Array[]();
User user = new User();
String username=usernameTextField.getText();
char[]password1=passwordField1.getPassword();
char[]password2=passwordField2.getPassword();
user.setUsername(username);
user.setPassword(new String(password1));
user.setEmail(emailTextField.getText().trim());
if(!Arrays.equals(password1,password2))
{
JOptionPane.showMessageDialog(Regist.this,"密码不同,请重新输入","提示信息", JOptionPane.INFORMATION_MESSAGE);
return;
}
Arrays.fill(password1,'0');// 清空保存密码的字符数组
Arrays.fill(password2,'0');// 清空保存密码的字符数组
if (DBHelper.save(user)) {
JOptionPane.showMessageDialog(Regist.this,"用户注册成功!","提示信息", JOptionPane.INFORMATION_MESSAGE);
return;
} else {
JOptionPane.showMessageDialog(Regist.this,"用户注册失败!","警告信息", JOptionPane.WARNING_MESSAGE);
return;
}
}
});
buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
tipLabel.setFont(new Font("微软雅黑", Font.PLAIN, 15));
buttonPanel.add(tipLabel);
Component glue = Box.createGlue();
buttonPanel.add(glue);
submitButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));
buttonPanel.add(submitButton);
JButton cancelButton =new JButton("取消");
cancelButton.addActionListener(new ActionListener() {
@Override
publicvoid actionPerformed(ActionEvent e) {
do_cancelButton_actionPerformed(e);
}
privatevoid do_cancelButton_actionPerformed(ActionEvente) {
//TODO Auto-generated method stub
Regist.this.dispose();
Login myLogin=new Login();
myLogin.setVisible(true);
return;
}
});
cancelButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));
buttonPanel.add(cancelButton);
pack();//自动调整窗体大小
setLocation(SwingUtil.centreContainer(getSize()));//让窗体居中显示
}
}
login 类:
package login;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import login.Regist;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import org.apache.commons.lang.RandomStringUtils;
import Util.DBHelper;
import Util.DBConfig;
public classLogin extends JFrame{
private StringrandomText;
JLabel myJLabel1 =new JLabel("用户:");
JTextField myTextField=new JTextField();
JLabel myJLabel3 =new JLabel("密码:");
JPasswordField myPasswordField=new JPasswordField();
JLabel myJLabel4 =new JLabel("忘记密码");
JButton myForget=new JButton("忘记密码");
JButton myRegister=new JButton("注册");
JButton myLogin=new JButton("登陆");
JButton myCancel=new JButton("取消");
public Login(){
this.setTitle("登陆") ;
this.setSize(350,250);
this.setDefaultCloseOperation(Login.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
JPanel jp_up=new JPanel();
JPanel jp_center=new JPanel();
JPanel jp_down=new JPanel();
JPanel validatePanel =new JPanel();
this.add(jp_up,BorderLayout.NORTH);
this.add(jp_down,BorderLayout.SOUTH);
this.add(jp_center,BorderLayout.CENTER);
Graphics g_validate =validatePanel.getGraphics();
Graphics g_center =jp_center.getGraphics();
Graphics g_up =jp_up.getGraphics();
Graphics g_down =jp_down.getGraphics();
jp_up.add(myJLabel1);
jp_up.add(myTextField);
myTextField.setPreferredSize(new Dimension(175,25));
jp_center.add(myJLabel3);
jp_center.add(myPasswordField);
jp_center.add(validatePanel);
JLabel validateLabel =new JLabel("验证码");
validateLabel.setFont(new Font("微软雅黑", Font.PLAIN, 15));
validatePanel.add(validateLabel);
JTextField validateTextField =new JTextField();
validateTextField.setFont(new Font("微软雅黑", Font.PLAIN, 15));
validatePanel.add(validateTextField);
validateTextField.setColumns(5);
randomText = RandomStringUtils.randomAlphanumeric(4);
CAPTCHALabel label =new CAPTCHALabel(randomText);//随机验证码
label.setFont(new Font("微软雅黑", Font.PLAIN, 15));
validatePanel.add(label);
myRegister.addActionListener(new ActionListener(){
@Override
publicvoid actionPerformed(ActionEvent e) {
//TODO Auto-generated method stub
Login.this.dispose();//关闭登陆窗口
Registframe = new Regist();
frame.setVisible(true);
}
});
myForget.addActionListener(new ActionListener(){
@Override
publicvoid actionPerformed(ActionEvent e) {
//TODO Auto-generated method stub
Login.this.dispose();//关闭登陆窗口
Object[]options = {"确定","取消","帮助"};
//定制可供选择按钮
intresponse=JOptionPane.showOptionDialog(null,"请重新注册","选项对话框标题",JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, null, options,options[0]);
if(response==0) {
JOptionPane.showMessageDialog(null,"您按下了确定按钮","消 息",JOptionPane.INFORMATION_MESSAGE);//消息对话框
Regist frame = new Regist();
frame.setVisible(true);
}else if(response==1) {
JOptionPane.showMessageDialog(null,"您按下了取消按钮","消息",JOptionPane.INFORMATION_MESSAGE);
return;
}else if(response==2) {
JOptionPane.showMessageDialog(null,"您按下了帮助按钮","消息",JOptionPane.INFORMATION_MESSAGE);
}
}
});
myPasswordField.setPreferredSize(new Dimension(175,25));
jp_down.add(myJLabel4);
jp_down.setLayout(new BorderLayout());
jp_down.add(myForget, BorderLayout.WEST);
jp_down.add(myRegister, BorderLayout.CENTER);
jp_down.add(myLogin, BorderLayout.EAST);
//initConnection();
myLogin.addActionListener(new ActionListener(){
@SuppressWarnings("deprecation")
@Override
publicvoid actionPerformed(ActionEvent e) {
//TODO Auto-generated method stub
Stringuser=myTextField.getText();
char[]password = myPasswordField.getPassword();
Stringvalidate=validateTextField.getText();
if (user.equals("")) {//判断用户名是否为空
JOptionPane.showMessageDialog(Login.this,"用户名不能为空!","警告信息", JOptionPane.WARNING_MESSAGE);
return;
}
if (password.equals("")) {//判断密码是否为空
JOptionPane.showMessageDialog(Login.this,"密码不能为空!","警告信息", JOptionPane.WARNING_MESSAGE);
return;
}
if (!DBHelper.exists(user)) {//如果用户名不存在则进行提示
JOptionPane.showMessageDialog(Login.this,"用户名不存在!","警告信息", JOptionPane.WARNING_MESSAGE);
return;
}
if (!DBHelper.check(user,password)) {//如果密码错误则进行提示
JOptionPane.showMessageDialog(Login.this,"密码错误!","警告信息", JOptionPane.WARNING_MESSAGE);
return;
}
else
{
if (!validate.equals(randomText)) {// 如果校验码不匹配则进行提示
JOptionPane.showMessageDialog(Login.this,"验证码错误!","警告信息", JOptionPane.WARNING_MESSAGE);
return;
}
else{
JOptionPane.showMessageDialog(Login.this,"用户登陆成功");
}
}
Login.this.dispose();//关闭登陆窗口
}});}
publicstatic void main(String[]args) {
Login myLogin=new Login();
myLogin.setVisible(true);
}
}
登陆界面:
输入正确时,点击登陆: