使用java awt 制作登陆,注册界面(结合数据库MY SQL )使用mac系统的eclipse

起因是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);

}



登陆界面:


























输入正确时,点击登陆:




package com.shou.loginfjame; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.SQLException; import java.util.List; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.xml.bind.util.ValidationEventCollector; import com.shou.LoginUtil.LoginUser; import com.shou.dao.LoginDao; import com.shuo.util.ValidCode; public class LoginFjame extends JFrame implements ActionListener { private JFrame frame = new JFrame("登录"); private JPanel panel = new JPanel(); private JLabel tiel = new JLabel("龍丶逸小说登录系统"); // 创建标题 private JLabel userLabel = new JLabel("用户名:"); // 创建UserJLabel private JTextArea userText=new JTextArea("请输入内容",7, 30); // 获取登录名 private JLabel passLabel = new JLabel("密 码:"); // 创建PassJLabel private JPasswordField passText = new JPasswordField(20); // 密码框隐藏 private JLabel verCodeLa = new JLabel("验证码:"); // 验证码 private JTextField inputCode = new JTextField(); // 验证码框 private ValidCode vcode = new ValidCode(); // 验证码内容 JTextField jt_code; private JButton loginButton = new JButton("登录"); // 创建登录按钮 private JButton registerButton = new JButton("注 册"); // 创建注册按钮 private JButton newPasswordButton = new JButton("忘记密码"); // 创建注册按钮 private JButton exitButton = new JButton("退出"); JTextField field = null; public LoginFjame() { System.out.println("====================================="); System.out.println("== 龍丶逸小说系统 =="); System.out.println("== V1.1.1.0 =="); System.out.println("====================================="); WinLogin(); } public void WinLogin() { panel.setLayout(null); // 设置布局为 null // 创建标题名称 this.tiel.setFont(new Font("宋体", 1, 20)); this.tiel.setBounds(150, 30, 300, 25); this.panel.add(this.tiel); // 创建 UserJLabel this.userLabel.setFont(new Font("宋体", 1, 13)); this.userLabel.setBounds(70, 80, 80, 25); this.panel.add(userLabel); // 创建文本域用于用户输入 this.userText.setBounds(145, 80, 165, 25); this.panel.add(this.userText); // 注册 this.registerButton.setFont(new Font("宋体", 1, 15)); this.registerButton.setContentAreaFilled(false); this.registerButton.setBorderPainted(false); /* registerButton.setBackground(Color.red); */ this.registerButton.setBounds(320, 80, 100, 25); this.panel.add(this.registerButton); // 变成小手 this.registerButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 创建PassJLabel this.passLabel.setFont(new Font("宋体", 1, 13)); this.passLabel.setBounds(70, 110, 80, 25); this.panel.add(this.passLabel); // 密码输入框 隐藏 this.passText.setBounds(145, 110, 165, 25); this.panel.add(this.passText); // 忘记密码 this.newPasswordButton.setFont(new Font("宋体", 1, 15)); this.newPasswordButton.setContentAreaFilled(false); this.newPasswordButton.setBorderPainted(false); /* registerButton.setBackground(Color.red); */ this.newPasswordButton.setBounds(320, 110, 100, 25); this.panel.add(this.newPasswordButton); // 变成小手 this.newPasswordButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 验证码code this.verCodeLa.setFont(new Font("宋体", 1, 13)); this.verCodeLa.setBounds(70, 140, 80, 25); this.panel.add(this.verCodeLa); // 验证码框 this.inputCode.setBounds(145, 140, 165, 25); this.panel.add(this.inputCode); // 验证码图片 this.vcode.setBounds(320, 140, 165, 25); this.panel.add(this.vcode); System.out.println(this.vcode); // 创建登录按钮 this.loginButton.setFont(new Font("宋体", 1, 15)); this.loginButton.setBounds(95, 190, 80, 25); this.panel.add(this.loginButton); // 变成小手 this.loginButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 退出按钮 this.exitButton.setFont(new Font("宋体", 1, 15)); this.exitButton.setBounds(230, 190, 80, 25); this.panel.add(this.exitButton); // 变成小手 this.exitButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); // 设置窗体的位置及大小 this.frame.setSize(460, 355); frame.setLocationRelativeTo(null); // 在屏幕中居中显示 frame.add(this.panel); // 添加面板 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置X号后关闭 //设置按钮 this.registerButton.addActionListener(this); //注册按钮 this.newPasswordButton.addActionListener(this); //忘记密码 this.loginButton.addActionListener(this); //登录 this.exitButton.addActionListener(this); //退出 // 往窗体里放其他控件 frame.setVisible(true); // 设置窗体可见 } @Override public void actionPerformed(ActionEvent e) { JButton bt = (JButton) e.getSource(); // 获取按钮信息 String str = bt.getText(); // 获取用户名 String name = this.userText.getText().trim(); // 获取密码 String password = this.passText.getText().trim(); // 获取验证码 String code = this.inputCode.getText().trim(); // 获取jsp验证码 String vcode = this.vcode.getCode(); // 登录 if (str.equals("登录")) { System.out.println("登录"); // 验证码转为大写 String Dcode = code.toUpperCase(); String Dvcode = vcode.toUpperCase(); // 验证码判断 if (Dcode.equals(Dvcode)) { //获取页面的用户名 String username=this.userText.getText().trim(); // 根据用户名查看是否有该用户 try { List loginUser=new LoginDao().queryAll(username); String a=loginUser.toString(); System.out.println(a.toString()); if(!a.toString().equals("[]")){ //密码判断 String mysqlPasword=loginUser.get(0).l_password(); if(mysqlPasword.equals(password)){ //登录成功 JOptionPane pane = new JOptionPane("登录成功"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); }else{ JOptionPane pane = new JOptionPane("密码错误错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); } }else{ JOptionPane pane = new JOptionPane("用户名错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); dialog.show(); } /*System.out.println(loginUser.toString()); String sqlUername=loginUser.get(0).getL_username();*/ /*int sqlpassword=loginUser.get(0).getL_power();*/ /*System.out.println("loginF:"+sqlUername);*/ } catch (SQLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } else { JOptionPane pane = new JOptionPane("验证码错误,请重新输入"); JDialog dialog = pane.createDialog(this, "警告"); System.out.println(dialog.getFont()); dialog.show(); } } else // 退出 if (str.equals("退出")) { System.out.println("退出"); System.exit(0); } else // 注册 if (str.equals("注 册")) { System.out.println("注 册"); } // 注册 else if (str.equals("忘记密码")) { System.out.println("忘记密码"); } else { System.out.println("异常错误"); } } public boolean isValidCodeRight() { System.out.println(this.jt_code.getText()); if (this.jt_code == null) { return false; } if (this.vcode == null) { return true; } if (this.vcode.getCode().equals(this.jt_code.getText())) { return true; } return false; } public static void main(String[] args) { new LoginFjame(); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值