基于Java JFrame的登录界面+Mysql(二:注册界面+Mysql 连接)

上次把登录界面重新弄完了,然后这里贴这个部分完整的实际图片:

然后如果自己有需求的也可以自行修改或者添加参数图片这些

登录界面                      

基于Java JFrame的登录界面+Mysql(二:注册界面+Mysql 连接)_第1张图片                             

点击注册后             

基于Java JFrame的登录界面+Mysql(二:注册界面+Mysql 连接)_第2张图片                                   

如果信息没有输入完全

基于Java JFrame的登录界面+Mysql(二:注册界面+Mysql 连接)_第3张图片

成功后跳转回原来的登录框:如果密码输入错误,否则就跳转到功能界面

基于Java JFrame的登录界面+Mysql(二:注册界面+Mysql 连接)_第4张图片

以及5.20写完的找回密码部分:

在填入完上面信息后点击找回即可出现对应密码;

如果密保问题回答失败会弹出警告窗口

基于Java JFrame的登录界面+Mysql(二:注册界面+Mysql 连接)_第5张图片

然后同时也对整体的类设计修改后把其相当于重新改写了一边代码如下所示:

package windows;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

/*
 * 初始登录界面
 */
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import mysql.Mysql;

public class LoginWindow extends JFrame implements ActionListener{
	//所有成员;
	boolean flag;
	private static final long serialVersionUID = 1L;
	JPanel mb;
	JLabel lb,lb1,lb2,lb3;
	JLabel namelb ,passwordlab;
	JButton bt,bt1,bt2;
	JTextField username;	
	JPasswordField password;
	JCheckBox select1,select2;
	public LoginWindow() {
		//初始化所有对象
		lb=new JLabel(new ImageIcon("C:\\interface.png"));
		//这里的图片自己找一张,大小大概 570x160像素图片
		mb=new JPanel();
		username =new JTextField(20);
		password=new JPasswordField(20);
		namelb = new JLabel("ID");
		passwordlab = new JLabel("Code");
	//	lb1=new JLabel("注册账号");
	//	lb2=new JLabel("找回密码");
		bt1 = new JButton("注册账号");
		bt2 = new JButton("找回密码");
		select1=new JCheckBox("记住密码");
		select2=new JCheckBox("自动登陆");
		bt=new JButton("登 录");
		setCheckBoxInfo();
		setButtonInfo();
		setLabInfo();
		setPanelInfo();
		addComponent();
		setFrame();
	}
	public void setCheckBoxInfo() {
		select1.setFont(new Font("楷体",Font.PLAIN,15));
		select1.setBackground(Color.WHITE);
		select2.setFont(new Font("楷体",Font.PLAIN,15));
		select2.setBackground(new Color(255,250,250));
		select1.setBounds(130,90,120,20);
		select2.setBounds(290,90,120,20);	
	}
	public void setButtonInfo() {
		bt.setBounds(130,130,250,37);
		bt.setFont(new Font("宋体",Font.PLAIN,16));
		bt.setBackground(new Color(0,178,238));
		bt.setForeground(Color.white);
		bt.addActionListener(this);
		bt1.setBounds(420,10,90,30);
		bt2.setBounds(420,50,90,30);
		bt1.setForeground(new Color(28,134,238));
		bt1.setFont(new Font("楷体",Font.PLAIN,13));
		bt1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		bt1.addActionListener(this);
		bt2.setForeground(new Color(28,134,238));
		bt2.setFont(new Font("楷体",Font.PLAIN,13));
		bt2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		bt2.addActionListener(this);
	}
	public void setFrame() {
		this.setSize(550, 400);
		this.setTitle("登录界面");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocation(400, 180);
		this.setResizable(false);
		this.setVisible(true);
	}
	public void setLabInfo() {
		username.setBounds(130, 14, 250, 37);
		username.setFont(new Font("宋体",Font.PLAIN,16));	
		password.setBounds(130,48, 250, 37);
		password.setFont(new Font("宋体",Font.PLAIN,16));	
		namelb.setBounds(50,14,40,37);
		passwordlab.setBounds(50, 48, 40, 37);
		//y,x,width,height; 130为左侧居中位置
	}
	public void setPanelInfo() {
		//将面板的布局设为null,然后自定义布局	
		mb.setLayout(null);
		mb.setBackground(Color.white);
	}
	public void addComponent() {
		//将组建添加到面板当中
		mb.add(bt2);mb.add(bt1);mb.add(select1);mb.add(select2);
		mb.add(bt);mb.add(username);mb.add(password);
		mb.setSize(540,190);mb.add(namelb);mb.add(passwordlab);
		this.add(lb,BorderLayout.NORTH);	
		this.add(mb,BorderLayout.CENTER);
	}
	public void findPassword() {
		this.dispose();
		FindPasswordWindow fw  = new FindPasswordWindow();
		fw.setVisible(true);
		
	}
	public void regist() {
		this.dispose();
		RegisterWindow rw  = new RegisterWindow();
		 rw.setVisible(true);
	}
	public void loginJudge() {
		Mysql mysql = new Mysql();
		mysql.connectSQL();
		String tname = username.getText();
		String tpass = new String (password.getPassword());
		try {
			this.flag = mysql.loginMatch(tname,tpass);
		} catch (SQLException e2) {
			
			e2.printStackTrace();
		} catch (Exception e2) {
			
			e2.printStackTrace();
		}
		//清空输入栏
		username.setText("");
		password.setText("");
		//如果用户名判断成功则关闭login窗口并打开Spider窗口,否则状态栏显示loadfail;
		if(flag) {
			this.dispose();
			 SpiderWindow sw  = new SpiderWindow();
			 sw.setVisible(true);
		}
		else {
			WarnWindow ww = new WarnWindow();
			ww.loginFail();
		}
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==bt)
		{
			this.loginJudge();
		}
		else if(e.getSource()==bt1) {
			this.regist();
		}
		else if(e.getSource()==bt2) {
			this.findPassword();
		}
	}
}

然后现在的注册界面:

package windows;

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
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 javax.swing.filechooser.FileNameExtensionFilter;

import mysql.Mysql;
import user.User;

public class RegisterWindow extends JFrame implements ActionListener{
	JPanel mb;
	JButton bt,bt1;
	JComboBox cb = null;
	Vector v ;
	JLabel namelb,studyNumberlb,classroomlb,answerlb,securityQuestionlb;
	JLabel passwordlb,passwordlb2;
	JTextField name,studyNumber,classroom,answer;	
	JPasswordField password,rePassword;
	public RegisterWindow(){
		password = new JPasswordField(20);
		rePassword = new JPasswordField(20);
		name = new JTextField(20);
		studyNumber = new JTextField(20);
		classroom = new JTextField(20);
		answer = new JTextField(20);
		classroomlb = new JLabel("班级");
		namelb = new JLabel("姓名");
		passwordlb = new JLabel("密码");
		passwordlb2 = new JLabel("重复输入密码");
		studyNumberlb = new JLabel("学号");
		answerlb = new JLabel("答案");
		securityQuestionlb = new JLabel("密保问题");
		setComboBox();
		mb=new JPanel();
		bt=new JButton("注 册");
		bt1=new JButton("清 空");
		setLocation();
		setPanelInfo();
		setFrame();
		setButton();
		setTextfieldInfo();
	}
	public void setComboBox() {
		v = new Vector ();
		cb = new JComboBox(v);
		v.add("你的辅导员姓名是");
		v.add("你的最好的朋友是");
		v.add("你最喜欢的女孩子名字是");
		cb.setBorder(BorderFactory.createTitledBorder("选择你的密保问题"));
		cb.setMaximumRowCount(3);
	}
	public void setButton() {
		bt.setFont(new Font("宋体",Font.PLAIN,15));
		//bt.setBackground(new Color(0,178,238));
		bt.setForeground(Color.black);
		bt1.setFont(new Font("宋体",Font.PLAIN,15));
		//bt1.setBackground(new Color(0,178,238));
		bt1.setForeground(Color.black);
		bt.addActionListener(this);
		bt1.addActionListener(this);
	}
	public void setTextfieldInfo() {
		name.setFont(new Font("宋体",Font.PLAIN,16));
		answer.setFont(new Font("宋体",Font.PLAIN,16));
		studyNumber.setFont(new Font("宋体",Font.PLAIN,16));
		classroom.setFont(new Font("宋体",Font.PLAIN,16));
		password.setFont(new Font("宋体",Font.PLAIN,16));
		rePassword.setFont(new Font("宋体",Font.PLAIN,16));
		name.setBounds(130, 14, 250, 37);
		studyNumber.setBounds(130,48, 250, 37);
		classroom.setBounds(130,180, 250, 37);
		answer.setBounds(130,268, 250, 37);
		password.setBounds(130,92, 250, 37);
		rePassword.setBounds(130,136, 250, 37);
	}
	public void setFrame() {
		this.setLocation(400, 180);
		this.setSize(700, 450);
		this.setVisible(true);
	}
	public void setLocation() {
		namelb.setBounds(50,14,40,37);
		studyNumberlb.setBounds(50,48,40,37);
		passwordlb.setBounds(50, 92, 40, 37);
		passwordlb2.setBounds(50, 136, 40,37);
		classroomlb.setBounds(50,180,40,37);
		securityQuestionlb.setBounds(50,224,40,37);
		answerlb.setBounds(50,268,40,37);
		name.setBounds(160, 14, 250, 37);
		studyNumber.setBounds(160,48, 250, 37);
		classroom.setBounds(160,180, 250, 37);
		answer.setBounds(160,268, 250, 37);
		password.setBounds(160,92, 250, 37);
		rePassword.setBounds(160,136, 250, 37);
		bt.setBounds(160,350,100,37);
		bt1.setBounds(270,350,100,37);
		cb.setBounds(130,224,250,45);
	}
	public void setPanelInfo() {
		//将面板的布局设为null,然后自定义布局	
		mb.setLayout(null);
		mb.setBackground(Color.white);
		mb.add(namelb); mb.add(answerlb); mb.add(classroomlb);
		mb.add(passwordlb);mb.add(passwordlb2);mb.add(studyNumberlb);
		mb.add(securityQuestionlb);mb.add(studyNumber);
		mb.add(answer); mb.add(classroom); mb.add(name);
		mb.add(password); mb.add(rePassword); mb.add(bt); mb.add(bt1);
		mb.add(cb);
		this.add(mb);
	}
	public void clearTextfield() {
		password.setText("");
		rePassword.setText("");
		name.setText("");
		studyNumber.setText("");
		classroom.setText("");
		answer.setText("");
		password.setText("");
	}

	public boolean checkLegal(String tpass, String repass) {
		if(tpass.equals(repass)) return true;
		else return false;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==bt)
		{
			String tpass = new String (password.getPassword());
			String repass = new String (rePassword.getPassword());
			if (this.checkLegal(tpass, repass)) {
				//判断注册信息是否输入完整
				if(classroom.getText().length()==0||name.getText().length()==0||answer.getText().length()==0) {
					WarnWindow ww = new WarnWindow();
					ww.registIncomplete();
				}
				//完整的情况
				else {
					User user = new User(); 
					user.setName(name.getText());
					String str = new String(password.getPassword());
					user.setPassword(str);
					user.setId(studyNumber.getText());
					user.setClassroom(classroom.getText());
				user.setSecurityQuestion((String)cb.getSelectedItem());
					user.setSecurityAnswer(answer.getText());
					Mysql mq = new Mysql();
					mq.connectSQL();
					try {
						if(mq.register(user)) {
						LoginWindow lw =new LoginWindow();
							this.dispose();
						}else {
							WarnWindow ww = new WarnWindow();
							ww.sqlConnectFail();
						}
					}catch (SQLException e1){e1.printStackTrace();}	
				}
			}else{
					WarnWindow ww = new WarnWindow();
					ww.reputCheckWarning();
					clearTextfield();
				}
		}
		else if(e.getSource()==bt1) {
			clearTextfield();
		}
	}
}

以及暂时具有登录查询和注册输入的Mysql类:

//5.25将找回密码部分更新

package mysql;
//傻瓜式连接数据库;
/*| studyNumberId    | varchar(30)  | YES  |     | NULL    |       |
| name             | varchar(30)  | NO   |     | NULL    |       |
| password         | varchar(32)  | NO   |     | NULL    |       |
| class            | varchar(20)  | YES  |     | NULL    |       |
| securityAnswer   | varchar(30)  | NO   |     | NULL    |       |
| securityQuestion | varchar(100) | YES  |     | NULL    |       |
+------------------+--------------+------+-----+---------+-------+*/
/*
 * 类的结构
 * public Mysql();
 * 构造:	无参构造:(并调用ConnectSql();)
 * 方法:
 * public void connectSql();连接数据库
 * public boolean loginMatch();	//由login调用:匹配login传来的text参数,
 * 并将其与数据库对象匹配;
 * public void register();由注册窗口RegisterWindow的register方法调用,
 * 调用person()并进行注册; 将信息写入数据库
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import user.User;
import windows.WarnWindow;
public class Mysql {
	public static final String DBDRIVER = "com.mysql.cj.jdbc.Driver";
	public static final String DBURL = "jdbc:mysql://localhost:3306
/database_01?serverTimezone=UTC&characterEncoding=utf-8  ";
//将上面database_01替换为自己所创建的数据库
	public static final String DBUSER ="root";
//连接数据库的id
	public static final String DBPW = "123456";
//连接数据库的密码
	private static String tname;
	private static String tpass;
	Statement stmt = null;
	String pass = null,pass1 = null ;
	Connection connection = null;
	public Mysql() {	
	}
	public boolean register(User user) throws SQLException {
		stmt = connection.createStatement();
		String sql = "insert  into  user "
				+"values('"+user.getId()+"','"+user.getName()+"','"+user.getPassword()+"','"
				+user.getClassroom()+"','"+user.getSecurityAnswer()+"','"+user.getSecurityQuestion()+"')";
		
		if(stmt.execute(sql) ) return false;
		else return true;
	}
	public void connectSQL() {
		try {
			Class.forName(DBDRIVER);
		}catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			connection = DriverManager.getConnection(DBURL,DBUSER,DBPW);
		} catch(SQLException e) {
			e.printStackTrace();
		}
		System.out.println(connection);
	}
	public String findPassword(User user) throws SQLException {
		Mysql.tname = user.getId();
		Mysql.tpass = user.getSecurityAnswer();
		stmt = connection.createStatement();
		String sql2 = "SELECT securityAnswer FROM user WHERE studyNumberId ='"+Mysql.tname+"'";
		ResultSet rs = stmt.executeQuery(sql2);
		try {
			while(rs.next()){ 
			 pass = rs.getString("securityAnswer");
			}
		}catch(SQLException ex) {//对搜索数据失败的异常处理
			WarnWindow ww = new WarnWindow();
			ww.registIncomplete();
		}
		 pass1 = Mysql.tpass ;
		 if(pass1.equals(pass)) {
			 String sql = "SELECT password FROM user WHERE studyNumberId ='"+Mysql.tname+"'";
			 ResultSet rs2 = stmt.executeQuery(sql);
			 while(rs2.next()){ 
				 pass = rs2.getString("password");
			 }
			 return pass;
		 }
		 else return pass;
		
	}
	public boolean loginMatch(String tname,String tpass)throws SQLException,Exception {
		//开始查询
		Mysql.tname = tname;
		Mysql.tpass = tpass;
		stmt = connection.createStatement();
		String sql = "SELECT password FROM user WHERE studyNumberId ='"+Mysql.tname+"'";
		ResultSet rs = stmt.executeQuery(sql);
		 while(rs.next()){ 
			 pass = rs.getString("password");
		 }
		 pass1 = Mysql.tpass ;
		 if(pass1.equals(pass)) return true;
		 else return false;
	}
	
	public void closeConnection() {
		  try { 
			  connection.close(); } 
		  catch (SQLException e) { e.printStackTrace(); }
		 
	}
}

以及后面写的WarnWindow类作为警告弹出:

package windows;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class WarnWindow {

	JFrame temp ;
	//临时的警告弹出窗口
	public WarnWindow() {
		temp = new JFrame();
		temp.setVisible(true);
		temp.setLocation(500, 250);
		temp.setSize(100, 200);
	}
	public void spiderSuccessful() {
		JLabel templb = new JLabel("爬取成功");
		temp.add(templb);
	}
	public void reputCheckWarning() {
		JLabel templb = new JLabel("两次输入的密码不一样!");
		temp.add(templb);
	}
	public void findFail() {
		JLabel templb = new JLabel("答案有误");
		temp.add(templb);
	}
	public void registerSuccessful() {
		JLabel templb = new JLabel("注册成功");
		temp.add(templb);
	}
	public void loginFail() {
		JLabel templb = new JLabel("密码输入错误");
		temp.add(templb);
	}
	public void sqlConnectFail() {
		JLabel templb = new JLabel("数据库连接错误");
		temp.add(templb);
	}
	public void registIncomplete() {
		JLabel templb = new JLabel("注册信息输入不完整或者有误");
		temp.add(templb);
	}
	
}

5.25更新的找回密码窗口

package windows;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import mysql.Mysql;
import user.User;

public class FindPasswordWindow extends JFrame implements ActionListener{
	JPanel mb;
	JButton bt,bt1;
	JComboBox cb = null;
	Vector v ;
	JLabel namelb,studyNumberlb,classroomlb,answerlb,securityQuestionlb;
	JLabel passwordlb;

	JTextField name,studyNumber,classroom,answer;	

	public FindPasswordWindow(){

		name = new JTextField(20);
		studyNumber = new JTextField(20);
		classroom = new JTextField(20);
		answer = new JTextField(20);
		classroomlb = new JLabel("班级");
		namelb = new JLabel("姓名");
		studyNumberlb = new JLabel("学号");
		answerlb = new JLabel("答案");
		securityQuestionlb = new JLabel("密保问题");
		setComboBox();
		mb=new JPanel();
		bt=new JButton("找 回");
		bt1=new JButton("退 出");
		setLocation();
		setPanelInfo();
		setFrame();
		setButton();
		setTextfieldInfo();
	}
	public void setComboBox() {
		v = new Vector ();
		cb = new JComboBox(v);
		v.add("你的辅导员姓名是");
		v.add("你的最好的朋友是");
		v.add("你最喜欢的女孩子名字是");
		cb.setBorder(BorderFactory.createTitledBorder("选择你的密保问题"));
		cb.setMaximumRowCount(3);
	}
	public void setButton() {
		bt.setFont(new Font("宋体",Font.PLAIN,15));
		//bt.setBackground(new Color(0,178,238));
		bt.setForeground(Color.black);
		bt1.setFont(new Font("宋体",Font.PLAIN,15));
		//bt1.setBackground(new Color(0,178,238));
		bt1.setForeground(Color.black);
		bt.addActionListener(this);
		bt1.addActionListener(this);
	}
	public void setTextfieldInfo() {
		name.setFont(new Font("宋体",Font.PLAIN,16));
		answer.setFont(new Font("宋体",Font.PLAIN,16));
		studyNumber.setFont(new Font("宋体",Font.PLAIN,16));
		classroom.setFont(new Font("宋体",Font.PLAIN,16));
		//rePassword.setFont(new Font("宋体",Font.PLAIN,16));
	}
	public void setFrame() {
		this.setLocation(400, 180);
		this.setSize(700, 450);
		this.setVisible(true);
	}
	public void setLocation() {
		namelb.setBounds(50,14,40,37);
		studyNumberlb.setBounds(50,48,40,37);
		//passwordlb.setBounds(50,268,40,37);
		//passwordlb2.setBounds(50,224,40,37);
		classroomlb.setBounds(50, 92, 40, 37);
		securityQuestionlb.setBounds(50, 136, 40,37);
		cb.setBounds(160,136,250,45);
		answerlb.setBounds(50,180,40,37);
		name.setBounds(160, 14, 250, 37);
		studyNumber.setBounds(160,48, 250, 37);
		classroom.setBounds(160,92, 250, 37);
		answer.setBounds(160,180,250,37);
	//	password.setBounds(160,180, 250, 37);
	//	rePassword.setBounds(160,136, 250, 37); 
		bt.setBounds(160,350,100,37);
		bt1.setBounds(270,350,100,37);
		
	}
	public void setPanelInfo() {
		//将面板的布局设为null,然后自定义布局	
		mb.setLayout(null);
		mb.setBackground(Color.white);
		mb.add(namelb); mb.add(answerlb); mb.add(classroomlb);
	
		mb.add(studyNumberlb);
		mb.add(securityQuestionlb);mb.add(studyNumber);
		mb.add(answer); mb.add(classroom); mb.add(name);
		
		mb.add(bt); mb.add(bt1);
		mb.add(cb);
		this.add(mb);
	}
	public void clearTextfield() {

		name.setText("");
		studyNumber.setText("");
		classroom.setText("");
		answer.setText("");
	}

	public boolean checkLegal(String tpass, String repass) {
		if(tpass.equals(repass)) return true;
		else return false;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==bt)
		{
				//判断注册信息是否输入完整
				if(classroom.getText().length()==0||name.getText().length()==0) {
					WarnWindow ww = new WarnWindow();
					ww.registIncomplete();
				}
				//完整的情况
				else {
					User user = new User(); 
					user.setName(name.getText());
					user.setId(studyNumber.getText());
					user.setClassroom(classroom.getText());
					user.setSecurityQuestion((String) cb.getSelectedItem());
					user.setSecurityAnswer(answer.getText());
					Mysql mq = new Mysql();
					mq.connectSQL();
					try {
						String pass = mq.findPassword(user);
						if(pass != null) {
							passwordlb = new JLabel("您的密码是"+pass);
							mb.add(passwordlb);
							passwordlb.setBounds(160,268, 250, 37);
							passwordlb.setFont(new Font("宋体",Font.PLAIN,16));
						}else {
							WarnWindow ww = new WarnWindow();
							ww.findFail();
						}
					}catch (SQLException e1) {e1.printStackTrace();}	
				}
			}
		else if(e.getSource()==bt1) {
			this.dispose();
		}
	}
}

 

完整代码已上传至百度云

链接  提取码: b2jk

你可能感兴趣的:(登录界面+爬虫项目)