概述
基于Swing编程的用户登录、注册界面(数据库连接池)
登录注册页面都是使用了嵌套布局
1、新建用户登录界面类:myLoginGUI
public class myLoginGUI extends JFrame (继承JFrame类)
主要代码如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class myLoginGUI extends JFrame {
/*给窗口添加属性如下:*/
JButton Login = new JButton("用户登录");
JButton Cancel = new JButton("重置输入");
JButton Register = new JButton("注册用户");
JLabel usernameLable = new JLabel("用户名:");
JLabel passwordLable = new JLabel("密 码:");
JPasswordField password = new JPasswordField(10);
JTextField username = new JTextField(10);
Image titlebj = Toolkit.getDefaultToolkit().getImage("src/com/weida/practise/day0706/images/titlebj.png");
/*构造方法如下:*/
public myLoginGUI(){
setTitle("官方登录界面");
setIconImage(titlebj);
//设置流式布局 1或FlowLayout.CENTER ,居中对齐
//第一个面板:用户数据填写
JPanel Userdata = new JPanel();
Userdata.setLayout(new FlowLayout(FlowLayout.LEFT));
Userdata.add(usernameLable);
Userdata.add(username);
Userdata.add(passwordLable);
Userdata.add(password);
add(Userdata); //添加第一个面板到界面
//第二个面板:按钮组件
JPanel BuJp = new JPanel();
BuJp.setLayout(new GridLayout(3,1,5,5));
BuJp.add(Login);
BuJp.add(Cancel);
BuJp.add(Register); //用户注册
add(BuJp); //添加第二个面板到界面
setLayout(new FlowLayout());
setSize(400,250);
setResizable(false); //设置不可拉伸大小
setLocationRelativeTo(null);//窗体居中显示
setVisible(true); //打开显示窗口
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗体退出时操作
//为登录按钮添加监听事件
Login.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (username.getText().trim().length() == 0 || new String(password.getPassword()).trim().length() == 0) {
JOptionPane.showMessageDialog(null, "用户名密码不允许为空");
return;
}
try{
String myusername = username.getText().trim();
String mypassword = new String(password.getPassword()).trim();
Userdao userdao = new Userdao();
User user = userdao.Login(myusername, mypassword);
if(user!=null) {
System.out.println(user);
JOptionPane.showMessageDialog(null, "登录成功");
}
else JOptionPane.showMessageDialog(null, "用户名或者密码错误");
}catch (Exception e){
e.printStackTrace();
}
}
});
//为重新输入按钮添加监听事件
Cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
username.setText("");
password.setText("");
}
});
//为用户注册按钮添加监听事件
Register.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
myResiterGui resiterGui = new myResiterGui();//注册窗口
resiterGui.setVisible(true);
setVisible(false); //关闭显示窗口
}
});
}
/*在main方法中新建登录窗口对象*/
public static void main(String[] args) {
new myLoginGUI();
}
}
运行界面如下:
2、新建用户注册登录界面,主要代码如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class myResiterGui extends JFrame {
/*注册界面属性*/
JLabel usernameLable = new JLabel("新用户名:");
JLabel passwordLable = new JLabel("用户密码:");
JLabel passwordAaginLable = new JLabel("再次输入:");
JLabel genderLable = new JLabel("您的性别:");
JLabel hobbyLable = new JLabel("您的爱好:");
JLabel nianjiLable = new JLabel("您的年级:");
JLabel phoneLable = new JLabel("您的电话:");
JLabel addressLable = new JLabel("您的地址:");
//单选按钮,默认选中男
JRadioButton man = new JRadioButton("男 ", true);
JRadioButton woman = new JRadioButton("女 ");
//爱好复选框:
JCheckBox hobby1 = new JCheckBox("唱 ");
JCheckBox hobby2 = new JCheckBox("跳 ");
JCheckBox hobby3 = new JCheckBox("RAP ");
JComboBox shownianji = new JComboBox(); //爱好多选框
//输入框
JTextField username = new JTextField(10);
JPasswordField password = new JPasswordField(10);
JPasswordField passwordAagin = new JPasswordField(10);
JTextField phone = new JTextField(10);
JTextField address = new JTextField(10);
//按钮
JButton returnLogin = new JButton("返回登录");
JButton registerOK = new JButton("确认注册");
JButton cancel = new JButton("重新填入");
public myResiterGui() {
setFont(new Font("宋体", Font.BOLD, 16));
/*添加属性、设置窗体布局*/
setTitle("官方注册界面");
setDefaultCloseOperation(EXIT_ON_CLOSE);//设置关闭退出功能
//
//第一个面板 :用户名和密码输入
JPanel UserdataJP = new JPanel();
UserdataJP.setLayout(new GridLayout(3,2,5,5));
UserdataJP.add(usernameLable);
UserdataJP.add(username);
UserdataJP.add(passwordLable);
UserdataJP.add(password);
UserdataJP.add(passwordAaginLable);
UserdataJP.add(passwordAagin);
add(UserdataJP); //添加第一个面板
//
//第二个面板 :性别
JPanel genderjp = new JPanel(new FlowLayout());
genderjp.add(genderLable);
ButtonGroup gender = new ButtonGroup();//单选按钮的组,约束组内单选只能选择一个
gender.add(man);
gender.add(woman);
genderjp.add(man);
genderjp.add(woman);
add(genderjp); //添加第二个面板
//第三个面板 :爱好
JPanel hobbyjp = new JPanel(new FlowLayout());
hobbyjp.add(hobbyLable);
hobbyjp.add(hobby1);
hobbyjp.add(hobby2);
hobbyjp.add(hobby3);
add(hobbyjp); //添加第三个面板
//第四个面板 :年级
JPanel nianjijp = new JPanel(new FlowLayout());
nianjijp.add(nianjiLable);
shownianji.addItem("大一");
shownianji.addItem("大二");
shownianji.addItem("大三");
shownianji.addItem("大四");
nianjijp.add(shownianji);
add(nianjijp); //添加第四个面板:年级
//第五个面板 :联系手机与地址
JPanel lianxijb = new JPanel();
lianxijb.setLayout(new GridLayout(2,2,5,5));
lianxijb.add(phoneLable);
lianxijb.add(phone);
lianxijb.add(addressLable);
lianxijb.add(address);
add(lianxijb); //添加第五个面板 :联系手机与地址
//第六个面板 按钮组件
JPanel Bujp = new JPanel();
Bujp.setLayout(new FlowLayout(FlowLayout.CENTER));
Bujp.add(registerOK); //确认注册
Bujp.add(cancel); //重新输入按钮
Bujp.add(returnLogin); //返回登录
add(Bujp); //添加第六个面板 按钮组件
setSize(312, 400);
//嵌套布局:布局里包含布局,借助面板JPanel,一般一种布局解决不了问题或者组件比较多的时候
setLayout(new FlowLayout());
setResizable(false); //设置不可拉伸大小
setVisible(true);
setLocationRelativeTo(null);//窗体居中显示
/*返回登录界面按钮*/
returnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
myLoginGUI mylogingui = new myLoginGUI();//注册窗口
mylogingui.setVisible(true);
setVisible(false); //关闭显示窗口
}
});
/*重新输入按钮*/
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
password.setText("");
username.setText("");
passwordAagin.setText("");
phone.setText("");
address.setText("");
}
});
/*确认注册按钮*/
registerOK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
/*判断用户名、密码是否为空*/
if(username.getText().length() != 0 && password.getPassword().length != 0 &&
passwordAagin.getPassword().length != 0){
/*用空格来演示特殊字符*/
if(username.getText().contains(" ")){
/*重载提示框*/
JOptionPane.showMessageDialog(null,"注册用户名不能包含特殊字符"
,"错误的用户信息", JOptionPane.WARNING_MESSAGE);
return;
}
/*密码有特殊字符*/
if(password.getPassword().toString().contains(" ")|| passwordAagin.getPassword().toString().contains(" ")){
JOptionPane.showMessageDialog(null,"注册密码不能包含特殊字符"
,"错误的用户信息", JOptionPane.WARNING_MESSAGE);
return;
}
/*密码不一致*/
if (!(new String(password.getPassword()).trim().equals(new String(passwordAagin.getPassword()).trim()))){
JOptionPane.showMessageDialog(null,"两次输入密码不一致"
,"错误的用户信息", JOptionPane.WARNING_MESSAGE);
return;
}
/*输入正确*/
User user = new User();
//用户名
user.setUsername(username.getText().toString());
//密码
user.setPassword(new String(password.getPassword()));
//手机号码
user.setPhone(phone.getText().toString());
//地址
user.setAddress(address.getText().toString());
//判断用户性别:
if(man.isSelected()) user.setGender("男");
else user.setGender("女");
//判断用户爱好:
StringBuffer hobbystr = new StringBuffer();
if(hobby1.isSelected()) hobbystr.append("唱,");
if(hobby2.isSelected())hobbystr.append("跳,");
if(hobby3.isSelected()) hobbystr.append("RAP,");
String hobbystr1 = hobbystr.toString();
user.setHobby(hobbystr1);
//判断用户年级:
user.setNianji((String)shownianji.getSelectedItem());
System.out.println(user);
try{
//添加用户
com.weida.practise.day0709.Dao.Userdao userdao = new Userdao();
if (userdao.addUser(user))
JOptionPane.showMessageDialog(null,"添加成功!");
else JOptionPane.showMessageDialog(null,"添加发生未知错误!(可能已经存在该用户)"
,"错误的用户信息", JOptionPane.WARNING_MESSAGE);
}catch (Exception e){
e.printStackTrace();
}
}else {
JOptionPane.showMessageDialog(null,"注册用户名和密码不能为空");
return;
}
}
});
}
}
注册界面如下:
3、阿里云服务器中,新建数据库BlueBrige,在数据库中添加新数数据表user,新建表字段如下:
4、插入新的数据:
5、在项目中新建JDBUtils数据库工具类、druid.properties阿里云数据连接池配置文件:
工具类代码如下:
public class JDBCUtils {
//1.定义成员变量 DataSource
private static DataSource ds ;
static{
try {
//1.加载配置文件
Properties pro = new Properties();
pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
//2.获取DataSource
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取连接
*/
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
/**
* 释放资源
*/
public static void close(Statement stmt,Connection conn){
close(null,stmt,conn);
}
public static void close(ResultSet rs , Statement stmt, Connection conn){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt != null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();//归还连接
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 获取连接池方法
*/
public static DataSource getDataSource(){
return ds;
}
}
6、新建持久层代码,用来操作数据数据:
导包:
代码如下:
/*阿里云服务器数据库*/
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
/*用户登录*/
public User Login(String username, String password){
try {
String sql = "select * from user where username = ? and password = ?";
User user = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), username, password);
if (user!= null)return user;
else return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/*用户注册*/
public boolean addUser(User user){
//先判断是否有这个用户名:
String sql = "select * from user where username = ?";
try {
User userflag = template.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), user.getUsername());
if (userflag != null) return false;
//空值会提醒异常,记得抛出
}
catch (Exception e) {
//定义sql
sql = "insert into user(username,password,gender,phone,address) values(?,?,?,?,?)";
//执行sql
template.update(sql, user.getUsername(), user.getPassword(),
user.getGender(), user.getPhone(), user.getAddress());
}
return true;
}
7、测试程序功能:
登录:输入正确账号密码:
用户密码为空:
输入错误的用户密码:
点击注册用户来到用户注册页面:
(1)用户名、密码为空时:
(2)两次密码输入不一致时:
(3)注册用户名、密码有空格时:
(4)输入信息完全正确时:
(5)重复添加相同用户名:
(6)点击返回登录,用新账号登录:
8、使用navicat查看是否数据插入:
不足:
1、持久层代码可以优化;
2、swing做出来的界面还是非常粗糙;
3、功能逻辑设还不够严谨
二次修改于2020.7.9 14:02:32
最后
以上就是平常钢笔为你收集整理的基于Swing编程的用户登录、注册界面(操作阿里云服务器数据库)的全部内容,希望文章能够帮你解决基于Swing编程的用户登录、注册界面(操作阿里云服务器数据库)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复