Java实现注册登录系统——基于Java Swing实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014543872/article/details/83999337

技术简介:Java Swing 介绍

Swing 是一个为Java设计的GUI工具包。

Swing是JAVA基础类的一部分。

Swing包括了图形用户界面(GUI)器件如:文本框,按钮,分隔窗格和表。

Swing提供许多比AWT更好的屏幕显示元素。它们用纯Java写成,所以同Java本身一样可以跨平台运行,这一点不像AWT。它们是JFC的一部分。它们支持可更换的面板和主题(各种操作系统默认的特有主题),然而不是真的使用原生平台提供的设备,而是仅仅在表面上模仿它们。这意味着你可以在任意平台上使用JAVA支持的任意面板。轻量级组件的缺点则是执行速度较慢,优点就是可以在所有平台上采用统一的行为。

  以上简介是引用的菜鸟教程里面的介绍,这个教程还是很不错的,比较简洁明了;点此这里查看菜鸟教程

首先呢,我们既然要实现一个用户注册登录系统基于-Java Swing,就避免不了和数据库打交道,在这里我们选择MySQL数据库,采用的数据库驱动jar是:mysql-connector-java-5.1.22-bin.jar

1.第一步我们需要建立我们的数据库表,因为只涉及到用户的注册登录行为,我们的表结构很简单,只有用户名(username)和用户密码(password),我们的数据库名为mysql,表名为jdbc_dome 表结构如下图所示:  

2.第二步我们来创建创建Java project 命名随你意;在这里我起的贪吃鬼(这个案例是我之前做的贪吃蛇小游戏的注册登录哪来的-)

项目目录如下图所示:

3.将下来我们就直接贴代码啦,代码里面该有的注释我都写上了,总共就三个包:一个包中是连接数据库的类(里面封装好了数据库连接以及释放资源的静态方法,直接在用的时候用类名调用就可以了)另一个是注册登录的类,还有一个图片资源包(里面放了一下我们所需要的图片)

扫描二维码关注公众号,回复: 4176025 查看本文章

  1> 创建包com.mooc.login 类Login.java和Register.java

   Login.java类   //用户登录窗体

package com.mooc.login;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;

import javax.swing.Icon;
import javax.swing.ImageIcon;
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 javax.swing.border.EmptyBorder;

import com.mooc.jdbcUtil.jdbcUtil;
import com.mysql.jdbc.Statement;
/**
 * 用户登录
 * @author 大南海
 *
 */
public class Login extends JFrame{
     
	private JPanel contentPane;
	private JButton btn1,btn2,btn3;
	private JTextField userName;
	private JPasswordField password;
	private JLabel label1,label2;
	
	private int LOGIN_WIDTH=360;
	private int LOGIN_HEIGTH=350;
	
	
	Connection conn;
	Statement stam;
	
	public Login() {
		
	     setTitle("贪吃鬼");  //设置窗体标题
		 setBounds(100, 50, LOGIN_WIDTH, LOGIN_HEIGTH	);  //设置窗体坐标以及打下
		 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //设置窗体可关闭
		 setResizable(false);  //设置窗体大小不可以改变
		 setVisible(true);    //设置窗体可见
		 //设置窗体标题图标
		 setIconImage(
			Toolkit.getDefaultToolkit().getImage(Login.class.getResource("/images/log.jpg"))	 	 
				 );
		 /**
		  * 添加一个面板容器到窗体中
		  */
		 contentPane = new JPanel();
         contentPane.setBackground(Color.WHITE);
		 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
	   	 setContentPane(contentPane);
		 contentPane.setLayout(null);	 
		 //账号标签
		 label1=new JLabel("");
		 label1.setBounds(80,76, 54, 28);
		 label1.setIcon(new ImageIcon(Login.class.getResource("/images/user.png")));
		 contentPane.add(label1);
		 
		 
		 //密码标签
		 label2=new JLabel("");
		 label2.setBounds(80, 135, 54, 28);
		 label2.setIcon(new ImageIcon(Login.class.getResource("/images/psw.png")));
		 contentPane.add(label2);
	
         //账号输入框
		 userName=new JTextField();
		 userName.setBounds(139, 80, 161, 25);
		 contentPane.add(userName);
		 
		 //密码输入框
		 password=new JPasswordField();
		 password.setBounds(139, 140, 161, 25);
		 
	     contentPane.add(password);
	     
	     
	     //按钮—登录
	     btn1=new JButton("登   录");
	     btn1.setBounds(95, 210, 80, 23);
	     btn1.setIcon(new ImageIcon(Login.class.getResource("/images/btn1.png")));
	     btn1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource()==btn1) {	 
					try {
						conn=jdbcUtil.getConnection();//获取数据库连接
						stam= (Statement) conn.createStatement();  //创建sql语句执行对象
						//编写sql语句
						String sql="select * from user where username='"+userName.getText()+"'  and password='"+password.getText()+"'     ";
					    //执行sql语句
						ResultSet rs=stam.executeQuery(sql);
						if(rs.next()) {
					    dispose();//关闭当前窗口							
						new Main();
						}		
					}catch (Exception e0) {
						e0.printStackTrace();
					}finally {
						jdbcUtil.result(conn, stam);
						
					}
				}
			}
		});
	     contentPane.add(btn1);
	     //按钮—退出
	     btn2=new JButton("退  出");
	     btn2.setBounds(210, 210, 80, 23);
	     btn2.setIcon( new ImageIcon(Login.class.getResource("/images/exit.png")));
	     btn2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource()==btn2) {
				    dispose();
				}
			}
		});
	     
	     
	     contentPane.add(btn2);     
	     //按钮-注册
	     btn3=new JButton("注        册");
	     btn3.setBounds(95,240,200, 23);
	     btn3.setIcon(new ImageIcon(Login.class.getResource("/images/regier.png")));
	     btn3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {

				 dispose();//关闭登录窗体
				new Register().addMan(); // 打开注册窗体
				
			}
		});
	     contentPane.add(btn3);
	     
	}	
		
	public static void main(String[] args) {
		new Login();
	}	
		
	
}

  Register.java类   //用户注册窗体

package com.mooc.login;

import java.awt.Color;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

import javax.swing.ImageIcon;
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 javax.swing.border.EmptyBorder;

import com.mooc.jdbcUtil.jdbcUtil;
/**
 * 用户注册
 * @author 大南海
 *
 */

public class Register  extends JFrame {
  
	  private int LOGIN_WIDTH=360;
	  private int LOGIN_HEIGTH=350;
	  private JPanel contentPane;
	  private JTextField userName;
	  private JPasswordField password;
	  private JButton btn3,btn4;
	  private JLabel label3,label4;
	  Connection conn;
	  Statement stam;
	  
	  
       public void addMan() {
		
    	   setTitle("注册");
    	   setTitle("增删改查");
  		   setBounds(100, 50, LOGIN_WIDTH, LOGIN_HEIGTH	);
  		   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  		   setResizable(false);
  		   setVisible(true);
  		   //设置窗体标题图标
  		   setIconImage(
  			Toolkit.getDefaultToolkit().getImage(Login.class.getResource("/images/log.jpg"))	 	 
  				 );
  		   /**
  		   * 添加一个面板容器到窗体中
  		   */
  		   contentPane = new JPanel();
           contentPane.setBackground(Color.WHITE);
  		   contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
  	   	   setContentPane(contentPane);
  		   contentPane.setLayout(null);
  		   
  		//账号标签
  		 label3=new JLabel("");
  		 label3.setBounds(80,76, 54, 28);
  		 label3.setIcon(new ImageIcon(Login.class.getResource("/images/user.png")));
  		 contentPane.add(label3);
  		 
  		 
  		 //密码标签
  		 label4=new JLabel("");
  		 label4.setBounds(80, 135, 54, 28);
  		 label4.setIcon(new ImageIcon(Login.class.getResource("/images/psw.png")));
  		 contentPane.add(label4);
  	
           //账号输入框
  		 userName=new JTextField();
  		 userName.setBounds(139, 80, 161, 25);
  		 contentPane.add(userName);
  		 
  		 //密码输入框
  		 password=new JPasswordField();
  		 password.setBounds(139, 140, 161, 25);
  		 
  	     contentPane.add(password);
  		   
  	     btn3=new JButton("登   录");
	     btn3.setBounds(95, 210, 80, 23);
	     btn3.setIcon(new ImageIcon(Login.class.getResource("/images/insist.png")));
  		 btn3.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				if(e.getSource()==btn3) {
					try {
						
						//加载数据库驱动
						conn=jdbcUtil.getConnection();
						//创建执行sql语句的对象
						stam=conn.createStatement();
						//编写sql语句
						String sql="insert into user values('"+userName.getText()+"','"+password.getText()+"')";
						//执行sql语句
						stam.execute(sql);
						JOptionPane.showMessageDialog(null, "注册成功!");
						dispose();  //关闭注册窗体
					    new Login();  //打开登录窗体
						
						
					}catch (Exception e1) {
						e1.printStackTrace();
					}finally {
						jdbcUtil.result(conn, stam);
					}
					
				}
				
			}
		});
	     
	     contentPane.add(btn3);
  		  
  		 btn4=new JButton("退  出");
	     btn4.setBounds(210, 210, 80, 23);
	     btn4.setIcon( new ImageIcon(Login.class.getResource("/images/exit.png")));
	     btn4.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
			
				if(e.getSource()==btn4) {
				  
				    dispose();
				}
				
				
				
			}
		});
	     
	     
	     contentPane.add(btn4);
	     
	}
	
	
}

另外你可以创建一个登录成功后的窗体:比如Mian.java 

package com.mooc.login;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.LinkedList;
import java.util.WeakHashMap;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JOptionPane;

public class Main extends JFrame {

	private static final int GAME_WIDTH = 1100;
	private static final int GAME_HEIGTH = 600;
	

	/**
	 * 构造方法
	 */
	public Main() {
		setTitle("主界面");
		setSize(GAME_WIDTH, GAME_HEIGTH);
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		setBackground(Color.darkGray);
		setLocationRelativeTo(null);// 居中显示
		
	
		
	}


}

   2>创建包com.mooc.jdbcUtil  类jdbcUtil,java

    类jdbcUtil,java   //数据库封装操作类

package com.mooc.jdbcUtil;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
 *      数据库连接类
 * @author 大南海
 *
 */
public class jdbcUtil {

	private static final String dricerClass;
	private static final String url;
	private static final String username;
	private static final String password;

	static {
		dricerClass = "com.mysql.jdbc.Driver";
		url = "jdbc:mysql:///jdbc_dome";
		username = "root";
		password = "root";
	}

	/*
	 * 加载数据库的方法
	 */

	public static void locadClass() throws ClassNotFoundException {

		Class.forName(dricerClass);
	}

	/*
	 * 获取数据库连接的方法
	 */
	public static Connection getConnection() throws Exception {

		Connection conn = DriverManager.getConnection(url, username, password);

		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;
		}
	}

	/*
	 * 关闭连接并释放资源的方法
	 */
	public static void result(Connection conn, Statement stam, ResultSet rs) {

		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;
		}
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			rs = null;
		}

	}

}

运行截图:

  

截止现在 我们的这个简单小例子-注册登录就算完成了,以上内容功能是不缺少了,可能会缺少图片资源。你也可以自己找一下;你也可以选择下使用我之前上传的这个资源案例,是个小游戏(贪吃蛇),其中也包含这个注册登录;点击这里可以下载:

https://download.csdn.net/download/u014543872/10780768​​​​​​​

谢谢你的浏览,以上内容如有错误欢迎在评论区指出或者私信我;

猜你喜欢

转载自blog.csdn.net/u014543872/article/details/83999337