文章目录
- 任务12
- 【任务12.1】创建用户信息表
- 【任务12.2】在com.qst.dms.entity下创建用户实体类User,以便封装用户数据
- 【任务12.3】在com.qst.dms.service下创建用户业务类UserService
- 【任务12.4】在项目根目录下创建图片文件夹images,存储dms.png
- 【任务12.5】在com.qst.dms.ui下创建用户注册窗口RegistFrame,并将用户注册信息保存到数据库
- 任务13
- 【任务13.1】在com.qst.dms.ui下,创建用户登录窗口LoginFrame,登录成功则进入系统主界面
- 【任务13.2】使用对话框优化LoginFrame登录窗口中的错误提示
- 【任务13.3】使用对话框优化RegistFrame注册窗口中的错误提示
任务12
【任务12.1】创建用户信息表
create table user
(
username VARCHAR(50) not null,
password VARCHAR(50),
gender INTEGER,
hobby VARCHAR(500),
address VARCHAR(50),
degree VARCHAR(50)
);
【任务12.2】在com.qst.dms.entity下创建用户实体类User,以便封装用户数据
成员变量参考12.1
public User(String username, String password, int sex, String hobby, String address, String degree)
程序设计
package com.qst.dms.entity;public class User {public String username;public String password;public Integer gender;public String hobby;public String address;public String degree;public User() {}public User(String username, String password, Integer gender, String hobby, String address, String degree) {this.username = username;this.password = password;this.gender = gender;this.hobby = hobby;this.address = address;this.degree = degree;}/*** 获取* @return username*/public String getUsername() {return username;}/*** 设置* @param username*/public void setUsername(String username) {this.username = username;}/*** 获取* @return password*/public String getPassword() {return password;}/*** 设置* @param password*/public void setPassword(String password) {this.password = password;}/*** 获取* @return gender*/public Integer getGender() {return gender;}/*** 设置* @param gender*/public void setGender(Integer gender) {this.gender = gender;}/*** 获取* @return hobby*/public String getHobby() {return hobby;}/*** 设置* @param hobby*/public void setHobby(String hobby) {this.hobby = hobby;}/*** 获取* @return address*/public String getAddress() {return address;}/*** 设置* @param address*/public void setAddress(String address) {this.address = address;}/*** 获取* @return degree*/public String getDegree() {return degree;}/*** 设置* @param degree*/public void setDegree(String degree) {this.degree = degree;}public String toString() {return "User{username = " + username + ", password = " + password + ", gender = " + gender + ", hobby = " + hobby + ", address = " + address + ", degree = " + degree + "}";}
}
【任务12.3】在com.qst.dms.service下创建用户业务类UserService
// 根据用户名查询用户,各用户的用户名不能相同
public User findUserByName(String userName) // 保存用户信息
public boolean saveUser(User user)
程序设计
package com.qst.dms.service;import com.qst.dms.entity.User;
import com.qst.dms.util.DBUtil;import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;public class UserService {DBUtil db = new DBUtil();Connection conn = null;// 根据用户名查询用户,各用户的用户名不能相同public User findUserByName(String userName) {// 返回符合条件的用户对象User user = new User();try {try {conn = db.getConnection();} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException throwables) {throwables.printStackTrace();} catch (InstantiationException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}String querySqluser = "SELECT * FROM user WHERE username = ?";Object[] userParams = {userName};ResultSet userResult = db.executeQuery(querySqluser, userParams);db.commitAll();if (userResult.next()) {// 设置登出记录的属性值user.setUsername(userResult.getString("username"));user.setPassword(userResult.getString("password"));user.setAddress(userResult.getString("address"));user.setGender(userResult.getInt("gender"));user.setHobby(userResult.getString("hobby"));user.setDegree(userResult.getString("degree"));}userResult.close();} catch (Exception e) {e.printStackTrace();} finally {// 关闭数据库连接,释放资源if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}return user;}// 保存用户信息public boolean saveUser(User user) {// 返回保存结果,成功返回true,失败返回falsetry {try {conn = db.getConnection();} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException throwables) {throwables.printStackTrace();} catch (InstantiationException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}String querySqluser = "INSERT INTO user VALUES (?, ?, ?, ?, ?, ?)";Object[] queryParams = {user.getUsername(), user.getPassword(), user.getGender(), user.getHobby(), user.getAddress(), user.getDegree()};db.executeUpdate(querySqluser, queryParams);db.commitAll();return true;} catch (Exception e) {e.printStackTrace();} finally {// 关闭数据库连接,释放资源if (conn != null) {try {conn.close();} catch (SQLException e) {e.printStackTrace();}}}return false;}
}
【任务12.4】在项目根目录下创建图片文件夹images,存储dms.png
【任务12.5】在com.qst.dms.ui下创建用户注册窗口RegistFrame,并将用户注册信息保存到数据库
Frame
- RegistFrame类,面板,7个标签,1个文本域,2个密码域,2个单选框,4个多选框,1个多行文本域,1个下拉复选框,2个按钮、user userService
- Logo 窗口标题 缩小、放大、关闭 标签 位置 大小
- Panel 面板,网格布局,布局 8行一列,嵌套面板,8个子面板,流式布局
- 7个标签,1个文本域,2个密码域,2个单选框,4个多选框,1个多行文本域,1个下拉复选框,2个按钮
- 8个子面板分别加入相关元素
- 单选框,男女单选框,单选组
- 确定动作及相关处理,动作监听
// 注册监听器,监听确定按钮btnOk.addActionListener(new RegisterListener());
- 重置动作及相关处理,动作监听
// 注册监听器,监听重置按钮
btnCancle.addActionListener(new ResetListener());
- RegistFrame中的成员属性如下:
// 主面板
private JPanel p;// 标签
private JLabel lblName, lblPwd, lblRePwd, lblSex, lblHobby, lblAdress,
lblDegree;
// 用户名,文本框
private JTextField txtName;
// 密码和确认密码,密码框
private JPasswordField txtPwd, txtRePwd;
// 性别,单选按钮
private JRadioButton rbMale, rbFemale;
// 爱好,多选框
private JCheckBox ckbRead, ckbNet, ckbSwim, ckbTour;
// 地址,文本域
private JTextArea txtAdress;
// 学历,组合框
private JComboBox<String> cmbDegree;
// 确认和取消,按钮
private JButton btnOk, btnCancle;
// 注册的用户
private static User user;// 用户业务类
private UserService userService;// 构造方法public RegistFrame() {super("用户注册");// 实例化用户业务类对象userService = new UserService();// 设置窗体的iconImageIcon icon = new ImageIcon("images\\dms.png");this.setIconImage(icon.getImage());// 设置面板布局,网格布局p = new JPanel(new GridLayout(8, 1));// 实例化组件lblName = new JLabel("用 户 名:");lblPwd = new JLabel("密 码:");lblRePwd = new JLabel("确认密码:");lblSex = new JLabel("性 别:");lblHobby = new JLabel("爱 好:");lblAdress = new JLabel("地 址:");lblDegree = new JLabel("学 历:");txtName = new JTextField(16);txtPwd = new JPasswordField(16);txtRePwd = new JPasswordField(16);rbMale = new JRadioButton("男");rbFemale = new JRadioButton("女");// 性别的单选逻辑ButtonGroup bg = new ButtonGroup();bg.add(rbMale);bg.add(rbFemale);ckbRead = new JCheckBox("阅读");ckbNet = new JCheckBox("上网");ckbSwim = new JCheckBox("游泳");ckbTour = new JCheckBox("旅游");txtAdress = new JTextArea(3, 20);// 组合框显示的学历数组String str[] = { "小学", "初中", "高中", "本科", "硕士", "博士" };cmbDegree = new JComboBox<String>(str);// 设置组合框可编辑cmbDegree.setEditable(true);btnOk = new JButton("确定");// 注册监听器,监听确定按钮btnOk.addActionListener(new RegisterListener());btnCancle = new JButton("重置");// 注册监听器,监听重置按钮btnCancle.addActionListener(new ResetListener());// 将组件分组放入面板,然后将小面板放入主面板JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));p1.add(lblName);p1.add(txtName);p.add(p1);JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT));p2.add(lblPwd);p2.add(txtPwd);p.add(p2);JPanel p3 = new JPanel(new FlowLayout(FlowLayout.LEFT));p3.add(lblRePwd);p3.add(txtRePwd);p.add(p3);JPanel p4 = new JPanel(new FlowLayout(FlowLayout.LEFT));p4.add(lblSex);p4.add(rbMale);p4.add(rbFemale);p.add(p4);JPanel p5 = new JPanel(new FlowLayout(FlowLayout.LEFT));p5.add(lblHobby);p5.add(ckbRead);p5.add(ckbNet);p5.add(ckbSwim);p5.add(ckbTour);p.add(p5);JPanel p6 = new JPanel(new FlowLayout(FlowLayout.LEFT));p6.add(lblAdress);p6.add(txtAdress);p.add(p6);JPanel p7 = new JPanel(new FlowLayout(FlowLayout.LEFT));p7.add(lblDegree);p7.add(cmbDegree);p.add(p7);JPanel p8 = new JPanel(new FlowLayout(FlowLayout.CENTER));p8.add(btnOk);p8.add(btnCancle);p.add(p8);// 主面板放入窗体中this.add(p);// 设置窗体大小和位置居中this.setSize(310, 350);this.setLocationRelativeTo(null);// 设置窗体不可改变大小this.setResizable(false);// 设置窗体初始可见this.setVisible(true);}// 监听类,负责处理确认按钮的业务逻辑private class RegisterListener implements ActionListener {// 重写actionPerFormed()方法,事件处理方法public void actionPerformed(ActionEvent e) {// 获取用户输入的数据}}// 监听类,负责处理重置按钮public class ResetListener implements ActionListener {// 重写actionPerFormed()方法,重置组件内容事件处理方法public void actionPerformed(ActionEvent e) {// 清空姓名、密码、确认密码内容txtName.setText("");txtPwd.setText("");txtRePwd.setText("");// 重置单选按钮为未选择rbMale.setSelected(false);rbFemale.setSelected(false);// 重置所有的复选按钮为未选择ckbRead.setSelected(false);ckbNet.setSelected(false);ckbSwim.setSelected(false);ckbTour.setSelected(false);// 清空地址栏txtAdress.setText("");// 重置组合框为未选择状态cmbDegree.setSelectedIndex(0);}}
程序设计
package com.qst.dms.ui;import com.qst.dms.entity.User;
import com.qst.dms.service.UserService;import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class RegistFrame extends JFrame{// 主面板private JPanel p;// 标签private JLabel lblName, lblPwd, lblRePwd, lblSex, lblHobby, lblAdress,lblDegree;// 用户名,文本框private static JTextField txtName;// 密码和确认密码,密码框private static JPasswordField txtPwd;private static JPasswordField txtRePwd;// 性别,单选按钮private static JRadioButton rbMale;private static JRadioButton rbFemale;// 爱好,多选框private static JCheckBox ckbRead;private static JCheckBox ckbNet;private static JCheckBox ckbSwim;private static JCheckBox ckbTour;// 地址,文本域private static JTextArea txtAdress;// 学历,组合框private static JComboBox<String> cmbDegree;// 确认和取消,按钮private JButton btnOk, btnCancle;// 注册的用户private static User user;// 用户业务类private UserService userService;private Image iconImage;// 构造方法public RegistFrame() {super();// 实例化用户业务类对象userService = new UserService();// 设置窗体的iconImageIcon icon = new ImageIcon("images\\dms.png");this.setIconImage(icon.getImage());// 设置面板布局,网格布局p = new JPanel(new GridLayout(8, 1));// 实例化组件lblName = new JLabel("用 户 名:");lblPwd = new JLabel("密 码:");lblRePwd = new JLabel("确认密码:");lblSex = new JLabel("性 别:");lblHobby = new JLabel("爱 好:");lblAdress = new JLabel("地 址:");lblDegree = new JLabel("学 历:");txtName = new JTextField(16);txtPwd = new JPasswordField(16);txtRePwd = new JPasswordField(16);rbMale = new JRadioButton("男");rbFemale = new JRadioButton("女");// 性别的单选逻辑ButtonGroup bg = new ButtonGroup();bg.add(rbMale);bg.add(rbFemale);ckbRead = new JCheckBox("阅读");ckbNet = new JCheckBox("上网");ckbSwim = new JCheckBox("游泳");ckbTour = new JCheckBox("旅游");txtAdress = new JTextArea(3, 20);// 组合框显示的学历数组String str[] = { "小学", "初中", "高中", "本科", "硕士", "博士" };cmbDegree = new JComboBox<String>(str);// 设置组合框可编辑cmbDegree.setEditable(true);btnOk = new JButton("确定");// 注册监听器,监听确定按钮btnOk.addActionListener((ActionListener) new RegisterListener());btnCancle = new JButton("重置");// 注册监听器,监听重置按钮btnCancle.addActionListener(new ResetListener());// 将组件分组放入面板,然后将小面板放入主面板JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));p1.add(lblName);p1.add(txtName);p.add(p1);JPanel p2 = new JPanel(new FlowLayout(FlowLayout.LEFT));p2.add(lblPwd);p2.add(txtPwd);p.add(p2);JPanel p3 = new JPanel(new FlowLayout(FlowLayout.LEFT));p3.add(lblRePwd);p3.add(txtRePwd);p.add(p3);JPanel p4 = new JPanel(new FlowLayout(FlowLayout.LEFT));p4.add(lblSex);p4.add(rbMale);p4.add(rbFemale);p.add(p4);JPanel p5 = new JPanel(new FlowLayout(FlowLayout.LEFT));p5.add(lblHobby);p5.add(ckbRead);p5.add(ckbNet);p5.add(ckbSwim);p5.add(ckbTour);p.add(p5);JPanel p6 = new JPanel(new FlowLayout(FlowLayout.LEFT));p6.add(lblAdress);p6.add(txtAdress);p.add(p6);JPanel p7 = new JPanel(new FlowLayout(FlowLayout.LEFT));p7.add(lblDegree);p7.add(cmbDegree);p.add(p7);JPanel p8 = new JPanel(new FlowLayout(FlowLayout.CENTER));p8.add(btnOk);p8.add(btnCancle);p.add(p8);// 主面板放入窗体中this.add(p);// 设置窗体大小和位置居中this.setSize(310, 400);this.setLocationRelativeTo(null);// 设置窗体不可改变大小this.setResizable(false);// 设置窗体初始可见this.setVisible(true);}public void setIconImage(Image iconImage) {this.iconImage = iconImage;}// 监听类,负责处理确认按钮的业务逻辑private class RegisterListener implements ActionListener {// 重写actionPerFormed()方法,事件处理方法public void actionPerformed(ActionEvent e) {// 获取用户输入的数据String userName = txtName.getText().trim();String password = new String(txtPwd.getPassword());String rePassword = new String(txtRePwd.getPassword());int sex = Integer.parseInt(rbFemale.isSelected()?"0":"1");String hobby = (ckbRead.isSelected()?"阅读":"")+ (ckbNet.isSelected()?"上网":"")+ (ckbNet.isSelected()?"游泳":"")+ (ckbNet.isSelected()?"旅游":"");String address = txtAdress.getText().trim();String degree = cmbDegree.getSelectedItem().toString().trim();//判断两次输入密码是否一致if (password.equals(rePassword)){//将数据封装到对象中user = new User(userName, password, sex, hobby, address, degree);//保存数据if (userService.saveUser(user)){//输出提示信息JOptionPane.showMessageDialog(null,"注册成功!","成功提示",JOptionPane.PLAIN_MESSAGE);}else{//输出提示信息JOptionPane.showMessageDialog(null,"注册失败!","错误提示",JOptionPane.ERROR_MESSAGE);}}else{//输出提示信息JOptionPane.showMessageDialog(null,"两次输入的密码不一致!","错误提示",JOptionPane.ERROR_MESSAGE);}}}// 监听类,负责处理重置按钮public static class ResetListener implements ActionListener {// 重写actionPerFormed()方法,重置组件内容事件处理方法public void actionPerformed(ActionEvent e) {// 清空姓名、密码、确认密码内容txtName.setText("");txtPwd.setText("");txtRePwd.setText("");// 重置单选按钮为未选择rbMale.setSelected(false);rbFemale.setSelected(false);// 重置所有的复选按钮为未选择ckbRead.setSelected(false);ckbNet.setSelected(false);ckbSwim.setSelected(false);ckbTour.setSelected(false);// 清空地址栏txtAdress.setText("");// 重置组合框为未选择状态cmbDegree.setSelectedIndex(0);}}
}
任务13
【任务13.1】在com.qst.dms.ui下,创建用户登录窗口LoginFrame,登录成功则进入系统主界面
// 主面板
private JPanel p;
// 标签
private JLabel lblName, lblPwd;
// 用户名,文本框
private JTextField txtName;
// 密码,密码框
private JPasswordField txtPwd;
// 确认、取消和注册,按钮
private JButton btnOk, btnCancle, btnRegist;
// 登录用户
private static User user;// 用户业务类
private UserService userService;// 构造方法public LoginFrame() {super("登录");// 实例化用户业务类对象userService = new UserService();// 设置窗体的iconImageIcon icon = new ImageIcon("images\\dms.png");this.setIconImage(icon.getImage());// 实例化组件p = new JPanel();// 使用null布局p.setLayout(null);lblName = new JLabel("用户名:");lblPwd = new JLabel("密 码:");txtName = new JTextField(20);txtPwd = new JPasswordField(20);txtPwd.setEchoChar('*');btnOk = new JButton("登录");btnOk.addActionListener(new LoginListener());btnCancle = new JButton("重置");btnCancle.addActionListener(new ResetListener());btnRegist = new JButton("注册");btnRegist.addActionListener(new RegistListener());lblName.setBounds(30, 30, 60, 25);lblPwd.setBounds(30, 60, 60, 25);txtName.setBounds(95, 30, 120, 25);txtPwd.setBounds(95, 60, 120, 25);btnOk.setBounds(30, 90, 60, 25);btnCancle.setBounds(95, 90, 60, 25);btnRegist.setBounds(160, 90, 60, 25);p.add(lblName);p.add(txtName);p.add(lblPwd);p.add(txtPwd);p.add(btnOk);p.add(btnCancle);p.add(btnRegist);// 主面板放入窗体中this.add(p);// 设置窗体大小和位置this.setSize(250, 170);// 设置窗口在屏幕中央this.setLocationRelativeTo(null);// 设置窗体不能改变大小this.setResizable(false);// 设置窗体的默认关闭按钮this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗体初始可见this.setVisible(true);}// 监听类,负责处理登录按钮public class LoginListener implements ActionListener {// 重写actionPerFormed()方法,事件处理逻辑public void actionPerformed(ActionEvent e) {// 根据用户名查询用户}}// 监听类,负责处理重置按钮public class ResetListener implements ActionListener {// 重写actionPerFormed()方法,事件处理方法public void actionPerformed(ActionEvent e) {// 清空文本框txtName.setText("");txtPwd.setText("");}}// 监听类,负责处理注册按钮public class RegistListener implements ActionListener {// 重写actionPerFormed()方法,事件处理方法public void actionPerformed(ActionEvent e) {// 创建注册窗口new RegistFrame();}}
程序设计
package com.qst.dms.ui;import com.qst.dms.entity.User;
import com.qst.dms.service.UserService;
import sun.rmi.log.ReliableLog;
import sun.rmi.runtime.Log;import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;public class LoginFrame extends JFrame {// 主面板private JPanel p;// 标签private JLabel lblName, lblPwd;// 用户名,文本框private JTextField txtName;// 密码,密码框private JPasswordField txtPwd;// 确认、取消和注册,按钮private JButton btnOk, btnCancle, btnRegist;// 登录用户private static User user;// 用户业务类private UserService userService;// 构造方法public LoginFrame() {super("登录");// 实例化用户业务类对象userService = new UserService();// 设置窗体的iconImageIcon icon = new ImageIcon("images\\dms.png");this.setIconImage(icon.getImage());// 实例化组件p = new JPanel();// 使用null布局p.setLayout(null);lblName = new JLabel("用户名:");lblPwd = new JLabel("密 码:");txtName = new JTextField(20);txtPwd = new JPasswordField(20);txtPwd.setEchoChar('*');btnOk = new JButton("登录");btnOk.addActionListener((ActionListener) new LoginListener());btnCancle = new JButton("重置");btnCancle.addActionListener(new RegistFrame.ResetListener());btnRegist = new JButton("注册");btnRegist.addActionListener(new RegistListener());lblName.setBounds(30, 30, 60, 25);lblPwd.setBounds(30, 60, 60, 25);txtName.setBounds(95, 30, 120, 25);txtPwd.setBounds(95, 60, 120, 25);btnOk.setBounds(30, 90, 60, 25);btnCancle.setBounds(95, 90, 60, 25);btnRegist.setBounds(160, 90, 60, 25);p.add(lblName);p.add(txtName);p.add(lblPwd);p.add(txtPwd);p.add(btnOk);p.add(btnCancle);p.add(btnRegist);// 主面板放入窗体中this.add(p);// 设置窗体大小和位置this.setSize(250, 170);// 设置窗口在屏幕中央this.setLocationRelativeTo(null);// 设置窗体不能改变大小this.setResizable(false);// 设置窗体的默认关闭按钮this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 设置窗体初始可见this.setVisible(true);}// 监听类,负责处理登录按钮public class LoginListener implements ActionListener {// 重写actionPerFormed()方法,事件处理逻辑public void actionPerformed(ActionEvent e) {// 根据用户名查询用户user = userService.findUserByName((txtName.getText().trim()));if(user != null) {//判断输入的密码是否正确if (user.getPassword().equals(new String(txtPwd.getPassword()))) {//输出提示信息JOptionPane.showMessageDialog(null,"登录成功!","成功提示",JOptionPane.PLAIN_MESSAGE);//登录成功LoginFrame.this.setVisible(false);//显示主窗口//new MainFrame();} else {//输出提示信息JOptionPane.showMessageDialog(null, "密码错误!请重新输入!", "错误提示", JOptionPane.ERROR_MESSAGE);//清空密码框txtPwd.setText("");}}else{//输出提示信息JOptionPane.showMessageDialog(null,"该用户不存在,请先注册!","错误提示",JOptionPane.ERROR_MESSAGE);}}}// 监听类,负责处理重置按钮public class ResetListener implements ActionListener {// 重写actionPerFormed()方法,事件处理方法public void actionPerformed(ActionEvent e) {// 清空文本框txtName.setText("");txtPwd.setText("");}}// 监听类,负责处理注册按钮public class RegistListener implements ActionListener {// 重写actionPerFormed()方法,事件处理方法public void actionPerformed(ActionEvent e) {// 创建注册窗口new RegistFrame();}}
}
【任务13.2】使用对话框优化LoginFrame登录窗口中的错误提示
JOptionPane.showMessageDialog(null,"密码错误!请重新输入!","错误提示", JOptionPane.ERROR_MESSAGE);JOptionPane.showMessageDialog(null,"该用户不存在,请先注册!","错误提示",JOptionPane.ERROR_MESSAGE);
【任务13.3】使用对话框优化RegistFrame注册窗口中的错误提示
JOptionPane.showMessageDialog(null,"注册成功!","成功提示",JOptionPane.PLAIN_MESSAGE);JOptionPane.showMessageDialog(null,"注册失败!","错误提示",JOptionPane.ERROR_MESSAGE);JOptionPane.showMessageDialog(null,"两次输入的密码不一致!","错误提示",JOptionPane.ERROR_MESSAGE);
测试
注册界面
登录界面