2019-07-03 19:52:32
第一篇初次写不太熟练,将一些基础的实现,记录下来。
本片代码主要有Java的桌面图形界面,Java I/O流,JDBC等等内容实现用户登录,在此地基础上添加了用户注册。代码相当的简陋,但是简单,容易理解。
数据库使用Start SampServer实现
源码:
加载数据库驱动,获取数据库连接
package 包位置
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcRe {
static {
String driverName="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/abc";
String name="root";
String passwd="";
}
public static void locadClass() throws ClassNotFoundException {
//加载驱动
String driverName="com.mysql.jdbc.Driver";
Class.forName(driverName);
}
public static Connection getConnection() throws Exception {
String url="jdbc:mysql://localhost:3306/abc";
String name="root";
String passwd="";
//获得连接
Connection conn = DriverManager.getConnection(url, name, passwd);
return conn;
}
public static void result(Connection conn, Statement stam) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
conn = null;
}
if (stam != null) {
try {
stam.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
stam = null;
}
}
}
注册代码
package 包位置
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class Register extends JFrame implements ActionListener{
JPanel p1,p2,p3,p4,p5,p6,p7,p8;
JLabel l1,l2,l3,l4,l5,l6,l7,l8;
JTextField j1,j4,j5,j6,j7;
JPasswordField j2,j3;
JRadioButton r1,r2;
JCheckBox ck1,ck2,ck3,ck4;
JTextArea adddr;
JComboBox degree;
JButton bu1,bu2;
Connection conn;
Statement stam;
public Register() {
super("用户注册");
this.setLayout(new GridLayout(8,1));
p1 =new JPanel(new FlowLayout(FlowLayout.LEFT));
l1 =new JLabel("用户名:");
j1=new JTextField(18);
p1.add(l1);
p1.add(j1);
this.add(p1);
p2 =new JPanel(new FlowLayout(FlowLayout.LEFT));
l2 =new JLabel("密码:");
j2=new JPasswordField(19);
p2.add(l2);
p2.add(j2);
this.add(p2);
p3 =new JPanel(new FlowLayout(FlowLayout.LEFT));
l3 =new JLabel("确认密码:");
j3=new JPasswordField(16);
p3.add(l3);
p3.add(j3);
this.add(p3);
p4 =new JPanel(new FlowLayout(FlowLayout.LEFT));
l4 =new JLabel("确性别:");
r1=new JRadioButton("男");
r2=new JRadioButton("女");
this.r1.setSelected(true);
p4.add(l4);
p4.add(r1);
p4.add(r2);
this.add(p4);
p5 =new JPanel(new FlowLayout(FlowLayout.LEFT));
l5 =new JLabel("爱好:");
ck1=new JCheckBox("阅读");
ck2=new JCheckBox("上网");
ck3=new JCheckBox("游泳");
ck4=new JCheckBox("旅游");
p5.add(l5);
p5.add(ck1);
p5.add(ck2);
p5.add(ck3);
p5.add(ck4);
this.add(p5);
p6 =new JPanel(new FlowLayout(FlowLayout.LEFT));
l6 =new JLabel("地址:");
adddr =new JTextArea(2,19);
p6.add(l6);
p6.add(adddr);
this.add(p6);
p7 =new JPanel(new FlowLayout(FlowLayout.LEFT));
l7 =new JLabel("学历:");
String str[]= {"小学","初中","高中","大学"};
degree=new JComboBox(str);
p7.add(l7);
p7.add(degree);
this.add(p7);
p8 =new JPanel(new FlowLayout(FlowLayout.CENTER));
bu1=new JButton("注册");
bu2=new JButton("取消");
p8.add(bu1);
p8.add(bu2);
this.add(p8);
//创建监听
awtEvent();
}
public static void main(String[] args) {
Register r=new Register();
r.setVisible(true);
r.setSize(300, 400);
r.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//监听
private void awtEvent() {
//单选框互斥
r1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(r1.isSelected()){
r2.setSelected(false);
}
}
});
r2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(r2.isSelected()){
r1.setSelected(false);
}
}
});
//注册按钮监听
bu1.addActionListener(this);
//取消按钮监听代码
bu2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
}
@Override
public void actionPerformed(ActionEvent e) {
//获取选取的性别
String s1=null;
if(r1.isSelected()) {
s1="男";
}else {
s1="女";
}
//获取复选框中的值
String info ="";
//通过面板属性名获取到该面板上的所有组件
for(Component c:p5.getComponents()){
//通过 instanceof方法筛选复选框组件
if(c instanceof JCheckBox){
//判断复选框组件是否被选中
if(((JCheckBox) c).isSelected()){
//获取该复选框组件信息
info += ((JCheckBox)c).getText();
}
}
}
//下拉列表框中的值
String x = degree.getSelectedItem().toString();
//判断用户名和密码是否为空
judge();
//判断用户名是否重复
userName();
//两次密码是否一致
pwdName();
if(e.getSource()==bu1) {
try {
//加载数据库驱动
conn=JdbcRe.getConnection();
//创建执行sql语句的对象
stam=conn.createStatement();
//编写sql语句
String sql="insert into user values('"+j1.getText()+"','"+j2.getText()+"','"+j3.getText()+"','"+s1+"','"+info+"','"+adddr.getText()+"','"+x+"')";
//执行sql语句
stam.execute(sql);
//提示框
JOptionPane.showMessageDialog(null, "注册成功!!!!!");
//关闭注册界面窗体
this.dispose();
//打开登录窗体
User user=new User();
user.setVisible(true);
user.setSize(600,500);
user.setLocation(600,200);
}catch (Exception e1) {
e1.printStackTrace();
}finally {
JdbcRe.result(conn, stam);
}
}
}
//判断用户名密码是否为空
private void judge() {
if (j1.getText().isEmpty()) {// 判断用户名是否为空
this.dispose();
JOptionPane.showMessageDialog(this, "用户名不能为空!", "警告信息", JOptionPane.WARNING_MESSAGE);
Register r=new Register();
r.setVisible(true);
r.setSize(300, 400);
r.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return;
}
if (new String(j2.getText()).isEmpty()) {// 判断密码是否为空
this.dispose();
JOptionPane.showMessageDialog(this, "密码不能为空!", "警告信息", JOptionPane.WARNING_MESSAGE);
Register r=new Register();
r.setVisible(true);
r.setSize(300, 400);
r.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return;
}
if (new String(j3.getText()).isEmpty()) {// 判断确认密码是否为空
this.dispose();
JOptionPane.showMessageDialog(this, "确认密码不能为空!", "警告信息", JOptionPane.WARNING_MESSAGE);
Register r=new Register();
r.setVisible(true);
r.setSize(300, 400);
r.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return;
}
}
//判断新的用户是否和原用户名有重复
private void userName() {
try {
conn=JdbcRe.getConnection();
stam=(Statement)conn.createStatement();
//编写SQL语句
String sql="select id num from User where id='"+j1.getText()+"'";
ResultSet rs=stam.executeQuery(sql);
int num=0;
if(rs!=null&&rs.next()) {
num=rs.getInt("num");
System.out.println(num);
}
//判断用户名是否有相同
if (num>0) {
JOptionPane.showMessageDialog(this, "用户名已使用,请重新选取","警告消息",JOptionPane.WARNING_MESSAGE);
this.dispose();
Register r=new Register();
r.setVisible(true);
r.setSize(300, 400);
r.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return;
}
} catch (Exception e) {
e.printStackTrace();
}
}
//判断相关用户的密码和确认密码相同
private void pwdName() {
if (j2.equals(j3)) {
JOptionPane.showMessageDialog(this, "两次密码不相同","警告消息",JOptionPane.WARNING_MESSAGE);
Register r=new Register();
r.setVisible(true);
r.setSize(300, 400);
r.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return;
}
}
}
用户登录界面代码
package 包位置
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Random;
import javax.swing.BorderFactory;
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.JRadioButton;
import javax.swing.JTextField;
import chat.ChatRoomClient;
import chat.ChatRoomServer;
import cn.jhc.util.ComStr;
import cn.jhc.util.RandomYzm;
import cn.jhc.view.MainWinow;
public class User extends JFrame implements ActionListener,MouseListener{
String rYzm;
JPanel p1,p2,p3,p4;
JLabel j1,j2,j3,lyzm;
JTextField t1,inYzm;
JPasswordField t2;
JButton b,b1,yzm,b2;
JRadioButton jr1;
Connection conn;
Statement stam;
public User() {
super("用户登录界面");
this.setLayout(new GridLayout(4,1));
p1=new JPanel(null);
p2=new JPanel(null);
p3=new JPanel(null);
p4=new JPanel(null);
JLabel jl1=new JLabel("大傻逼登录系统");
jl1.setFont(new Font("宋体",Font.BOLD,20));
jl1.setBounds(180,0, 170, 80);
j1=new JLabel("用户名:");
j1.setFont(new Font("宋体",Font.BOLD,20));
j1.setBounds(90,0,110,30);
j2=new JLabel("密 码:");
j2.setFont(new Font("宋体",Font.BOLD,20));
j2.setBounds(90,80,110,30);
lyzm =new JLabel("验证码:");
lyzm.setFont(new Font("宋体",Font.BOLD,20));
lyzm.setBounds(90,45,110,30);
//用于输入验证码的文本框
inYzm =new JTextField();
inYzm.setFont(new Font("宋体",Font.BOLD,20));
inYzm.setBounds(190,45,190,30);
//显示验证码的按钮
yzm=new JButton();
rYzm = RandomYzm.getYzm();
yzm.setFont(new Font("宋体",Font.BOLD,20));
yzm.setBounds(400,45,130,30);
yzm.setOpaque(false);
yzm.setBorder(BorderFactory.createCompoundBorder(null,null));
yzm.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
yzmAction(evt);
}
});
//用户名输入框
t1=new JTextField(9);
t1.setFont(new Font("宋体",Font.BOLD,20));
t1.setBounds(190,0,190,30);
//用户密码框
t2=new JPasswordField(9);
t2.setFont(new Font("宋体",Font.BOLD,20));
t2.setBounds(190,80,190,30);
b=new JButton("登录");
b.setFont(new Font("宋体",Font.BOLD,20));
b.setBounds(90, 20,290, 30);
b1=new JButton("注册");
b1.setFont(new Font("宋体",Font.BOLD,20));
b1.setBounds(400, 0, 130, 30);
b2=new JButton("修改密码");
b2.setFont(new Font("宋体",Font.BOLD,20));
b2.setBounds(400, 80, 130, 30);
jr1=new JRadioButton("记住密码");
jr1.setFont(new Font("宋体",Font.BOLD,20));
jr1.setBounds(400,20, 160, 30);
jr1.setSelected(false);
p1.add(jl1);
p2.add(j1);
p2.add(j2);
p2.add(b1);
p2.add(t1);
p2.add(t2);
p2.add(b2);
p3.add(lyzm);
p3.add(inYzm);
p3.add(yzm);
p4.add(b);
p4.add(jr1);
this.add(p1);
this.add(p2);
this.add(p3);
this.add(p4);
//创建监听
awtEvent();
//获取相应的用户名和密码
addPassword();
}
private void awtEvent() {
//登录按键监听
b.addActionListener(this);
//注册按键监听
b1.addMouseListener(this);
//修改密码监听
b2.addActionListener(this);
}
//用于刷新验证码
private void yzmAction(ActionEvent evt) {
//重新获取验证码
rYzm=RandomYzm.getYzm();
//设置显示的验证码
yzm.setText(rYzm);
}
private void addPassword() {
try {
BufferedReader buf=new BufferedReader(new FileReader("user.txt"));
String lien = buf.readLine();
if(lien!= null) {
String[] users=lien.split(" ");
t1.setText(users[0]);
t2.setText(users[1]);
}
buf.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
User user=new User();
user.setVisible(true);
user.setSize(600,500);
user.setLocation(600,200);
user.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b) {
try {
conn=JdbcRe.getConnection();
stam=(Statement)conn.createStatement();
//编写SQL语句
String sql="select id,pwd from User where id='"+t1.getText()+"'and pwd='"+t2.getText()+"'";
//执行SQL语句
ResultSet rs=stam.executeQuery(sql);
if(rs.next()) {
String get_InYzm=inYzm.getText();
System.out.println("获取到相应账号");
if(ComStr.compar2Str(get_InYzm, rYzm)){
System.out.println("验证码正确");
if(jr1.isSelected()){
FileWriter fWriter=new FileWriter("user.txt");
fWriter.write(t1.getText());
fWriter.write(" ");
fWriter.write(t2.getText());
fWriter.close();
}
this.dispose();//关闭窗口
String name=JOptionPane.showInputDialog(this,"请输入你的昵称:");
String num="ww";
//记住密码功能中用流将数据传到username.txt中
FileWriter f=new FileWriter("username.txt");
f.write(num);
f.write(" ");
f.write(name);
f.flush();
JOptionPane.showMessageDialog(null, "登录成功!");
}else {
JOptionPane.showMessageDialog(this, "验证码错误");
inYzm.setText("");
}
}else {
JOptionPane.showMessageDialog(null, "密码或账号错误,登录失败!","标题",JOptionPane.ERROR_MESSAGE);
t1.setText("");
t2.setText("");
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
if(e.getSource()==b2) {
this.dispose();
UserTrue userTrue=new UserTrue();
userTrue.setVisible(true);
userTrue.setSize(370,300);
userTrue.setLocation(300,300);
}
}
@Override
public void mouseClicked(MouseEvent e) {
this.dispose();
Register register=new Register();
register.setVisible(true);
register.setSize(300,300);
register.setLocation(100,100);
}
@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
}
}
用户是否存在验证
package 包位置;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
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;
public class UserTrue extends JFrame implements ActionListener{
JPanel p1;
JLabel l1;
JTextField j1;
JPanel p2;
JLabel l2;
JPasswordField j2;
JPanel p3;
JLabel l3;
JPasswordField j3;
JPanel p4;
JButton bu1;
Connection conn;
Statement stam;
public UserTrue() {
super("用户验证");
this.setLayout(new GridLayout(4,1));
p1 =new JPanel(null);
l1 =new JLabel("用 户 名:");
l1.setFont(new Font("宋体",Font.BOLD,20));
l1.setBounds(50, 20, 120, 30);
j1=new JTextField(10);
j1.setFont(new Font("宋体",Font.BOLD,20));
j1.setBounds(160, 20, 130, 30);
p1.add(l1);
p1.add(j1);
this.add(p1);
p2 =new JPanel(null);
l2 =new JLabel("密 码:");
l2.setFont(new Font("宋体",Font.BOLD,20));
l2.setBounds(50, 20, 120, 30);
j2=new JPasswordField(10);
j2.setFont(new Font("宋体",Font.BOLD,20));
j2.setBounds(160, 20, 130, 30);
p2.add(l2);
p2.add(j2);
this.add(p2);
p3 =new JPanel(null);
l3 =new JLabel("确认密码:");
l3.setFont(new Font("宋体",Font.BOLD,20));
l3.setBounds(50, 20, 120, 30);
j3=new JPasswordField(10);
j3.setFont(new Font("宋体",Font.BOLD,20));
j3.setBounds(160, 20, 130, 30);
p3.add(l3);
p3.add(j3);
this.add(p3);
p4 =new JPanel(null);
bu1=new JButton("确认");
bu1.addActionListener(this);
bu1.setBounds(100, 20, 120,30);
p4.add(bu1);
this.add(p4);
}
public static void main(String[] args) {
UserTrue userTrue=new UserTrue();
userTrue.setVisible(true);
userTrue.setSize(370,300);
userTrue.setLocation(300,300);
userTrue.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==bu1) {
try {
//加载驱动
conn=JdbcRe.getConnection();
//创建sql语句对象
stam=conn.createStatement();
//创建sql语句
String sql="select id,pwd,loginName from user where id='"+j1.getText()+"' and pwd='"+j2.getText()+"' and loginName='"+j3.getText()+"'";
//执行sql语句
ResultSet rs=stam.executeQuery(sql);
if(rs.next()) {
this.dispose();
UserModi userTrue=new UserModi();
userTrue.setVisible(true);
userTrue.setSize(370,300);
userTrue.setLocation(300,300);
userTrue.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}else {
j1.setText("");
j2.setText("");
j3.setText("");
JOptionPane.showMessageDialog(this, "用户名或密码不存在");
}
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
FileWriter fileWriter=new FileWriter("nameExit.txt");
fileWriter.write(j1.getText());
fileWriter.flush();
fileWriter.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
用户修改密码代码
Package包位置
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
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;
public class UserModi extends JFrame implements ActionListener{
JPanel p1,p2,p3,p4,p5;
JLabel l1,l2,l3;
JTextField j1;
JPasswordField j2,j3;
JButton bu1,bu2;
Connection conn;
Statement stam;
public UserModi() {
super("用户修改");
this.setLayout(new GridLayout(4,1));
p1 =new JPanel(null);
l1 =new JLabel("用 户 名:");
l1.setFont(new Font("宋体",Font.BOLD,20));
l1.setBounds(50, 20, 120, 30);
j1=new JTextField();
//j1=new JLabel("12");
j1.setFont(new Font("宋体",Font.BOLD,20));
j1.setBounds(160, 20, 130, 30);
//j1.setEditable(false);
p1.add(l1);
p1.add(j1);
this.add(p1);
try {
BufferedReader bufferedReader=new BufferedReader(new FileReader("nameExit.txt"));
String lenString=bufferedReader.readLine();
if (lenString!=null) {
String[] aaStrings=lenString.split(" ");
j1.setText(aaStrings[0]);
}
// bufferedReader.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
p2 =new JPanel(null);
l2 =new JLabel("密 码:");
l2.setFont(new Font("宋体",Font.BOLD,20));
l2.setBounds(50, 20, 120, 30);
j2=new JPasswordField(10);
j2.setFont(new Font("宋体",Font.BOLD,20));
j2.setBounds(160, 20, 130, 30);
p2.add(l2);
p2.add(j2);
this.add(p2);
p3 =new JPanel(null);
l3 =new JLabel("确认密码:");
l3.setFont(new Font("宋体",Font.BOLD,20));
l3.setBounds(50, 20, 120, 30);
j3=new JPasswordField(10);
j3.setFont(new Font("宋体",Font.BOLD,20));
j3.setBounds(160, 20, 130, 30);
p3.add(l3);
p3.add(j3);
this.add(p3);
p4 =new JPanel(null);
bu1=new JButton("确认");
bu1.addActionListener(this);
bu1.setBounds(50, 20, 120,30);
p4.add(bu1);
bu2=new JButton("退出");
bu2.setBounds(170, 20, 120,30);
bu2.addActionListener(this);
p4.add(bu2);
this.add(p4);
}
public static void main(String[] args) {
UserModi userTrue=new UserModi();
userTrue.setVisible(true);
userTrue.setSize(370,300);
userTrue.setLocation(300,300);
userTrue.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==bu1) {
try {
//加载驱动
conn=JdbcRe.getConnection();
//创建sql语句对象
stam=conn.createStatement();
//创建sql语句
String sql="update user set pwd=('"+j2.getText()+"') , loginName=('"+j3.getText()+"') where id=('"+j1.getText()+"')";
//执行sql语句
stam.execute(sql);
JOptionPane.showMessageDialog(null, "修改成功!!!!!");
//关闭修改窗体
this.dispose();
//打开登录窗体
User user=new User();
user.setVisible(true);
user.setSize(600,500);
user.setLocation(600,200);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if (e.getSource()==bu2) {
j1.setText("");
this.dispose();
}
}
}
验证码判断
Package 包位置
/**
* 实现不区分大小写比较两字符串是否相等
* 输出为true false
* @author Lenovo
*
*/
public class ComStr {
public static boolean compar2Str(String str1,String str2) {
char[] ch1=str1.toCharArray();//将字符串str1转换为字符数组
char[] ch2=str2.toCharArray();//将字符串str2转换为字符数组
String newStr1="";//声明转换为小写的字符串str1
String newStr2="";//声明转换为小写的字符串str1
for (int i = 0; i < ch1.length; i++) {
if(ch1[i]>=65&&ch1[i]<=90) {//如果字符串为大写就将其转换为小写
ch1[i]+=32;
}
newStr1 +=ch1[i];
}
for (int i = 0; i < ch2.length; i++) {
if(ch2[i]>=65&&ch2[i]<=90) {
ch2[i]+=32;
}
newStr2 +=ch2[i];
}
//如果两相等将输出true
if(newStr2.equals(newStr1)) {
return true;
}
return false;
}
}
验证码随机
Package 包位置
/**
* 用于随机产生长度为4的可变字符串
*/
import java.util.Random;
public class RandomYzm {
public static String getYzm() {
//定义长度为4
int lenght=4;
//35个随机数
String untes="1,2,3,4,5,6,7,8,9,q,w,e,r,t,y,u,i,o,p,a,s,d,f,g,h,j,k,l,z,x,c,v,b,n,m";
//使用字符串将随机数分割开
String unte[]=untes.split(",");
//对随机数的对象进行实例化
Random rd=new Random();
//设置用于存储产生的字符串
StringBuffer buf=new StringBuffer();
for (int i = 0; i < lenght; i++) {
buf.append(unte[rd.nextInt(35)]);
}
return buf.toString();
}
}