Java实训项目:GUI学生信息管理系统
程序员文章站
2024-03-16 14:18:04
...
Java实训项目:GUI学生信息管理系统
一、功能需求
1、可设置学校信息
2、可设置状态栏信息
3、登录与注册功能
4、登录用户修改密码功能
5、对学生记录增加和修改
6、查询:按学号、姓名、班级和系部进行查询
7、删除:按学号、班级、系部删除记录
8、统计:按性别、班级、系部统计人数
二、功能结构图
三、运行效果
1、登录窗口
2、注册窗口
3、主窗口
4、【系统设置】功能模块
(1)设置学校信息
(2)设置状态栏信息
(3)修改用户密码
5、【数据操作】功能模块
(1)增加学生记录
(2)编辑学生记录
(3)删除学生记录
u 按学号删除学生记录
u 按班级删除学生记录
u 按系部删除学生记录
(4) 浏览学生记录
6、【查询学生】功能模块
(1)按学号查询学生记录
(2)按姓名查询学生记录
(3)按班级查询学生记录
(4)按系部查询学生记录
7、【人数统计】功能模块
(1)按性别统计人数
(2)按班级统计人数
(3)按系部统计人数
8、【帮助】功能模块
(1)关于
(2)帮助
四、涉及知识点
(1)Java基本语法
(2)Java流程控制
(3)Java数据库编程(JDBC)
(4)Java面向对象编程
(5)Java核心API
(6)MySQL数据库操作
(7)MVC模式分层架构
五、项目结构图
六、实现步骤
1、创建Java项目
2、输入项目名
3、在项目里创建文件夹
4、在lib里添加jar包
5、在images里添加图片
6、在help里添加帮助文档
7、实体类
(1)学校实体College
/**
* 包名:net.hw.student.bean
* 类名:College
* 描述:学校实体
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.bean;
import java.util.Date;
public class College {
/**
* 学校标识符
*/
private int id;
/**
* 学校名称
*/
private String name;
/**
* 校长
*/
private String president;
/**
* 建校时间
*/
private Date startTime;
/**
* 联系电话
*/
private String telephone;
/**
* 电子邮箱
*/
private String email;
/**
* 通信地址
*/
private String address;
/**
* 学校简介
*/
private String profile;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPresident() {
return president;
}
public void setPresident(String president) {
this.president = president;
}
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getProfile() {
return profile;
}
public void setProfile(String profile) {
this.profile = profile;
}
@Override
public String toString() {
return "College [id=" + id + ", name=" + name + ", president=" + president + ", startTime=" + startTime
+ ", telephone=" + telephone + ", email=" + email + ", address=" + address + ", profile=" + profile
+ "]";
}
}
(2)状态实体Status
/**
* 包名:net.hw.student.bean
* 类名:Status
* 描述: 状态实体
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.bean;
public class Status {
/**
* 标识符
*/
private int id;
/**
* 校名
*/
private String college;
/**
* 版本
*/
private String version;
/**
* 作者
*/
private String author;
/**
* 联系电话
*/
private String telephone;
/**
* 通信地址
*/
private String address;
/**
* 电子邮箱
*/
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCollege() {
return college;
}
public void setCollege(String college) {
this.college = college;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Status [id=" + id + ", college=" + college + ", version=" + version + ", author=" + author
+ ", telephone=" + telephone + ", address=" + address + ", email=" + email + "]";
}
}
(3)学生实体Student
/**
* 包名:net.hw.student.bean
* 类名:Student
* 描述:学生实体
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.bean;
public class Student {
/**
* 学号
*/
private String id;
/**
* 姓名
*/
private String name;
/**
* 性别
*/
private String sex;
/**
* 年龄
*/
private int age;
/**
* 系部
*/
private String department;
/**
* 班级
*/
private String clazz;
/**
* 联系电话
*/
private String telephone;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + ", department=" + department
+ ", clazz=" + clazz + ", telephone=" + telephone + "]";
}
}
(4)用户实体User
/**
* 包名:net.hw.student.bean
* 类名:User
* 描述:
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.bean;
import java.util.Date;
public class User {
/**
* 用户标识符
*/
private int id;
/**
* 用户名
*/
private String usernname;
/**
* 密码
*/
private String password;
/**
* 联系电话
*/
private String telephone;
/**
* 注册时间
*/
private Date registerTime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsernname() {
return usernname;
}
public void setUsernname(String usernname) {
this.usernname = usernname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public Date getRegisterTime() {
return registerTime;
}
public void setRegisterTime(Date registerTime) {
this.registerTime = registerTime;
}
@Override
public String toString() {
return "User [id=" + id + ", usernname=" + usernname + ", password=" + password + ", telephone=" + telephone
+ ", registerTime=" + registerTime + "]";
}
}
8、数据库连接管理类
/**
* 包名:net.hw.student.dbutil
* 类名:ConnectionManager
* 描述:数据库连接管理类
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.dbutil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JOptionPane;
public class ConnectionManager {
/**
* 数据库驱动程序
*/
private static final String DRIVER = "com.mysql.jdbc.Driver";
/**
* 数据库统一资源标识符
*/
private static final String URL = "jdbc:mysql://localhost:3306/student";
/**
* 数据库用户名
*/
private static final String USERNAME = "root";
/**
* 数据库密码
*/
private static final String PASSWORD = "root";
/**
* 私有化构造方法,拒绝实例化
*/
private ConnectionManager() {
}
/**
* 获得数据库连接
*
* @return 数据库连接对象
*/
public static Connection getConnection() {
// 定义数据库连接
Connection conn = null;
try {
// 安装数据库驱动程序
Class.forName(DRIVER);
// 获得数据库连接
conn = DriverManager.getConnection(URL
+ "?useUnicode=true&characterEncoding=UTF8", USERNAME, PASSWORD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
// 返回数据库连接
return conn;
}
/**
* 关闭数据库连接
*
* @param conn
*/
public static void closeConnection(Connection conn) {
// 判断数据库连接是否为空
if (conn != null) {
// 判断数据库连接是否关闭
try {
if (!conn.isClosed()) {
// 关闭数据库连接
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 测试数据库连接是否成功
*
* @param args
*/
public static void main(String[] args) {
// 获得数据库连接
Connection conn = getConnection();
// 判断是否连接成功
if (conn != null) {
JOptionPane.showMessageDialog(null, "恭喜,数据库连接成功!");
} else {
JOptionPane.showMessageDialog(null, "遗憾,数据库连接失败!");
}
// 关闭数据库连接
closeConnection(conn);
}
}
9、数据访问接口
(1)学校数据访问接口CollegeDao
/**
* 包名:net.hw.student.dao
* 类名:CollegeDao
* 描述:学校数据访问接口
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.dao;
import net.hw.student.bean.College;
public interface CollegeDao {
College findById(int id);
int update(College college);
}
(2)状态数据访问接口StatusDao
/**
* 包名:net.hw.student.dao
* 类名:StatusDao
* 描述: 状态数据访问接口
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.dao;
import net.hw.student.bean.Status;
public interface StatusDao {
Status findById(int id);
int update(Status status);
}
(3)学生数据访问接口StudentDao
/**
* 包名:net.hw.student.dao
* 类名:StudentDao
* 描述:学生数据访问接口
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.dao;
import java.util.List;
import java.util.Vector;
import net.hw.student.bean.Student;
public interface StudentDao {
int insert(Student student);
int deleteById(String id);
int deleteByClass(String clazz);
int deleteByDepartment(String department);
int update(Student student);
Student findById(String id);
List<Student> findByName(String name);
List<Student> findByClass(String clazz);
List<Student> findByDepartment(String department);
List<Student> findAll();
Vector findRowsBySex();
Vector findRowsByClass();
Vector findRowsByDepartment();
}
(4)用户数据访问接口UserDao
/**
* 包名:net.hw.student.dao
* 类名:UserDao
* 描述:用户数据访问接口
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.dao;
import java.util.List;
import net.hw.student.bean.User;
public interface UserDao {
int insert(User user);
int deleteById(int id);
int update(User user);
User findById(int id);
List<User> findAll();
User login(String username, String password);
}
10、数据访问接口实现类
(1)学校数据访问接口实现类CollegeDaoImpl
/**
* 包名:net.hw.student.dao.impl
* 类名:CollegeDaoImpl
* 描述:学校数据访问接口实现类
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import net.hw.student.bean.College;
import net.hw.student.dao.CollegeDao;
import net.hw.student.dbutil.ConnectionManager;
public class CollegeDaoImpl implements CollegeDao{
@Override
public College findById(int id) {
// 声明学校对象
College college = null;
// 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "SELECT * FROM t_college WHERE id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setInt(1, id);
// 执行SQL查询,返回结果集
ResultSet rs = pstmt.executeQuery();
// 判断结果集是否有记录
if (rs.next()) {
// 实例化学校
college = new College();
// 利用当前记录字段值去设置学校对象的属性
college.setId(rs.getInt("id"));
college.setName(rs.getString("name"));
college.setPresident(rs.getString("president"));
college.setStartTime(rs.getDate("start_time"));
college.setTelephone(rs.getString("telephone"));
college.setEmail(rs.getString("email"));
college.setAddress(rs.getString("address"));
college.setProfile(rs.getString("profile"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回学校对象
return college;
}
@Override
public int update(College college) {
// 定义更新记录数
int count = 0;
// 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "UPDATE t_college SET name = ?, president = ?, start_time = ?,"
+ " telephone = ?, email = ?, address = ?, profile = ? WHERE id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, college.getName());
pstmt.setString(2, college.getPresident());
pstmt.setTimestamp(3, new Timestamp(college.getStartTime().getTime()));
pstmt.setString(4, college.getTelephone());
pstmt.setString(5, college.getEmail());
pstmt.setString(6, college.getAddress());
pstmt.setString(7, college.getProfile());
pstmt.setInt(8, college.getId());
// 执行更新操作,更新记录
count = pstmt.executeUpdate();
// 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回更新记录数
return count;
}
}
(2)状态数据访问接口实现类StatusDaoImpl
/**
* 包名:net.hw.student.dao.impl
* 类名:StatusDaoImpl
* 描述:状态数据访问接口实现类
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import net.hw.student.bean.Status;
import net.hw.student.dao.StatusDao;
import net.hw.student.dbutil.ConnectionManager;
public class StatusDaoImpl implements StatusDao {
@Override
public Status findById(int id) {
// 声明状态对象
Status status = null;
// 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "SELECT * FROM t_status WHERE id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setInt(1, id);
// 执行SQL查询,返回结果集
ResultSet rs = pstmt.executeQuery();
// 判断结果集是否有记录
if (rs.next()) {
// 实例化状态
status = new Status();
// 利用当前记录字段值去设置状态对象的属性
status.setId(rs.getInt("id"));
status.setCollege(rs.getString("college"));
status.setVersion(rs.getString("version"));
status.setAuthor(rs.getString("author"));
status.setTelephone(rs.getString("telephone"));
status.setAddress(rs.getString("address"));
status.setEmail(rs.getString("email"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回状态对象
return status;
}
@Override
public int update(Status status) {
// 定义更新记录数
int count = 0;
// 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "UPDATE t_status SET college = ?, version = ?, author = ?,"
+ " telephone = ?, address = ?, email = ? WHERE id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, status.getCollege());
pstmt.setString(2, status.getVersion());
pstmt.setString(3, status.getAuthor());
pstmt.setString(4, status.getTelephone());
pstmt.setString(5, status.getAddress());
pstmt.setString(6, status.getEmail());
pstmt.setInt(7, status.getId());
// 执行更新操作,更新记录
count = pstmt.executeUpdate();
// 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回更新记录数
return count;
}
}
(3)学生数据访问接口实现类StudentDaoImpl
/**
* 包名:net.hw.student.dao.impl
* 类名:StudentDaoImpl
* 描述:学生数据访问接口实现类
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import net.hw.student.bean.Student;
import net.hw.student.dao.StudentDao;
import net.hw.student.dbutil.ConnectionManager;
public class StudentDaoImpl implements StudentDao {
@Override
public int deleteByClass(String clazz) {
// 定义删除记录数
int count = 0;
// 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "DELETE FROM t_student WHERE class = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, clazz);
// 执行更新操作,删除记录
count = pstmt.executeUpdate();
// 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回删除记录数
return count;
}
@Override
public int deleteByDepartment(String department) {
// 定义删除记录数
int count = 0;
// 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "DELETE FROM t_student WHERE department = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, department);
// 执行更新操作,删除记录
count = pstmt.executeUpdate();
// 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回删除记录数
return count;
}
@Override
public int deleteById(String id) {
// 定义删除记录数
int count = 0;
// 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "DELETE FROM t_student WHERE id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, id);
// 执行更新操作,删除记录
count = pstmt.executeUpdate();
// 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回删除记录数
return count;
}
@Override
public List<Student> findAll() {
// 声明用户列表
List<Student> students = new ArrayList<Student>();
// 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "SELECT * FROM t_student";
try {
// 创建语句对象
Statement stmt = conn.createStatement();
// 执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
// 遍历结果集
while (rs.next()) {
// 创建学生实体
Student student = new Student();
// 设置实体属性
student.setId(rs.getString("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
student.setClazz(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
// 将实体添加到学生列表
students.add(student);
}
// 关闭结果集
rs.close();
// 关闭语句对象
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回学生列表
return students;
}
/**
* 按班级查询学生
*/
@Override
public List<Student> findByClass(String clazz) {
// 声明用户列表
List<Student> students = new ArrayList<Student>();
// 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "SELECT * FROM t_student WHERE class LIKE ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, clazz + "%");
// 执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
// 遍历结果集
while (rs.next()) {
// 创建学生实体
Student student = new Student();
// 设置实体属性
student.setId(rs.getString("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
student.setClazz(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
// 将实体添加到学生列表
students.add(student);
}
// 关闭结果集
rs.close();
// 关闭语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回学生列表
return students;
}
/**
* 按系部查询学生
*/
@Override
public List<Student> findByDepartment(String department) {
// 声明用户列表
List<Student> students = new ArrayList<Student>();
// 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "SELECT * FROM t_student WHERE department LIKE ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, department + "%");
// 执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
// 遍历结果集
while (rs.next()) {
// 创建学生实体
Student student = new Student();
// 设置实体属性
student.setId(rs.getString("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
student.setClazz(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
// 将实体添加到学生列表
students.add(student);
}
// 关闭结果集
rs.close();
// 关闭语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回学生列表
return students;
}
@Override
public Student findById(String id) {
// 声明学生
Student student = null;
// 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "SELECT * FROM t_student WHERE id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, id);
// 执行SQL查询,返回结果集
ResultSet rs = pstmt.executeQuery();
// 判断结果集是否有记录
if (rs.next()) {
// 创建学生实体
student = new Student();
// 设置实体属性
student.setId(rs.getString("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
student.setClazz(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回学生
return student;
}
@Override
public List<Student> findByName(String name) {
// 声明用户列表
List<Student> students = new ArrayList<Student>();
// 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "SELECT * FROM t_student WHERE name LIKE ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, name + "%");
// 执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
// 遍历结果集
while (rs.next()) {
// 创建学生实体
Student student = new Student();
// 设置实体属性
student.setId(rs.getString("id"));
student.setName(rs.getString("name"));
student.setSex(rs.getString("sex"));
student.setAge(rs.getInt("age"));
student.setDepartment(rs.getString("department"));
student.setClazz(rs.getString("class"));
student.setTelephone(rs.getString("telephone"));
// 将实体添加到学生列表
students.add(student);
}
// 关闭结果集
rs.close();
// 关闭语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回学生列表
return students;
}
@Override
public Vector findRowsByClass() {
// 定义向量
Vector rows = new Vector();
// 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "SELECT class AS '班级', count(*) AS '人数'" + " FROM t_student GROUP BY class ORDER BY class DESC";
try {
// 创建语句对象
Statement stmt = conn.createStatement();
// 执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
// 遍历结果集
while (rs.next()) {
Vector<String> currentRow = new Vector();
currentRow.addElement(rs.getString("班级"));
currentRow.addElement(rs.getInt("人数") + "");
rows.addElement(currentRow);
}
} catch (SQLException e) {
e.printStackTrace();
}
return rows;
}
@Override
public Vector findRowsByDepartment() {
// 定义向量
Vector rows = new Vector();
// 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "SELECT department AS '系部', count(*) AS '人数'"
+ " FROM t_student GROUP BY department ORDER BY department DESC";
try {
// 创建语句对象
Statement stmt = conn.createStatement();
// 执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
// 遍历结果集
while (rs.next()) {
Vector<String> currentRow = new Vector();
currentRow.addElement(rs.getString("系部"));
currentRow.addElement(rs.getInt("人数") + "");
rows.addElement(currentRow);
}
} catch (SQLException e) {
e.printStackTrace();
}
return rows;
}
@Override
public Vector findRowsBySex() {
// 定义向量
Vector rows = new Vector();
// 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "SELECT sex AS '性别', count(*) AS '人数'" + " FROM t_student GROUP BY sex ORDER BY sex DESC";
try {
// 创建语句对象
Statement stmt = conn.createStatement();
// 执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
// 遍历结果集
while (rs.next()) {
Vector<String> currentRow = new Vector();
currentRow.addElement(rs.getString("性别"));
currentRow.addElement(rs.getInt("人数") + "");
rows.addElement(currentRow);
}
} catch (SQLException e) {
e.printStackTrace();
}
return rows;
}
@Override
public int insert(Student student) {
// 定义插入记录数
int count = 0;
// 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "INSERT INTO t_student (id, name, sex, age, department, class, telephone)"
+ " VALUES (?, ?, ?, ?, ?, ?, ?)";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, student.getId());
pstmt.setString(2, student.getName());
pstmt.setString(3, student.getSex());
pstmt.setInt(4, student.getAge());
pstmt.setString(5, student.getDepartment());
pstmt.setString(6, student.getClazz());
pstmt.setString(7, student.getTelephone());
// 执行更新操作,插入新记录
count = pstmt.executeUpdate();
// 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回插入记录数
return count;
}
@Override
public int update(Student student) {
// 定义更新记录数
int count = 0;
// 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "UPDATE t_student SET name = ?, sex = ?, age = ?,"
+ " department = ?, class = ?, telephone = ? WHERE id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, student.getName());
pstmt.setString(2, student.getSex());
pstmt.setInt(3, student.getAge());
pstmt.setString(4, student.getDepartment());
pstmt.setString(5, student.getClazz());
pstmt.setString(6, student.getTelephone());
pstmt.setString(7, student.getId());
// 执行更新操作,更新记录
count = pstmt.executeUpdate();
// 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
System.err.println(e.getMessage());
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回更新记录数
return count;
}
}
(4)用户数据访问接口实现类UserDaoImpl
/**
* 包名:net.hw.student.dao.impl
* 类名:UserDaoImpl
* 描述:用户数据访问接口实现类
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
import net.hw.student.bean.User;
import net.hw.student.dao.UserDao;
import net.hw.student.dbutil.ConnectionManager;
public class UserDaoImpl implements UserDao {
@Override
public int deleteById(int id) {
return 0;
}
@Override
public List<User> findAll() {
return null;
}
@Override
public User findById(int id) {
// 声明用户
User user = null;
// 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "SELECT * FROM t_user WHERE id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setInt(1, id);
// 执行SQL查询,返回结果集
ResultSet rs = pstmt.executeQuery();
// 判断结果集是否有记录
if (rs.next()) {
// 创建用户实体
user = new User();
// 设置实体属性
user.setId(rs.getInt("id"));
user.setUsernname(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setTelephone(rs.getString("telephone"));
user.setRegisterTime(rs.getTimestamp("register_time"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回用户
return user;
}
@Override
public int insert(User user) {
// 定义插入记录数
int count = 0;
// 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "INSERT INTO t_user (username, password, telephone, register_time)"
+ " VALUES (?, ?, ?, ?)";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, user.getUsernname());
pstmt.setString(2, user.getPassword());
pstmt.setString(3,user.getTelephone());
pstmt.setTimestamp(4, new Timestamp(user.getRegisterTime().getTime()));
// 执行更新操作,插入新记录
count = pstmt.executeUpdate();
// 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回插入记录数
return count;
}
@Override
public User login(String username, String password) {
// 声明用户
User user = null;
// 获取数据库连接
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "SELECT * FROM t_user WHERE username = ? AND password = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, username);
pstmt.setString(2, password);
// 执行查询,返回结果集
ResultSet rs = pstmt.executeQuery();
// 判断是否有记录
if (rs.next()) {
// 实例化用户
user = new User();
// 设置用户属性
user.setId(rs.getInt("id"));
user.setUsernname(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setTelephone(rs.getString("telephone"));
user.setRegisterTime(rs.getTimestamp("register_time"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回用户
return user;
}
@Override
public int update(User user) {
// 定义更新记录数
int count = 0;
// 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "UPDATE t_user SET username = ?, password = ?, telephone = ?,"
+ " register_time = ? WHERE id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, user.getUsernname());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getTelephone());
pstmt.setTimestamp(4, new Timestamp(user.getRegisterTime().getTime()));
pstmt.setInt(5, user.getId());
// 执行更新操作,更新记录
count = pstmt.executeUpdate();
// 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回更新记录数
return count;
}
}
11、服务接口
(1)学校服务接口CollegeService
/**
* 包名:net.hw.student.service
* 类名:CollegeService
* 描述:学校服务接口
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.service;
import net.hw.student.bean.College;
public interface CollegeService {
College findCollegeById(int id);
int updateCollege(College college);
}
(2)状态服务接口StatusService
/**
* 包名:net.hw.student.service
* 类名:StatusService
* 描述:状态服务接口
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.service;
import net.hw.student.bean.Status;
public interface StatusService {
Status findStatusById(int id);
int updateStatus(Status status);
}
(3)学生服务接口StudentService
/**
* 包名:net.hw.student.service
* 类名:StudentService
* 描述:学生服务接口
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.service;
import java.util.List;
import java.util.Vector;
import net.hw.student.bean.Student;
public interface StudentService {
int addStudent(Student student);
int deleteStudentById(String id);
int deleteStudentsByClass(String clazz);
int deleteStudentsByDepartment(String department);
int updateStudent(Student student);
Student findStudentById(String id);
List<Student> findStudentsByName(String name);
List<Student> findStudentsByClass(String clazz);
List<Student> findStudentsByDepartment(String department);
List<Student> findAllStudents();
Vector findRowsBySex();
Vector findRowsByClass();
Vector findRowsByDepartment();
}
(4)用户服务接口UserService
/**
* 包名:net.hw.student.service
* 类名:UserService
* 描述:用户服务接口
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.service;
import java.util.List;
import net.hw.student.bean.User;
public interface UserService {
int addUser(User user);
int deleteUserById(int id);
int updateUser(User user);
User findUserById(int id);
List<User> findAllUsers();
User login(String username, String password);
}
12、服务接口实现类
(1)学校服务接口实现类CollegeServiceImpl
/**
* 包名:net.hw.student.service.impl
* 类名:CollegeServiceImpl
* 描述:学校服务接口实现类
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.service.impl;
import net.hw.student.bean.College;
import net.hw.student.dao.CollegeDao;
import net.hw.student.dao.impl.CollegeDaoImpl;
import net.hw.student.service.CollegeService;
public class CollegeServiceImpl implements CollegeService {
/**
* 声明学校数据访问对象
*/
private CollegeDao collegeDao = new CollegeDaoImpl();
@Override
public College findCollegeById(int id) {
return collegeDao.findById(id);
}
@Override
public int updateCollege(College college) {
return collegeDao.update(college);
}
}
(2)状态服务接口实现类StatusServiceImpl
/**
* 包名:net.hw.student.service.impl
* 类名:StatusServiceImpl
* 描述:状态服务接口实现类
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.service.impl;
import net.hw.student.bean.Status;
import net.hw.student.dao.StatusDao;
import net.hw.student.dao.impl.StatusDaoImpl;
import net.hw.student.service.StatusService;
public class StatusServiceImpl implements StatusService{
/**
* 声明状态数据访问对象
*/
private StatusDao statusDao = new StatusDaoImpl();
@Override
public Status findStatusById(int id) {
return statusDao.findById(id);
}
@Override
public int updateStatus(Status status) {
return statusDao.update(status);
}
}
(3)学生服务接口实现类StudentServiceImpl
/**
* 包名:net.hw.student.service.impl
* 类名:StudentServiceImpl
* 描述:学生服务接口实现类
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.service.impl;
import java.util.List;
import java.util.Vector;
import net.hw.student.bean.Student;
import net.hw.student.dao.StudentDao;
import net.hw.student.dao.impl.StudentDaoImpl;
import net.hw.student.service.StudentService;
public class StudentServiceImpl implements StudentService {
/**
* 声明学生数据访问对象
*/
private StudentDao studentDao = new StudentDaoImpl();
@Override
public int addStudent(Student student) {
return studentDao.insert(student);
}
@Override
public int deleteStudentById(String id) {
return studentDao.deleteById(id);
}
@Override
public int deleteStudentsByClass(String clazz) {
return studentDao.deleteByClass(clazz);
}
@Override
public int deleteStudentsByDepartment(String department) {
return studentDao.deleteByDepartment(department);
}
@Override
public List<Student> findAllStudents() {
return studentDao.findAll();
}
@Override
public Vector findRowsByClass() {
return studentDao.findRowsByClass();
}
@Override
public Vector findRowsByDepartment() {
return studentDao.findRowsByDepartment();
}
@Override
public Vector findRowsBySex() {
return studentDao.findRowsBySex();
}
@Override
public Student findStudentById(String id) {
return studentDao.findById(id);
}
@Override
public List<Student> findStudentsByClass(String clazz) {
return studentDao.findByClass(clazz);
}
@Override
public List<Student> findStudentsByDepartment(String department) {
return studentDao.findByDepartment(department);
}
@Override
public List<Student> findStudentsByName(String name) {
return studentDao.findByName(name);
}
@Override
public int updateStudent(Student student) {
return studentDao.update(student);
}
}
(4)用户服务接口实现类UserServiceImpl
/**
* 包名:net.hw.student.service.impl
* 类名:UserServiceImpl
* 描述:用户服务接口实现类
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.service.impl;
import java.util.List;
import net.hw.student.bean.User;
import net.hw.student.dao.UserDao;
import net.hw.student.dao.impl.UserDaoImpl;
import net.hw.student.service.UserService;
public class UserServiceImpl implements UserService{
/**
* 声明用户数据访问对象
*/
private UserDao userDao = new UserDaoImpl();
@Override
public int addUser(User user) {
return userDao.insert(user);
}
@Override
public int deleteUserById(int id) {
return userDao.deleteById(id);
}
@Override
public List<User> findAllUsers() {
return userDao.findAll();
}
@Override
public User findUserById(int id) {
return userDao.findById(id);
}
@Override
public User login(String username, String password) {
return userDao.login(username, password);
}
@Override
public int updateUser(User user) {
return userDao.update(user);
}
}
13、应用程序类
/**
* 包名:net.hw.student.app
* 类名:Application
* 描述:存放全局数据
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.app;
import net.hw.student.gui.LoginFrame;
import net.hw.student.gui.MainFrame;
import net.hw.student.gui.RegisterFrame;
public class Application {
/**
* 登录用户标识符
*/
public static int id;
/**
* 登录用户名
*/
public static String username;
/**
* 登录窗口
*/
public static LoginFrame loginFrame;
/**
* 主窗口
*/
public static MainFrame mainFrame;
/**
* 注册窗口
*/
public static RegisterFrame registerFrame;
}
14、界面类(Frame)
(1)添加学生窗口AddStudentFrame
/**
* 包名:net.hw.student.gui
* 类名:AddStudentFrame
* 描述:添加学生记录
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import net.hw.student.bean.Student;
import net.hw.student.service.StudentService;
import net.hw.student.service.impl.StudentServiceImpl;
public class AddStudentFrame extends JFrame {
/**
* 声明面板
*/
private JPanel panel;
private JPanel pnlCenter;
private JPanel pnlRow1;
private JPanel pnlRow2;
private JPanel pnlRow3;
private JPanel pnlRow4;
private JPanel pnlRow5;
private JPanel pnlRow6;
private JPanel pnlRow7;
private JPanel pnlSouth;
/**
* 声明标签
*/
private JLabel lblId;
private JLabel lblName;
private JLabel lblSex;
private JLabel lblAge;
private JLabel lblDepartment;
private JLabel lblClass;
private JLabel lblTelephone;
/**
* 声明文本框
*/
private JTextField txtId;
private JTextField txtName;
private JTextField txtSex;
private JTextField txtAge;
private JTextField txtDepartment;
private JTextField txtClass;
private JTextField txtTelephone;
/**
* 声明按钮
*/
private JButton btnExit;
private JButton btnOK;
private JButton btnCancel;
/**
* 当前记录行号
*/
private int currentRow;
/**
* 学生列表
*/
private List<Student> students;
/**
* 构造方法
*
* @param title
*/
public AddStudentFrame(String title) {
super(title);
initGUI();
}
/**
* 初始化用户界面
*/
private void initGUI() {
// 创建组件
panel = (JPanel) getContentPane();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
pnlRow1 = new JPanel();
pnlRow2 = new JPanel();
pnlRow3 = new JPanel();
pnlRow4 = new JPanel();
pnlRow5 = new JPanel();
pnlRow6 = new JPanel();
pnlRow7 = new JPanel();
pnlRow1.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow2.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow3.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow4.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow5.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow6.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow7.setLayout(new FlowLayout(FlowLayout.LEFT));
lblId = new JLabel("学号:");
lblName = new JLabel("姓名:");
lblSex = new JLabel("性别:");
lblAge = new JLabel("年龄:");
lblDepartment = new JLabel("系部:");
lblClass = new JLabel("班级:");
lblTelephone = new JLabel("电话:");
txtId = new JTextField(20);
txtName = new JTextField(20);
txtSex = new JTextField(20);
txtAge = new JTextField(20);
txtDepartment = new JTextField(20);
txtClass = new JTextField(20);
txtTelephone = new JTextField(20);
btnOK = new JButton("确定[O]");
btnCancel = new JButton("取消[C]");
btnExit = new JButton("退出[X]");
btnOK.setMnemonic(KeyEvent.VK_O);
btnCancel.setMnemonic(KeyEvent.VK_C);
btnExit.setMnemonic(KeyEvent.VK_X);
// 添加组件
panel.setLayout(new BorderLayout());
panel.add(pnlCenter, BorderLayout.CENTER);
panel.add(pnlSouth, BorderLayout.SOUTH);
pnlCenter.setLayout(new GridLayout(7, 1));
pnlCenter.add(pnlRow1);
pnlCenter.add(pnlRow2);
pnlCenter.add(pnlRow3);
pnlCenter.add(pnlRow4);
pnlCenter.add(pnlRow5);
pnlCenter.add(pnlRow6);
pnlCenter.add(pnlRow7);
pnlRow1.add(lblId);
pnlRow1.add(txtId);
pnlRow2.add(lblName);
pnlRow2.add(txtName);
pnlRow3.add(lblSex);
pnlRow3.add(txtSex);
pnlRow4.add(lblAge);
pnlRow4.add(txtAge);
pnlRow5.add(lblDepartment);
pnlRow5.add(txtDepartment);
pnlRow6.add(lblClass);
pnlRow6.add(txtClass);
pnlRow7.add(lblTelephone);
pnlRow7.add(txtTelephone);
pnlSouth.add(btnOK);
pnlSouth.add(btnCancel);
pnlSouth.add(btnExit);
// 设置窗口属性
setResizable(false);
pack();
setLocationRelativeTo(null);
setVisible(true);
// 【确定】按钮单击事件
btnOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 创建学生对象
Student student = new Student();
student.setId(txtId.getText().trim());
student.setName(txtName.getText().trim());
student.setSex(txtSex.getText().trim());
student.setAge(Integer.parseInt(txtAge.getText()));
student.setDepartment(txtDepartment.getText().trim());
student.setClazz(txtClass.getText().trim());
student.setTelephone(txtTelephone.getText().trim());
// 创建学生服务对象
StudentService studentService = new StudentServiceImpl();
// 添加学生记录
int count = studentService.addStudent(student);
// 判断是否添加成功
if (count > 0) {
JOptionPane.showMessageDialog(null, "添加记录成功!", "增加学生记录", JOptionPane.INFORMATION_MESSAGE);
txtId.setText("");
txtName.setText("");
txtSex.setText("");
txtAge.setText("");
txtDepartment.setText("");
txtClass.setText("");
txtTelephone.setText("");
txtId.requestFocus();
} else {
JOptionPane.showMessageDialog(null, "添加记录失败!", "增加学生记录", JOptionPane.ERROR_MESSAGE);
}
}
});
txtId.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
if (txtId.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, "学号不能为空!", "增加学生记录", JOptionPane.WARNING_MESSAGE);
txtId.requestFocus();
}
}
@Override
public void focusGained(FocusEvent e) {
}
});
txtAge.addFocusListener(new FocusListener() {
@Override
public void focusLost(FocusEvent e) {
if (!isNumber(txtAge.getText().trim())) {
JOptionPane.showMessageDialog(null, "注意:年龄全由数字构成!", "增加学生记录", JOptionPane.WARNING_MESSAGE);
txtAge.setText("");
txtAge.requestFocus();
}
}
@Override
public void focusGained(FocusEvent e) {
}
});
// 【取消】按钮单击事件
btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
txtId.setText("");
txtName.setText("");
txtSex.setText("");
txtAge.setText("");
txtDepartment.setText("");
txtClass.setText("");
txtTelephone.setText("");
txtId.requestFocus();
}
});
// 【退出】按钮单击事件
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
// 文本框按键事件
txtId.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
if (!txtId.getText().trim().equals("")) {
txtName.requestFocus();
}
}
}
});
txtName.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
txtSex.requestFocus();
}
}
});
txtSex.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
txtAge.requestFocus();
}
}
});
txtAge.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
txtDepartment.requestFocus();
}
}
});
txtDepartment.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
txtClass.requestFocus();
}
}
});
txtClass.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
txtTelephone.requestFocus();
}
}
});
}
/**
* 判断一个字符串是否全是数字
*
* @param str
* @return
*/
boolean isNumber(String str) {
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) < '0' || str.charAt(i) > '9') {
return false;
}
}
return true;
}
}
(2)浏览学生窗口BrowseStudentsFrame
/**
* 包名:net.hw.student.gui
* 类名:BrowseStudentsFrame
* 描述:浏览学生记录
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import net.hw.student.bean.Student;
import net.hw.student.service.StudentService;
import net.hw.student.service.impl.StudentServiceImpl;
public class BrowseStudentsFrame extends JFrame {
/**
* 声明面板
*/
private JPanel panel;
private JPanel pnlCenter;
private JPanel pnlRow1;
private JPanel pnlRow2;
private JPanel pnlRow3;
private JPanel pnlRow4;
private JPanel pnlRow5;
private JPanel pnlRow6;
private JPanel pnlRow7;
private JPanel pnlSouth;
/**
* 声明标签
*/
private JLabel lblId;
private JLabel lblName;
private JLabel lblSex;
private JLabel lblAge;
private JLabel lblDepartment;
private JLabel lblClass;
private JLabel lblTelephone;
/**
* 声明文本框
*/
private JTextField txtId;
private JTextField txtName;
private JTextField txtSex;
private JTextField txtAge;
private JTextField txtDepartment;
private JTextField txtClass;
private JTextField txtTelephone;
/**
* 声明按钮
*/
private JButton btnTop;
private JButton btnPrevious;
private JButton btnNext;
private JButton btnBottom;
private JButton btnExit;
/**
* 当前记录行号
*/
private int currentRow;
/**
* 学生列表
*/
private List<Student> students;
/**
* 创建学生服务对象
*/
StudentService studentService;
/**
* 构造方法
*
* @param title
*/
public BrowseStudentsFrame(String title) {
super(title);
initGUI();
}
/**
* 初始化用户界面
*/
private void initGUI() {
// 创建组件
panel = (JPanel) getContentPane();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
pnlRow1 = new JPanel();
pnlRow2 = new JPanel();
pnlRow3 = new JPanel();
pnlRow4 = new JPanel();
pnlRow5 = new JPanel();
pnlRow6 = new JPanel();
pnlRow7 = new JPanel();
pnlRow1.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow2.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow3.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow4.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow5.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow6.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow7.setLayout(new FlowLayout(FlowLayout.LEFT));
lblId = new JLabel("学号:");
lblName = new JLabel("姓名:");
lblSex = new JLabel("性别:");
lblAge = new JLabel("年龄:");
lblDepartment = new JLabel("系部:");
lblClass = new JLabel("班级:");
lblTelephone = new JLabel("电话:");
txtId = new JTextField(40);
txtName = new JTextField(40);
txtSex = new JTextField(40);
txtAge = new JTextField(40);
txtDepartment = new JTextField(40);
txtClass = new JTextField(40);
txtTelephone = new JTextField(40);
txtId.setEditable(false);
txtName.setEditable(false);
txtSex.setEditable(false);
txtAge.setEditable(false);
txtDepartment.setEditable(false);
txtClass.setEditable(false);
txtTelephone.setEditable(false);
btnTop = new JButton("第一条[T]");
btnPrevious = new JButton("上一条[P]");
btnNext = new JButton("下一条[N]");
btnBottom = new JButton("最后一条[B]");
btnExit = new JButton("退出[X]");
btnTop.setMnemonic(KeyEvent.VK_T);
btnPrevious.setMnemonic(KeyEvent.VK_P);
btnNext.setMnemonic(KeyEvent.VK_N);
btnBottom.setMnemonic(KeyEvent.VK_B);
btnExit.setMnemonic(KeyEvent.VK_X);
// 添加组件
panel.setLayout(new BorderLayout());
panel.add(pnlCenter, BorderLayout.CENTER);
panel.add(pnlSouth, BorderLayout.SOUTH);
pnlCenter.setLayout(new GridLayout(7, 1));
pnlCenter.add(pnlRow1);
pnlCenter.add(pnlRow2);
pnlCenter.add(pnlRow3);
pnlCenter.add(pnlRow4);
pnlCenter.add(pnlRow5);
pnlCenter.add(pnlRow6);
pnlCenter.add(pnlRow7);
pnlRow1.add(lblId);
pnlRow1.add(txtId);
pnlRow2.add(lblName);
pnlRow2.add(txtName);
pnlRow3.add(lblSex);
pnlRow3.add(txtSex);
pnlRow4.add(lblAge);
pnlRow4.add(txtAge);
pnlRow5.add(lblDepartment);
pnlRow5.add(txtDepartment);
pnlRow6.add(lblClass);
pnlRow6.add(txtClass);
pnlRow7.add(lblTelephone);
pnlRow7.add(txtTelephone);
pnlSouth.add(btnTop);
pnlSouth.add(btnPrevious);
pnlSouth.add(btnNext);
pnlSouth.add(btnBottom);
pnlSouth.add(btnExit);
// 设置窗口属性
setSize(500, 300);
setResizable(false);
pack();
setLocationRelativeTo(null);
setVisible(true);
// 创建学生服务对象
studentService = new StudentServiceImpl();
// 获取全部学生列表
students = studentService.findAllStudents();
// 判断是否有学生记录
if (students.size() > 0) {
// 设置当前记录号
currentRow = 1;
// 设置窗口标题
setTitle("浏览学生表记录" + " && 当前记录:" + currentRow);
// 填充窗口各文本框数据
fillFrameData(currentRow);
} else {
JOptionPane.showMessageDialog(null, "表中没有记录!", "浏览学生表记录", JOptionPane.ERROR_MESSAGE);
btnTop.setEnabled(false);
btnPrevious.setEnabled(false);
btnNext.setEnabled(false);
btnBottom.setEnabled(false);
}
// 【第一条】按钮单击事件
btnTop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 设置当前记录号
currentRow = 1;
// 填充当前记录数据
fillFrameData(currentRow);
}
});
// 【上一条】按钮单击事件
btnPrevious.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (currentRow > 1) {
// 设置当前记录号
currentRow--;
// 填充当前记录数据
fillFrameData(currentRow);
} else {
JOptionPane.showMessageDialog(null, "已到第一条记录!", "浏览学生表记录", JOptionPane.WARNING_MESSAGE);
}
}
});
// 【下一条】按钮单击事件
btnNext.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (currentRow < students.size() - 1) {
// 设置当前记录号
currentRow++;
// 填充当前记录数据
fillFrameData(currentRow);
} else {
JOptionPane.showMessageDialog(null, "已到最后一条记录!", "浏览学生表记录", JOptionPane.WARNING_MESSAGE);
}
}
});
// 【最后一条】按钮单击事件
btnBottom.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 设置当前记录号
currentRow = students.size() - 1;
// 填充当前记录数据
fillFrameData(currentRow);
}
});
// 【退出】按钮单击事件
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
}
/**
* 将当前记录数据填充窗口各文本框
*
* @param currentRow
*/
private void fillFrameData(int currentRow) {
if (currentRow > 0) {
setTitle("浏览学生表记录" + " && 当前记录:" + currentRow);
txtId.setText(students.get(currentRow).getId());
txtName.setText(students.get(currentRow).getName());
txtSex.setText(students.get(currentRow).getSex());
txtAge.setText(students.get(currentRow).getAge() + "");
txtDepartment.setText(students.get(currentRow).getDepartment());
txtClass.setText(students.get(currentRow).getClazz());
txtTelephone.setText(students.get(currentRow).getTelephone());
}
}
}
(3)修改密码窗口ChangePasswordFrame
/**
* 包名:net.hw.student.gui
* 类名:ChangePasswordFrame
* 描述:修改密码
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.gui;
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.WindowAdapter;
import java.awt.event.WindowEvent;
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 net.hw.student.app.Application;
import net.hw.student.bean.User;
import net.hw.student.service.UserService;
import net.hw.student.service.impl.UserServiceImpl;
public class ChangePasswordFrame extends JFrame {
/**
* 面板
*/
private JPanel panel;
private JPanel pnlRow1;
private JPanel pnlRow2;
private JPanel pnlRow3;
private JPanel pnlRow4;
private JPanel pnlRow5;
/**
* 标签
*/
private JLabel lblUsername;
private JLabel lblPassword;
private JLabel lblNewPassword1;
private JLabel lblNewPassword2;
/**
* 文本框
*/
private JTextField txtUsername;
private JPasswordField txtPassword;
private JPasswordField txtNewPassword1;
private JPasswordField txtNewPassword2;
/**
* 按钮
*/
private JButton btnOK;
private JButton btnCancel;
/**
* 学生管理应用程序
*/
private static Application app;
/**
* 创建用户服务对象
*/
private UserService userService;
public ChangePasswordFrame(String title) {
super(title);
// 初始化用户界面
initGUI();
}
/**
* 初始化用户界面
*/
private void initGUI() {
// 创建对象
panel = (JPanel) getContentPane();
pnlRow1 = new JPanel();
pnlRow2 = new JPanel();
pnlRow3 = new JPanel();
pnlRow4 = new JPanel();
pnlRow5 = new JPanel();
lblUsername = new JLabel("用户名:");
lblPassword = new JLabel("旧密码:");
lblNewPassword1 = new JLabel("新密码:");
lblNewPassword2 = new JLabel("确 认:");
txtUsername = new JTextField(20);
txtUsername.setEditable(false);
txtPassword = new JPasswordField(20);
txtNewPassword1 = new JPasswordField(20);
txtNewPassword2 = new JPasswordField(20);
btnOK = new JButton("确定[O]");
btnOK.setMnemonic(KeyEvent.VK_O);
btnCancel = new JButton("取消[C]");
btnCancel.setMnemonic(KeyEvent.VK_C);
// 添加组件
panel.setLayout(new GridLayout(5, 1));
panel.add(pnlRow1);
panel.add(pnlRow2);
panel.add(pnlRow3);
panel.add(pnlRow4);
panel.add(pnlRow5);
pnlRow1.add(lblUsername);
pnlRow1.add(txtUsername);
pnlRow2.add(lblPassword);
pnlRow2.add(txtPassword);
pnlRow3.add(lblNewPassword1);
pnlRow3.add(txtNewPassword1);
pnlRow4.add(lblNewPassword2);
pnlRow4.add(txtNewPassword2);
pnlRow5.add(btnOK);
pnlRow5.add(btnCancel);
// 设置窗口属性
pack();
setVisible(true);
setResizable(false);
pack();
setLocationRelativeTo(null);
// 设置控件属性
txtUsername.setEditable(false);
txtUsername.setText(Application.username);
// 注册监听器,实现监听器接口,编写事件处理代码
// 取消按钮单击事件
btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
// 确定按钮单击事件
btnOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
changePassword();
}
});
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
dispose();
}
});
txtUsername.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
txtPassword.requestFocus();
}
}
});
txtPassword.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
txtNewPassword1.requestFocus();
}
}
});
txtNewPassword1.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
txtNewPassword2.requestFocus();
}
}
});
txtNewPassword2.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
btnOK.requestFocus();
}
}
});
btnOK.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
changePassword();
}
}
});
}
/**
* 修改密码的方法
*/
private void changePassword() {
int id = Application.id;
String username = Application.username;
String password = new String(txtPassword.getPassword());
String newPassword1 = new String(txtNewPassword1.getPassword());
String newPassword2 = new String(txtNewPassword2.getPassword());
// 创建学生服务对象
userService = new UserServiceImpl();
if (userService.login(username, password) == null) {
JOptionPane.showMessageDialog(null, "旧密码错误,请重新输入!", "错误提示", JOptionPane.ERROR_MESSAGE);
txtPassword.requestFocus();
txtPassword.selectAll();
} else if (newPassword1.equals("")) {
JOptionPane.showMessageDialog(null, "新密码不能为空!", "错误提示", JOptionPane.ERROR_MESSAGE);
txtNewPassword1.requestFocus();
} else if (newPassword2.equals("")) {
JOptionPane.showMessageDialog(null, "确认密码不能为空!", "错误提示", JOptionPane.ERROR_MESSAGE);
txtNewPassword2.requestFocus();
} else if (!newPassword1.equals(newPassword2)) {
JOptionPane.showMessageDialog(null, "两次密码不一致,请重新输入!", "错误提示", JOptionPane.ERROR_MESSAGE);
txtNewPassword1.setText("");
txtNewPassword2.setText("");
txtNewPassword1.requestFocus();
} else {
// 按标识符获取用户
User user = userService.findUserById(id);
System.out.println(user);
// 修改密码
user.setPassword(newPassword1);
// 更新用户信息
int count = userService.updateUser(user);
if (count > 0) {
JOptionPane.showMessageDialog(null, "密码修改成功!", "设置密码", JOptionPane.INFORMATION_MESSAGE);
dispose();
} else {
JOptionPane.showMessageDialog(null, "密码修改失败!", "设置密码", JOptionPane.WARNING_MESSAGE);
}
}
}
}
(4)按班级统计学生人数窗口CountStudentsByClassFrame
/**
* 包名:net.hw.student.gui
* 类名:CountStudentsByClassFrame
* 描述:按班级统计学生人数
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.print.PrinterException;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
import net.hw.student.service.StudentService;
import net.hw.student.service.impl.StudentServiceImpl;
public class CountStudentsByClassFrame extends JFrame {
/**
* 面板
*/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
/**
* 按钮
*/
private JButton btnPrint;
private JButton btnExit;
/**
* 记录行集
*/
private Vector rows;
/**
* 表格列标题
*/
private Vector<String> colHead;
/**
* 表格
*/
private JTable table;
/**
* 滚动面板
*/
private JScrollPane scroller;
/**
* 创建学生服务对象
*/
private StudentService studentService;
public CountStudentsByClassFrame(String title) {
super(title);
initGUI();
}
/**
* 初始化用户界面
*/
private void initGUI() {
// 创建对象
panel = (JPanel) getContentPane();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
rows = new Vector();
colHead = new Vector();
btnPrint = new JButton("打印[P]");
btnPrint.setMnemonic(KeyEvent.VK_P);
btnExit = new JButton("退出[X]");
btnExit.setMnemonic(KeyEvent.VK_X);
// 添加组件
panel.add(pnlSouth, BorderLayout.SOUTH);
panel.add(pnlCenter, BorderLayout.CENTER);
pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));
pnlSouth.add(btnPrint);
pnlSouth.add(btnExit);
pnlCenter.setLayout(new BorderLayout());
TitledBorder tb = new TitledBorder("统计结果");
pnlCenter.setBorder(tb);
// 创建学生服务对象
studentService = new StudentServiceImpl();
// 获取按性别统计结果记录行集
rows = studentService.findRowsByClass();
// 设置表头
colHead.add("班级");
colHead.add("人数");
// 创建表格(参数1:记录集;参数2:表头)
table = new JTable(rows, colHead);
scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
pnlCenter.add(scroller, BorderLayout.CENTER);
repaint(); // 重绘窗体
if (rows.isEmpty()) {
JOptionPane.showMessageDialog(this, "没有记录!", "错误提示", JOptionPane.WARNING_MESSAGE);
}
// 设置窗口属性
setSize(300, 200);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
// 【退出】按钮单击事件
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
dispose();
}
});
// 【打印】按钮单击事件
btnPrint.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
table.print();
} catch (PrinterException e1) {
e1.printStackTrace();
}
}
});
}
}
(5)按系部统计学生人数窗口CountStudentsByDepartmentFrame
/**
* 包名:net.hw.student.gui
* 类名:CountStudentsByDepartmentFrame
* 描述:按系部统计学生人数
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.print.PrinterException;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
import net.hw.student.service.StudentService;
import net.hw.student.service.impl.StudentServiceImpl;
public class CountStudentsByDepartmentFrame extends JFrame {
/**
* 面板
*/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
/**
* 按钮
*/
private JButton btnPrint;
private JButton btnExit;
/**
* 记录行集
*/
private Vector rows;
/**
* 表格列标题
*/
private Vector<String> colHead;
/**
* 表格
*/
private JTable table;
/**
* 滚动面板
*/
private JScrollPane scroller;
/**
* 创建学生服务对象
*/
private StudentService studentService;
public CountStudentsByDepartmentFrame(String title) {
super(title);
initGUI();
}
/**
* 初始化用户界面
*/
private void initGUI() {
// 创建对象
panel = (JPanel) getContentPane();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
rows = new Vector();
colHead = new Vector();
btnPrint = new JButton("打印[P]");
btnPrint.setMnemonic(KeyEvent.VK_P);
btnExit = new JButton("退出[X]");
btnExit.setMnemonic(KeyEvent.VK_X);
// 添加组件
panel.add(pnlSouth, BorderLayout.SOUTH);
panel.add(pnlCenter, BorderLayout.CENTER);
pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));
pnlSouth.add(btnPrint);
pnlSouth.add(btnExit);
pnlCenter.setLayout(new BorderLayout());
TitledBorder tb = new TitledBorder("统计结果");
pnlCenter.setBorder(tb);
// 创建学生服务对象
studentService = new StudentServiceImpl();
// 获取按性别统计结果记录行集
rows = studentService.findRowsByDepartment();
// 设置表头
colHead.add("系部");
colHead.add("人数");
// 创建表格(参数1:记录集;参数2:表头)
table = new JTable(rows, colHead);
scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
pnlCenter.add(scroller, BorderLayout.CENTER);
repaint(); // 重绘窗体
if (rows.isEmpty()) {
JOptionPane.showMessageDialog(this, "没有记录!", "错误提示", JOptionPane.WARNING_MESSAGE);
}
// 设置窗口属性
setSize(300, 200);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
// 【退出】按钮单击事件
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
dispose();
}
});
// 【打印】按钮单击事件
btnPrint.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
table.print();
} catch (PrinterException e1) {
e1.printStackTrace();
}
}
});
}
}
(6)按性别统计学生人数窗口CountStudentsBySexFrame
/**
* 包名:net.hw.student.gui
* 类名:CountStudentsBySexFrame
* 描述:按性别统计学生人数
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.print.PrinterException;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
import net.hw.student.service.StudentService;
import net.hw.student.service.impl.StudentServiceImpl;
public class CountStudentsBySexFrame extends JFrame {
/**
* 面板
*/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
/**
* 按钮
*/
private JButton btnPrint;
private JButton btnExit;
/**
* 记录行集
*/
private Vector rows;
/**
* 表格列标题
*/
private Vector<String> colHead;
/**
* 表格
*/
private JTable table;
/**
* 滚动面板
*/
private JScrollPane scroller;
/**
* 创建学生服务对象
*/
private StudentService studentService;
public CountStudentsBySexFrame(String title) {
super(title);
initGUI();
}
/**
* 初始化用户界面
*/
private void initGUI() {
// 创建对象
panel = (JPanel) getContentPane();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
rows = new Vector();
colHead = new Vector();
btnPrint = new JButton("打印[P]");
btnPrint.setMnemonic(KeyEvent.VK_P);
btnExit = new JButton("退出[X]");
btnExit.setMnemonic(KeyEvent.VK_X);
// 添加组件
panel.add(pnlSouth, BorderLayout.SOUTH);
panel.add(pnlCenter, BorderLayout.CENTER);
pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));
pnlSouth.add(btnPrint);
pnlSouth.add(btnExit);
pnlCenter.setLayout(new BorderLayout());
TitledBorder tb = new TitledBorder("统计结果");
pnlCenter.setBorder(tb);
// 创建学生服务对象
studentService = new StudentServiceImpl();
// 获取按性别统计结果记录行集
rows = studentService.findRowsBySex();
// 设置表头
colHead.add("性别");
colHead.add("人数");
// 创建表格(参数1:记录集;参数2:表头)
table = new JTable(rows, colHead);
scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
pnlCenter.add(scroller, BorderLayout.CENTER);
repaint(); // 重绘窗体
if (rows.isEmpty()) {
JOptionPane.showMessageDialog(this, "没有记录!", "错误提示", JOptionPane.WARNING_MESSAGE);
}
// 设置窗口属性
setSize(300, 200);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
// 【退出】按钮单击事件
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
dispose();
}
});
// 【打印】按钮单击事件
btnPrint.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
table.print();
} catch (PrinterException e1) {
e1.printStackTrace();
}
}
});
}
}
(7)按学号删除学生窗口DeleteStudentByIdFrame
/**
* 包名:net.hw.student.gui
* 类名:DeleteStudentByIdFrame
* 描述:按学号删除学生
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import net.hw.student.bean.Student;
import net.hw.student.service.StudentService;
import net.hw.student.service.impl.StudentServiceImpl;
public class DeleteStudentByIdFrame extends JFrame {
/**
* 学号标签
*/
private JLabel lblInputId;
/**
* 学号文本框
*/
private JTextField txtId;
/**
* 面板
*/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
private JPanel pnlNorth;
/**
* 按钮
*/
private JButton btnQuery;
private JButton btnBrowseAll;
private JButton btnDelete;
private JButton btnExit;
/**
* 记录行集
*/
private Vector rows;
/**
* 表格列标题
*/
private Vector<String> colHead;
/**
* 表格
*/
private JTable table;
/**
* 滚动面板
*/
private JScrollPane scroller;
/**
* 当前记录行号
*/
private int currentRow;
/**
* 学生列表
*/
private List<Student> students;
/**
* 创建学生服务对象
*/
private StudentService studentService;
/**
* 构造方法
*
* @param title
*/
public DeleteStudentByIdFrame(String title) {
super(title);
intiGUI();
}
private void intiGUI() {
// 创建对象
panel = (JPanel) getContentPane();
pnlNorth = new JPanel();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
rows = new Vector();
colHead = new Vector();
lblInputId = new JLabel("输入学号:");
txtId = new JTextField(10);
txtId.setHorizontalAlignment(JTextField.CENTER);
btnQuery = new JButton("查询[Q]");
btnQuery.setMnemonic(KeyEvent.VK_Q);
btnBrowseAll = new JButton("显示全部记录[A]");
btnBrowseAll.setMnemonic(KeyEvent.VK_A);
btnDelete = new JButton("删除查询的记录[D]");
btnDelete.setEnabled(false);// 删除按钮不可用
btnDelete.setMnemonic(KeyEvent.VK_D);
btnExit = new JButton("退出[X]");
btnExit.setMnemonic(KeyEvent.VK_X);
// 添加组件
panel.add(pnlSouth, BorderLayout.SOUTH);
panel.add(pnlCenter, BorderLayout.CENTER);
panel.add(pnlNorth, BorderLayout.NORTH);
pnlNorth.add(lblInputId);
pnlNorth.add(txtId);
pnlNorth.add(btnQuery);
pnlNorth.add(btnBrowseAll);
pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));
pnlSouth.add(btnDelete);
pnlSouth.add(btnExit);
pnlCenter.setLayout(new BorderLayout());
// 创建标题边框对象
TitledBorder tb = new TitledBorder("查询结果");
pnlCenter.setBorder(tb);
// 创建学生服务对象
studentService = new StudentServiceImpl();
// 获取全部学生列表
students = studentService.findAllStudents();
// 填充表格数据
fillTableData();
// 设置窗口属性
setSize(600, 400);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
// 【退出】按钮单击事件
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
dispose();
}
});
// 【删除】按钮单击事件
btnDelete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!rows.isEmpty()) {
long choice = JOptionPane.showConfirmDialog(null, "是否要删除记录?");
if (choice == JOptionPane.OK_OPTION) {
// 获取待删学生学号
String id = txtId.getText().trim();
// 按学号删除学生
int count = studentService.deleteStudentById(id);
if (count > 0) {
JOptionPane.showMessageDialog(null, "记录删除成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
// 重新获取全部学生列表
students = studentService.findAllStudents();
// 清空待删学生学号文本框
txtId.setText("");
// 填充数据
fillTableData();
// 删除按钮不可用
btnDelete.setEnabled(false);
} else {
JOptionPane.showMessageDialog(null, "记录删除失败!", "警告", JOptionPane.WARNING_MESSAGE);
}
}
}
}
});
// 【查询】按钮单击事件
btnQuery.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
doQuery();
}
});
// 【显示全部记录】按钮单击事件
btnBrowseAll.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
// 获取全部学生记录
students = studentService.findAllStudents();
// 填充表格数据
fillTableData();
// 删除按钮不可用
btnDelete.setEnabled(false);
}
});
// 文本框按键事件
txtId.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
doQuery();
}
}
});
// JTable单击事件
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// 获取当前行的行数
int row = table.rowAtPoint(e.getPoint());
// 选中鼠标单击的行
table.setRowSelectionInterval(row, row);
// 设置文本框内容
txtId.setText(table.getValueAt(row, 0).toString());
}
});
}
/**
* 查询方法
*/
private void doQuery() {
// 获取查询学号
String id = txtId.getText().trim();
if (!id.equals("")) {
students.clear();
Student student = studentService.findStudentById(id);
if (student != null) {
// 将查询到的学生添加到列表
students.add(student);
// 让删除按钮可用
btnDelete.setEnabled(true);
}
// 填充表格
fillTableData();
} else {
JOptionPane.showMessageDialog(this, "请输入待查学生学号!", "警告", JOptionPane.WARNING_MESSAGE);
txtId.requestFocus();
}
}
/**
* 填充表格方法
*/
private void fillTableData() {
// 填充表头
colHead.clear();
colHead.add("学号");
colHead.add("姓名");
colHead.add("性别");
colHead.add("年龄");
colHead.add("系部");
colHead.add("班级");
colHead.add("电话");
// 填充表记录
rows.clear();
for (Student student : students) {
Vector<String> currentRow = new Vector<String>();
currentRow.addElement(student.getId());
currentRow.addElement(student.getName());
currentRow.addElement(student.getSex());
currentRow.addElement(student.getAge() + "");
currentRow.addElement(student.getDepartment());
currentRow.addElement(student.getClazz());
currentRow.addElement(student.getTelephone());
// 将当前行添加到记录行集
rows.add(currentRow);
}
// 创建表格(参数1:记录集;参数2:表头)
table = new JTable(rows, colHead);
// 定义滚动面板
scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// 将滚动面板添加到中心面板
pnlCenter.add(scroller, BorderLayout.CENTER);
// 重绘窗体
repaint();
// 判断是否有记录行
if (rows.isEmpty()) {
JOptionPane.showMessageDialog(this, "没有符合条件的记录!", "错误提示", JOptionPane.WARNING_MESSAGE);
txtId.setText("");
} else {
// 让滚动条移到最上方
scroller.getVerticalScrollBar().setValue(0);
}
}
}
(8)按班级删除学生窗口DeleteStudentsByClassFrame
/**
* 包名:net.hw.student.gui
* 类名:DeleteStudentsByClassFrame
* 描述:按班级删除学生
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import net.hw.student.bean.Student;
import net.hw.student.service.StudentService;
import net.hw.student.service.impl.StudentServiceImpl;
public class DeleteStudentsByClassFrame extends JFrame {
/**
* 班级标签
*/
private JLabel lblInputClass;
/**
* 班级文本框
*/
private JTextField txtClass;
/**
* 面板
*/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
private JPanel pnlNorth;
/**
* 按钮
*/
private JButton btnQuery;
private JButton btnBrowseAll;
private JButton btnDelete;
private JButton btnExit;
/**
* 记录行集
*/
private Vector rows;
/**
* 表格列标题
*/
private Vector<String> colHead;
/**
* 表格
*/
private JTable table;
/**
* 滚动面板
*/
private JScrollPane scroller;
/**
* 当前记录行号
*/
private int currentRow;
/**
* 学生列表
*/
private List<Student> students;
/**
* 创建学生服务对象
*/
private StudentService studentService;
/**
* 构造方法
*
* @param title
*/
public DeleteStudentsByClassFrame(String title) {
super(title);
intiGUI();
}
private void intiGUI() {
// 创建对象
panel = (JPanel) getContentPane();
pnlNorth = new JPanel();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
rows = new Vector();
colHead = new Vector();
lblInputClass = new JLabel("输入班级:");
txtClass = new JTextField(10);
txtClass.setHorizontalAlignment(JTextField.CENTER);
btnQuery = new JButton("查询[Q]");
btnQuery.setMnemonic(KeyEvent.VK_Q);
btnBrowseAll = new JButton("显示全部记录[A]");
btnBrowseAll.setMnemonic(KeyEvent.VK_A);
btnDelete = new JButton("删除查询的记录[D]");
btnDelete.setEnabled(false);// 删除按钮不可用
btnDelete.setMnemonic(KeyEvent.VK_D);
btnExit = new JButton("退出[X]");
btnExit.setMnemonic(KeyEvent.VK_X);
// 添加组件
panel.add(pnlSouth, BorderLayout.SOUTH);
panel.add(pnlCenter, BorderLayout.CENTER);
panel.add(pnlNorth, BorderLayout.NORTH);
pnlNorth.add(lblInputClass);
pnlNorth.add(txtClass);
pnlNorth.add(btnQuery);
pnlNorth.add(btnBrowseAll);
pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));
pnlSouth.add(btnDelete);
pnlSouth.add(btnExit);
pnlCenter.setLayout(new BorderLayout());
// 创建标题边框对象
TitledBorder tb = new TitledBorder("查询结果");
pnlCenter.setBorder(tb);
// 创建学生服务对象
studentService = new StudentServiceImpl();
// 获取全部学生列表
students = studentService.findAllStudents();
// 填充表格数据
fillTableData();
// 设置窗口属性
setSize(600, 400);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
// 【退出】按钮单击事件
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
dispose();
}
});
// 【删除】按钮单击事件
btnDelete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!rows.isEmpty()) {
long choice = JOptionPane.showConfirmDialog(null, "是否要删除记录?");
if (choice == JOptionPane.OK_OPTION) {
// 获取待删班级
String clazz = txtClass.getText().trim();
// 按班级删除学生
int count = studentService.deleteStudentsByClass(clazz);
if (count > 0) {
JOptionPane.showMessageDialog(null, "记录删除成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
// 重新获取全部学生列表
students = studentService.findAllStudents();
// 清空待删班级文本框
txtClass.setText("");
// 填充数据
fillTableData();
// 删除按钮不可用
btnDelete.setEnabled(false);
} else {
JOptionPane.showMessageDialog(null, "记录删除失败!", "警告", JOptionPane.WARNING_MESSAGE);
}
}
}
}
});
// 【查询】按钮单击事件
btnQuery.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
doQuery();
}
});
// 【显示全部记录】按钮单击事件
btnBrowseAll.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
// 获取全部学生记录
students = studentService.findAllStudents();
// 填充表格数据
fillTableData();
// 删除按钮不可用
btnDelete.setEnabled(false);
}
});
// 文本框按键事件
txtClass.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
doQuery();
}
}
});
// JTable单击事件
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// 获取当前行的行数
int row = table.rowAtPoint(e.getPoint());
// 选中鼠标单击的行
table.setRowSelectionInterval(row, row);
// 设置文本框内容
txtClass.setText(table.getValueAt(row, 5).toString());
}
});
}
/**
* 查询方法
*/
private void doQuery() {
// 获取查询班级
String clazz = txtClass.getText().trim();
if (!clazz.equals("")) {
students = studentService.findStudentsByClass(clazz);
if (students.size() > 0) {
// 让删除按钮可用
btnDelete.setEnabled(true);
}
// 填充表格
fillTableData();
} else {
JOptionPane.showMessageDialog(this, "请输入待查班级!", "警告", JOptionPane.WARNING_MESSAGE);
txtClass.requestFocus();
}
}
/**
* 填充表格方法
*/
private void fillTableData() {
// 填充表头
colHead.clear();
colHead.add("学号");
colHead.add("姓名");
colHead.add("性别");
colHead.add("年龄");
colHead.add("系部");
colHead.add("班级");
colHead.add("电话");
// 填充表记录
rows.clear();
for (Student student : students) {
Vector<String> currentRow = new Vector<String>();
currentRow.addElement(student.getId());
currentRow.addElement(student.getName());
currentRow.addElement(student.getSex());
currentRow.addElement(student.getAge() + "");
currentRow.addElement(student.getDepartment());
currentRow.addElement(student.getClazz());
currentRow.addElement(student.getTelephone());
// 将当前行添加到记录行集
rows.add(currentRow);
}
// 创建表格(参数1:记录集;参数2:表头)
table = new JTable(rows, colHead);
// 定义滚动面板
scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// 将滚动面板添加到中心面板
pnlCenter.add(scroller, BorderLayout.CENTER);
// 重绘窗体
repaint();
// 判断是否有记录行
if (rows.isEmpty()) {
JOptionPane.showMessageDialog(this, "没有符合条件的记录!", "错误提示", JOptionPane.WARNING_MESSAGE);
txtClass.setText("");
} else {
// 让滚动条移到最上方
scroller.getVerticalScrollBar().setValue(0);
}
}
}
(9)按系部删除学生窗口DeleteStudentsByDepartmentFrame
/**
* 包名:net.hw.student.gui
* 类名:DeleteStudentsByDepartmentFrame
* 描述:按系部删除学生
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import net.hw.student.bean.Student;
import net.hw.student.service.StudentService;
import net.hw.student.service.impl.StudentServiceImpl;
public class DeleteStudentsByDepartmentFrame extends JFrame {
/**
* 系部标签
*/
private JLabel lblInputDepartment;
/**
* 系部文本框
*/
private JTextField txtDepartment;
/**
* 面板
*/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
private JPanel pnlNorth;
/**
* 按钮
*/
private JButton btnQuery;
private JButton btnBrowseAll;
private JButton btnDelete;
private JButton btnExit;
/**
* 记录行集
*/
private Vector rows;
/**
* 表格列标题
*/
private Vector<String> colHead;
/**
* 表格
*/
private JTable table;
/**
* 滚动面板
*/
private JScrollPane scroller;
/**
* 当前记录行号
*/
private int currentRow;
/**
* 学生列表
*/
private List<Student> students;
/**
* 创建学生服务对象
*/
private StudentService studentService;
/**
* 构造方法
*
* @param title
*/
public DeleteStudentsByDepartmentFrame(String title) {
super(title);
intiGUI();
}
private void intiGUI() {
// 创建对象
panel = (JPanel) getContentPane();
pnlNorth = new JPanel();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
rows = new Vector();
colHead = new Vector();
lblInputDepartment = new JLabel("输入系部:");
txtDepartment = new JTextField(10);
txtDepartment.setHorizontalAlignment(JTextField.CENTER);
btnQuery = new JButton("查询[Q]");
btnQuery.setMnemonic(KeyEvent.VK_Q);
btnBrowseAll = new JButton("显示全部记录[A]");
btnBrowseAll.setMnemonic(KeyEvent.VK_A);
btnDelete = new JButton("删除查询的记录[D]");
btnDelete.setEnabled(false);// 删除按钮不可用
btnDelete.setMnemonic(KeyEvent.VK_D);
btnExit = new JButton("退出[X]");
btnExit.setMnemonic(KeyEvent.VK_X);
// 添加组件
panel.add(pnlSouth, BorderLayout.SOUTH);
panel.add(pnlCenter, BorderLayout.CENTER);
panel.add(pnlNorth, BorderLayout.NORTH);
pnlNorth.add(lblInputDepartment);
pnlNorth.add(txtDepartment);
pnlNorth.add(btnQuery);
pnlNorth.add(btnBrowseAll);
pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));
pnlSouth.add(btnDelete);
pnlSouth.add(btnExit);
pnlCenter.setLayout(new BorderLayout());
// 创建标题边框对象
TitledBorder tb = new TitledBorder("查询结果");
pnlCenter.setBorder(tb);
// 创建学生服务对象
studentService = new StudentServiceImpl();
// 获取全部学生列表
students = studentService.findAllStudents();
// 填充表格数据
fillTableData();
// 设置窗口属性
setSize(600, 400);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
// 【退出】按钮单击事件
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
dispose();
}
});
// 【删除】按钮单击事件
btnDelete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!rows.isEmpty()) {
long choice = JOptionPane.showConfirmDialog(null, "是否要删除记录?");
if (choice == JOptionPane.OK_OPTION) {
// 获取待删系部
String department = txtDepartment.getText().trim();
// 按系部删除学生
int count = studentService.deleteStudentsByDepartment(department);
if (count > 0) {
JOptionPane.showMessageDialog(null, "记录删除成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
// 重新获取全部学生列表
students = studentService.findAllStudents();
// 清空待删班级文本框
txtDepartment.setText("");
// 填充数据
fillTableData();
// 删除按钮不可用
btnDelete.setEnabled(false);
} else {
JOptionPane.showMessageDialog(null, "记录删除失败!", "警告", JOptionPane.WARNING_MESSAGE);
}
}
}
}
});
// 【查询】按钮单击事件
btnQuery.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
doQuery();
}
});
// 【显示全部记录】按钮单击事件
btnBrowseAll.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
// 获取全部学生记录
students = studentService.findAllStudents();
// 填充表格数据
fillTableData();
// 删除按钮不可用
btnDelete.setEnabled(false);
}
});
// 文本框按键事件
txtDepartment.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
doQuery();
}
}
});
// JTable单击事件
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// 获取当前行的行数
int row = table.rowAtPoint(e.getPoint());
// 选中鼠标单击的行
table.setRowSelectionInterval(row, row);
// 设置文本框内容
txtDepartment.setText(table.getValueAt(row, 4).toString());
}
});
}
/**
* 查询方法
*/
private void doQuery() {
// 获取查询系部
String department = txtDepartment.getText().trim();
if (!department.equals("")) {
students = studentService.findStudentsByDepartment(department);
if (students.size() > 0) {
// 让删除按钮可用
btnDelete.setEnabled(true);
}
// 填充表格
fillTableData();
} else {
JOptionPane.showMessageDialog(this, "请输入待查系部!", "警告", JOptionPane.WARNING_MESSAGE);
txtDepartment.requestFocus();
}
}
/**
* 填充表格方法
*/
private void fillTableData() {
// 填充表头
colHead.clear();
colHead.add("学号");
colHead.add("姓名");
colHead.add("性别");
colHead.add("年龄");
colHead.add("系部");
colHead.add("班级");
colHead.add("电话");
// 填充表记录
rows.clear();
for (Student student : students) {
Vector<String> currentRow = new Vector<String>();
currentRow.addElement(student.getId());
currentRow.addElement(student.getName());
currentRow.addElement(student.getSex());
currentRow.addElement(student.getAge() + "");
currentRow.addElement(student.getDepartment());
currentRow.addElement(student.getClazz());
currentRow.addElement(student.getTelephone());
// 将当前行添加到记录行集
rows.add(currentRow);
}
// 创建表格(参数1:记录集;参数2:表头)
table = new JTable(rows, colHead);
// 定义滚动面板
scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// 将滚动面板添加到中心面板
pnlCenter.add(scroller, BorderLayout.CENTER);
// 重绘窗体
repaint();
// 判断是否有记录行
if (rows.isEmpty()) {
JOptionPane.showMessageDialog(this, "没有符合条件的记录!", "错误提示", JOptionPane.WARNING_MESSAGE);
txtDepartment.setText("");
} else {
// 让滚动条移到最上方
scroller.getVerticalScrollBar().setValue(0);
}
}
}
(10)编辑学生窗口EditStudentFrame
/**
* 包名:net.hw.student.gui
* 类名:EditStudentFrame
* 描述:编辑学生记录
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
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.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import net.hw.student.bean.Student;
import net.hw.student.service.StudentService;
import net.hw.student.service.impl.StudentServiceImpl;
public class EditStudentFrame extends JFrame {
/**
* 声明面板
*/
private JPanel panel;
private JPanel pnlCenter;
private JPanel pnlRow1;
private JPanel pnlRow2;
private JPanel pnlRow3;
private JPanel pnlRow4;
private JPanel pnlRow5;
private JPanel pnlRow6;
private JPanel pnlRow7;
private JPanel pnlSouth;
private JPanel pnlSouth1;
private JPanel pnlSouth2;
/**
* 声明标签
*/
private JLabel lblId;
private JLabel lblName;
private JLabel lblSex;
private JLabel lblAge;
private JLabel lblDepartment;
private JLabel lblClass;
private JLabel lblTelephone;
/**
* 声明文本框
*/
private JTextField txtId;
private JTextField txtName;
private JTextField txtSex;
private JTextField txtAge;
private JTextField txtDepartment;
private JTextField txtClass;
private JTextField txtTelephone;
/**
* 声明按钮
*/
private JButton btnTop;
private JButton btnPrevious;
private JButton btnNext;
private JButton btnBottom;
private JButton btnExit;
private JButton btnEdit;
private JButton btnOK;
private JButton btnCancel;
/**
* 当前记录行号
*/
private int currentRow;
/**
* 学生列表
*/
private List<Student> students;
/**
* 创建学生服务对象
*/
StudentService studentService;
// 构造方法
public EditStudentFrame(String title) {
super(title);
initGUI();
}
private void initGUI() {
// 创建组件
panel = (JPanel) getContentPane();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
pnlSouth.setLayout(new GridLayout(2, 1));
pnlRow1 = new JPanel();
pnlRow2 = new JPanel();
pnlRow3 = new JPanel();
pnlRow4 = new JPanel();
pnlRow5 = new JPanel();
pnlRow6 = new JPanel();
pnlRow7 = new JPanel();
pnlSouth1 = new JPanel();
pnlSouth2 = new JPanel();
pnlRow1.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow2.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow3.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow4.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow5.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow6.setLayout(new FlowLayout(FlowLayout.LEFT));
pnlRow7.setLayout(new FlowLayout(FlowLayout.LEFT));
lblId = new JLabel("学号:");
lblName = new JLabel("姓名:");
lblSex = new JLabel("性别:");
lblAge = new JLabel("年龄:");
lblDepartment = new JLabel("系部:");
lblClass = new JLabel("班级:");
lblTelephone = new JLabel("电话:");
txtId = new JTextField(40);
txtName = new JTextField(40);
txtSex = new JTextField(40);
txtAge = new JTextField(40);
txtDepartment = new JTextField(40);
txtClass = new JTextField(40);
txtTelephone = new JTextField(40);
txtId.setEditable(false);
txtName.setEditable(false);
txtSex.setEditable(false);
txtAge.setEditable(false);
txtDepartment.setEditable(false);
txtClass.setEditable(false);
txtTelephone.setEditable(false);
btnTop = new JButton("第一条[T]");
btnPrevious = new JButton("上一条[P]");
btnNext = new JButton("下一条[N]");
btnBottom = new JButton("最后一条[B]");
btnExit = new JButton("退出[X]");
btnEdit = new JButton("编辑[E]");
btnOK = new JButton("确定[O]");
btnCancel = new JButton("取消[C]");
btnOK.setEnabled(false);
btnCancel.setEnabled(false);
btnTop.setMnemonic(KeyEvent.VK_T);
btnPrevious.setMnemonic(KeyEvent.VK_P);
btnNext.setMnemonic(KeyEvent.VK_N);
btnBottom.setMnemonic(KeyEvent.VK_B);
btnExit.setMnemonic(KeyEvent.VK_X);
btnEdit.setMnemonic(KeyEvent.VK_E);
btnOK.setMnemonic(KeyEvent.VK_O);
btnCancel.setMnemonic(KeyEvent.VK_C);
// 添加组件
panel.setLayout(new BorderLayout());
panel.add(pnlCenter, BorderLayout.CENTER);
panel.add(pnlSouth, BorderLayout.SOUTH);
pnlCenter.setLayout(new GridLayout(7, 1));
pnlCenter.add(pnlRow1);
pnlCenter.add(pnlRow2);
pnlCenter.add(pnlRow3);
pnlCenter.add(pnlRow4);
pnlCenter.add(pnlRow5);
pnlCenter.add(pnlRow6);
pnlCenter.add(pnlRow7);
pnlRow1.add(lblId);
pnlRow1.add(txtId);
pnlRow2.add(lblName);
pnlRow2.add(txtName);
pnlRow3.add(lblSex);
pnlRow3.add(txtSex);
pnlRow4.add(lblAge);
pnlRow4.add(txtAge);
pnlRow5.add(lblDepartment);
pnlRow5.add(txtDepartment);
pnlRow6.add(lblClass);
pnlRow6.add(txtClass);
pnlRow7.add(lblTelephone);
pnlRow7.add(txtTelephone);
pnlSouth.add(pnlSouth1);
pnlSouth.add(pnlSouth2);
pnlSouth1.add(btnTop);
pnlSouth1.add(btnPrevious);
pnlSouth1.add(btnNext);
pnlSouth1.add(btnBottom);
pnlSouth1.add(btnExit);
pnlSouth2.add(btnEdit);
pnlSouth2.add(btnOK);
pnlSouth2.add(btnCancel);
// 设置窗口属性
setSize(500, 300);
setResizable(false);
pack();
setLocationRelativeTo(null);
setVisible(true);
// 创建学生服务对象
studentService = new StudentServiceImpl();
// 获取全部学生列表
students = studentService.findAllStudents();
// 判断是否有学生记录
if (students.size() > 0) {
// 设置当前记录号
currentRow = 1;
// 设置窗口标题
setTitle("浏览学生表记录" + " && 当前记录:" + currentRow);
// 填充窗口各文本框数据
fillFrameData(currentRow);
} else {
JOptionPane.showMessageDialog(null, "表中没有记录!", "浏览学生表记录", JOptionPane.ERROR_MESSAGE);
btnTop.setEnabled(false);
btnPrevious.setEnabled(false);
btnNext.setEnabled(false);
btnBottom.setEnabled(false);
}
// 【第一条】按钮单击事件
btnTop.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 设置当前记录号
currentRow = 1;
// 填充当前记录数据
fillFrameData(currentRow);
}
});
// 【上一条】按钮单击事件
btnPrevious.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (currentRow > 1) {
// 设置当前记录号
currentRow--;
// 填充当前记录数据
fillFrameData(currentRow);
} else {
JOptionPane.showMessageDialog(null, "已到第一条记录!", "浏览学生表记录", JOptionPane.WARNING_MESSAGE);
}
}
});
// 【下一条】按钮单击事件
btnNext.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (currentRow < students.size() - 1) {
// 设置当前记录号
currentRow++;
// 填充当前记录数据
fillFrameData(currentRow);
} else {
JOptionPane.showMessageDialog(null, "已到最后一条记录!", "浏览学生表记录", JOptionPane.WARNING_MESSAGE);
}
}
});
// 【最后一条】按钮单击事件
btnBottom.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 设置当前记录号
currentRow = students.size() - 1;
// 填充当前记录数据
fillFrameData(currentRow);
}
});
// 【退出】按钮单击事件
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dispose();
}
});
// 【编辑】按钮单击事件
btnEdit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
txtName.setEditable(true);
txtSex.setEditable(true);
txtAge.setEditable(true);
txtDepartment.setEditable(true);
txtClass.setEditable(true);
txtTelephone.setEditable(true);
btnOK.setEnabled(true);
btnCancel.setEnabled(true);
}
});
// 【确定】按钮单击事件
btnOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取当前学生实体
Student student = students.get(currentRow);
if (isNumber(txtAge.getText())) {
if (isLegalTelephone(txtTelephone.getText())) {
// 修改学生实体属性
student.setName(txtName.getText());
student.setSex(txtSex.getText());
student.setAge(Integer.parseInt(txtAge.getText()));
student.setDepartment(txtDepartment.getText());
student.setClazz(txtClass.getText());
student.setTelephone(txtTelephone.getText());
// 更新学生信息
int count = studentService.updateStudent(student);
// 判断是否更新成功
if (count > 0) {
JOptionPane.showMessageDialog(null, "更新记录成功!", "编辑学生记录", JOptionPane.INFORMATION_MESSAGE);
btnOK.setEnabled(false);
btnCancel.setEnabled(false);
btnEdit.setEnabled(true);
txtName.setEditable(false);
txtSex.setEditable(false);
txtAge.setEditable(false);
txtDepartment.setEditable(false);
txtClass.setEditable(false);
txtTelephone.setEditable(false);
// 重新获取全部学生列表
students = studentService.findAllStudents();
} else {
JOptionPane.showMessageDialog(null, "更新记录失败!", "编辑学生记录", JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(null, "非法手机号!", "编辑学生记录", JOptionPane.ERROR_MESSAGE);
txtTelephone.selectAll();
txtTelephone.requestFocus();
}
} else {
JOptionPane.showMessageDialog(null, "年龄必须是数字!", "编辑学生记录", JOptionPane.ERROR_MESSAGE);
txtAge.selectAll();
txtAge.requestFocus();
}
}
});
// 【取消】按钮单击事件
btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
btnOK.setEnabled(false);
btnCancel.setEnabled(false);
btnEdit.setEnabled(true);
txtName.setEditable(false);
txtSex.setEditable(false);
txtAge.setEditable(false);
txtDepartment.setEditable(false);
txtClass.setEditable(false);
txtTelephone.setEditable(false);
// 恢复文本框修改前的值
txtName.setText(students.get(currentRow).getName());
txtSex.setText(students.get(currentRow).getSex());
txtAge.setText(students.get(currentRow).getAge() + "");
txtDepartment.setText(students.get(currentRow).getDepartment());
txtClass.setText(students.get(currentRow).getClazz());
txtTelephone.setText(students.get(currentRow).getTelephone());
}
});
// 文本框按键事件
txtId.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
txtName.requestFocus();
}
}
});
txtName.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
txtSex.requestFocus();
}
}
});
txtSex.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
txtAge.requestFocus();
}
}
});
txtAge.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
txtDepartment.requestFocus();
}
}
});
txtDepartment.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
txtClass.requestFocus();
}
}
});
txtClass.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
txtTelephone.requestFocus();
}
}
});
}
/**
* 将当前记录数据填充窗口各文本框
*
* @param currentRow
*/
private void fillFrameData(int currentRow) {
if (currentRow > 0) {
setTitle("浏览学生表记录" + " && 当前记录:" + currentRow);
txtId.setText(students.get(currentRow).getId());
txtName.setText(students.get(currentRow).getName());
txtSex.setText(students.get(currentRow).getSex());
txtAge.setText(students.get(currentRow).getAge() + "");
txtDepartment.setText(students.get(currentRow).getDepartment());
txtClass.setText(students.get(currentRow).getClazz());
txtTelephone.setText(students.get(currentRow).getTelephone());
}
}
// 判断一个字符串是否全是数字
private boolean isNumber(String str) {
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) < '0' || str.charAt(i) > '9') {
return false;
}
}
return true;
}
/**
* 判断是否合法手机号
*
* @param telephone
* @return
*/
private boolean isLegalTelephone(String telephone) {
Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
Matcher m = p.matcher(telephone);
return m.matches();
}
}
(11)按学号查找学生窗口FindStudentByIdFrame
/**
* 包名:net.hw.student.gui
* 类名:FindStudentByIdFrame
* 描述:按学号查找学生
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.print.PrinterException;
import java.util.List;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import net.hw.student.bean.Student;
import net.hw.student.service.StudentService;
import net.hw.student.service.impl.StudentServiceImpl;
public class FindStudentByIdFrame extends JFrame {
/**
* 学号标签
*/
private JLabel lblInputId;
/**
* 学号文本框
*/
private JTextField txtId;
/**
* 面板
*/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
private JPanel pnlNorth;
/**
* 按钮
*/
private JButton btnQuery;
private JButton btnBrowseAll;
private JButton btnPrint;
private JButton btnExit;
/**
* 记录行集
*/
private Vector rows;
/**
* 表格列标题
*/
private Vector<String> colHead;
/**
* 表格
*/
private JTable table;
/**
* 滚动面板
*/
private JScrollPane scroller;
/**
* 当前记录行号
*/
private int currentRow;
/**
* 学生列表
*/
private List<Student> students;
/**
* 创建学生服务对象
*/
private StudentService studentService;
/**
* 构造方法
*
* @param title
*/
public FindStudentByIdFrame(String title) {
super(title);
intiGUI();
}
/**
* 初始化用户界面
*/
private void intiGUI() {
// 创建对象
panel = (JPanel) getContentPane();
pnlNorth = new JPanel();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
rows = new Vector();
colHead = new Vector();
lblInputId = new JLabel("输入学号:");
txtId = new JTextField(10);
txtId.setHorizontalAlignment(JTextField.CENTER);
btnQuery = new JButton("查询[Q]");
btnQuery.setMnemonic(KeyEvent.VK_Q);
btnBrowseAll = new JButton("显示全部记录[A]");
btnBrowseAll.setMnemonic(KeyEvent.VK_A);
btnPrint = new JButton("打印[P]");
btnPrint.setMnemonic(KeyEvent.VK_D);
btnExit = new JButton("退出[X]");
btnExit.setMnemonic(KeyEvent.VK_X);
// 添加组件
panel.add(pnlSouth, BorderLayout.SOUTH);
panel.add(pnlCenter, BorderLayout.CENTER);
panel.add(pnlNorth, BorderLayout.NORTH);
pnlNorth.add(lblInputId);
pnlNorth.add(txtId);
pnlNorth.add(btnQuery);
pnlNorth.add(btnBrowseAll);
pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));
pnlSouth.add(btnPrint);
pnlSouth.add(btnExit);
pnlCenter.setLayout(new BorderLayout());
// 创建标题边框对象
TitledBorder tb = new TitledBorder("查询结果");
pnlCenter.setBorder(tb);
// 创建学生服务对象
studentService = new StudentServiceImpl();
// 获取全部学生列表
students = studentService.findAllStudents();
// 填充表格数据
fillTableData();
// 设置窗口属性
setSize(600, 400);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
// 【退出】按钮单击事件
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
dispose();
}
});
// 【打印】按钮单击事件
btnPrint.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
table.print();
} catch (PrinterException e1) {
e1.printStackTrace();
}
}
});
// 【查询】按钮单击事件
btnQuery.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
doQuery();
}
});
// 【显示全部记录】按钮单击事件
btnBrowseAll.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
// 获取全部学生记录
students = studentService.findAllStudents();
// 填充表格数据
fillTableData();
// 删除按钮不可用
btnPrint.setEnabled(false);
}
});
// 文本框按键事件
txtId.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
doQuery();
}
}
});
// JTable单击事件
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// 获取当前行的行数
int row = table.rowAtPoint(e.getPoint());
// 选中鼠标单击的行
table.setRowSelectionInterval(row, row);
// 设置文本框内容
txtId.setText(table.getValueAt(row, 0).toString());
}
});
}
/**
* 查询方法
*/
private void doQuery() {
// 获取查询学号
String id = txtId.getText().trim();
if (!id.equals("")) {
students.clear();
Student student = studentService.findStudentById(id);
if (student != null) {
// 将查询到的学生添加到列表
students.add(student);
}
// 填充表格
fillTableData();
} else {
JOptionPane.showMessageDialog(this, "请输入待查学生学号!", "警告", JOptionPane.WARNING_MESSAGE);
txtId.requestFocus();
}
}
/**
* 填充表格方法
*/
private void fillTableData() {
// 填充表头
colHead.clear();
colHead.add("学号");
colHead.add("姓名");
colHead.add("性别");
colHead.add("年龄");
colHead.add("系部");
colHead.add("班级");
colHead.add("电话");
// 填充表记录
rows.clear();
for (Student student : students) {
Vector<String> currentRow = new Vector<String>();
currentRow.addElement(student.getId());
currentRow.addElement(student.getName());
currentRow.addElement(student.getSex());
currentRow.addElement(student.getAge() + "");
currentRow.addElement(student.getDepartment());
currentRow.addElement(student.getClazz());
currentRow.addElement(student.getTelephone());
// 将当前行添加到记录行集
rows.add(currentRow);
}
// 创建表格(参数1:记录集;参数2:表头)
table = new JTable(rows, colHead);
// 定义滚动面板
scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// 将滚动面板添加到中心面板
pnlCenter.add(scroller, BorderLayout.CENTER);
// 重绘窗体
repaint();
// 判断是否有记录行
if (rows.isEmpty()) {
JOptionPane.showMessageDialog(this, "没有符合条件的记录!", "错误提示", JOptionPane.WARNING_MESSAGE);
txtId.setText("");
} else {
// 让滚动条移到最上方
scroller.getVerticalScrollBar().setValue(0);
}
}
}
(12)按姓名查找学生窗口FindStudentByNameFrame
/**
* 包名:net.hw.student.gui
* 类名:FindStudentByNameFrame
* 描述:按姓名查找学生
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.print.PrinterException;
import java.util.List;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import net.hw.student.bean.Student;
import net.hw.student.service.StudentService;
import net.hw.student.service.impl.StudentServiceImpl;
public class FindStudentByNameFrame extends JFrame {
/**
* 姓名标签
*/
private JLabel lblInputName;
/**
* 姓名文本框
*/
private JTextField txtName;
/**
* 面板
*/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
private JPanel pnlNorth;
/**
* 按钮
*/
private JButton btnQuery;
private JButton btnBrowseAll;
private JButton btnPrint;
private JButton btnExit;
/**
* 记录行集
*/
private Vector rows;
/**
* 表格列标题
*/
private Vector<String> colHead;
/**
* 表格
*/
private JTable table;
/**
* 滚动面板
*/
private JScrollPane scroller;
/**
* 当前记录行号
*/
private int currentRow;
/**
* 学生列表
*/
private List<Student> students;
/**
* 创建学生服务对象
*/
private StudentService studentService;
/**
* 构造方法
*
* @param title
*/
public FindStudentByNameFrame(String title) {
super(title);
intiGUI();
}
/**
* 初始化用户界面
*/
private void intiGUI() {
// 创建对象
panel = (JPanel) getContentPane();
pnlNorth = new JPanel();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
rows = new Vector();
colHead = new Vector();
lblInputName = new JLabel("输入姓名:");
txtName = new JTextField(10);
txtName.setHorizontalAlignment(JTextField.CENTER);
btnQuery = new JButton("查询[Q]");
btnQuery.setMnemonic(KeyEvent.VK_Q);
btnBrowseAll = new JButton("显示全部记录[A]");
btnBrowseAll.setMnemonic(KeyEvent.VK_A);
btnPrint = new JButton("打印[P]");
btnPrint.setMnemonic(KeyEvent.VK_D);
btnExit = new JButton("退出[X]");
btnExit.setMnemonic(KeyEvent.VK_X);
// 添加组件
panel.add(pnlSouth, BorderLayout.SOUTH);
panel.add(pnlCenter, BorderLayout.CENTER);
panel.add(pnlNorth, BorderLayout.NORTH);
pnlNorth.add(lblInputName);
pnlNorth.add(txtName);
pnlNorth.add(btnQuery);
pnlNorth.add(btnBrowseAll);
pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));
pnlSouth.add(btnPrint);
pnlSouth.add(btnExit);
pnlCenter.setLayout(new BorderLayout());
// 创建标题边框对象
TitledBorder tb = new TitledBorder("查询结果");
pnlCenter.setBorder(tb);
// 创建学生服务对象
studentService = new StudentServiceImpl();
// 获取全部学生列表
students = studentService.findAllStudents();
// 填充表格数据
fillTableData();
// 设置窗口属性
setSize(600, 400);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
// 【退出】按钮单击事件
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
dispose();
}
});
// 【打印】按钮单击事件
btnPrint.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
table.print();
} catch (PrinterException e1) {
e1.printStackTrace();
}
}
});
// 【查询】按钮单击事件
btnQuery.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
doQuery();
}
});
// 【显示全部记录】按钮单击事件
btnBrowseAll.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
// 获取全部学生记录
students = studentService.findAllStudents();
// 填充表格数据
fillTableData();
}
});
// 文本框按键事件
txtName.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
doQuery();
}
}
});
// JTable单击事件
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// 获取当前行的行数
int row = table.rowAtPoint(e.getPoint());
// 选中鼠标单击的行
table.setRowSelectionInterval(row, row);
// 设置文本框内容
txtName.setText(table.getValueAt(row, 1).toString());
}
});
}
/**
* 查询方法
*/
private void doQuery() {
// 获取查询姓名
String name = txtName.getText().trim();
if (!name.equals("")) {
// 按姓名查询获取学生列表
students = studentService.findStudentsByName(name);
// 填充表格
fillTableData();
} else {
JOptionPane.showMessageDialog(this, "请输入待查学生姓名!", "警告", JOptionPane.WARNING_MESSAGE);
txtName.requestFocus();
}
}
/**
* 填充表格方法
*/
private void fillTableData() {
// 填充表头
colHead.add("学号");
colHead.add("姓名");
colHead.add("性别");
colHead.add("年龄");
colHead.add("系部");
colHead.add("班级");
colHead.add("电话");
// 填充表记录
rows.clear();
for (Student student : students) {
Vector<String> currentRow = new Vector<String>();
currentRow.addElement(student.getId());
currentRow.addElement(student.getName());
currentRow.addElement(student.getSex());
currentRow.addElement(student.getAge() + "");
currentRow.addElement(student.getDepartment());
currentRow.addElement(student.getClazz());
currentRow.addElement(student.getTelephone());
// 将当前行添加到记录行集
rows.add(currentRow);
}
// 创建表格(参数1:记录集;参数2:表头)
table = new JTable(rows, colHead);
// 定义滚动面板
scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// 将滚动面板添加到中心面板
pnlCenter.add(scroller, BorderLayout.CENTER);
// 重绘窗体
repaint();
// 判断是否有记录行
if (rows.isEmpty()) {
JOptionPane.showMessageDialog(this, "没有符合条件的记录!", "错误提示", JOptionPane.WARNING_MESSAGE);
txtName.setText("");
} else {
// 让滚动条移到最上方
scroller.getVerticalScrollBar().setValue(0);
}
}
}
(13)按班级查找学生窗口FindStudentsByClassFrame
/**
* 包名:net.hw.student.gui
* 类名:FindStudentsByClassFrame
* 描述:按班级查找学生
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.print.PrinterException;
import java.util.List;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import net.hw.student.bean.Student;
import net.hw.student.service.StudentService;
import net.hw.student.service.impl.StudentServiceImpl;
public class FindStudentsByClassFrame extends JFrame {
/**
* 班级标签
*/
private JLabel lblInputClass;
/**
* 班级文本框
*/
private JTextField txtClass;
/**
* 面板
*/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
private JPanel pnlNorth;
/**
* 按钮
*/
private JButton btnQuery;
private JButton btnBrowseAll;
private JButton btnPrint;
private JButton btnExit;
/**
* 记录行集
*/
private Vector rows;
/**
* 表格列标题
*/
private Vector<String> colHead;
/**
* 表格
*/
private JTable table;
/**
* 滚动面板
*/
private JScrollPane scroller;
/**
* 当前记录行号
*/
private int currentRow;
/**
* 学生列表
*/
private List<Student> students;
/**
* 创建学生服务对象
*/
private StudentService studentService;
/**
* 构造方法
*
* @param title
*/
public FindStudentsByClassFrame(String title) {
super(title);
intiGUI();
}
/**
* 初始化用户界面
*/
private void intiGUI() {
// 创建对象
panel = (JPanel) getContentPane();
pnlNorth = new JPanel();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
rows = new Vector();
colHead = new Vector();
lblInputClass = new JLabel("输入班级:");
txtClass = new JTextField(10);
txtClass.setHorizontalAlignment(JTextField.CENTER);
btnQuery = new JButton("查询[Q]");
btnQuery.setMnemonic(KeyEvent.VK_Q);
btnBrowseAll = new JButton("显示全部记录[A]");
btnBrowseAll.setMnemonic(KeyEvent.VK_A);
btnPrint = new JButton("打印[P]");
btnPrint.setMnemonic(KeyEvent.VK_D);
btnExit = new JButton("退出[X]");
btnExit.setMnemonic(KeyEvent.VK_X);
// 添加组件
panel.add(pnlSouth, BorderLayout.SOUTH);
panel.add(pnlCenter, BorderLayout.CENTER);
panel.add(pnlNorth, BorderLayout.NORTH);
pnlNorth.add(lblInputClass);
pnlNorth.add(txtClass);
pnlNorth.add(btnQuery);
pnlNorth.add(btnBrowseAll);
pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));
pnlSouth.add(btnPrint);
pnlSouth.add(btnExit);
pnlCenter.setLayout(new BorderLayout());
// 创建标题边框对象
TitledBorder tb = new TitledBorder("查询结果");
pnlCenter.setBorder(tb);
// 创建学生服务对象
studentService = new StudentServiceImpl();
// 获取全部学生列表
students = studentService.findAllStudents();
// 填充表格数据
fillTableData();
// 设置窗口属性
setSize(600, 400);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
// 【退出】按钮单击事件
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
dispose();
}
});
// 【打印】按钮单击事件
btnPrint.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
table.print();
} catch (PrinterException e1) {
e1.printStackTrace();
}
}
});
// 【查询】按钮单击事件
btnQuery.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
doQuery();
}
});
// 【显示全部记录】按钮单击事件
btnBrowseAll.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
// 获取全部学生记录
students = studentService.findAllStudents();
// 填充表格数据
fillTableData();
}
});
// 文本框按键事件
txtClass.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
doQuery();
}
}
});
// JTable单击事件
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// 获取当前行的行数
int row = table.rowAtPoint(e.getPoint());
// 选中鼠标单击的行
table.setRowSelectionInterval(row, row);
// 设置文本框内容
txtClass.setText(table.getValueAt(row, 5).toString());
}
});
}
/**
* 查询方法
*/
private void doQuery() {
// 获取查询班级
String clazz = txtClass.getText().trim();
if (!clazz.equals("")) {
// 按班级查询获取学生列表
students = studentService.findStudentsByClass(clazz);
// 填充表格
fillTableData();
} else {
JOptionPane.showMessageDialog(this, "请输入待查班级!", "警告", JOptionPane.WARNING_MESSAGE);
txtClass.requestFocus();
}
}
/**
* 填充表格方法
*/
private void fillTableData() {
// 填充表头
colHead.clear();
colHead.add("学号");
colHead.add("姓名");
colHead.add("性别");
colHead.add("年龄");
colHead.add("系部");
colHead.add("班级");
colHead.add("电话");
// 填充表记录
rows.clear();
for (Student student : students) {
Vector<String> currentRow = new Vector<String>();
currentRow.addElement(student.getId());
currentRow.addElement(student.getName());
currentRow.addElement(student.getSex());
currentRow.addElement(student.getAge() + "");
currentRow.addElement(student.getDepartment());
currentRow.addElement(student.getClazz());
currentRow.addElement(student.getTelephone());
// 将当前行添加到记录行集
rows.add(currentRow);
}
// 创建表格(参数1:记录集;参数2:表头)
table = new JTable(rows, colHead);
// 定义滚动面板
scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// 将滚动面板添加到中心面板
pnlCenter.add(scroller, BorderLayout.CENTER);
// 重绘窗体
repaint();
// 判断是否有记录行
if (rows.isEmpty()) {
JOptionPane.showMessageDialog(this, "没有符合条件的记录!", "错误提示", JOptionPane.WARNING_MESSAGE);
txtClass.setText("");
} else {
// 让滚动条移到最上方
scroller.getVerticalScrollBar().setValue(0);
}
}
}
(11)按系部查找学生窗口FindStudentsByDepartmentFrame
/**
* 包名:net.hw.student.gui
* 类名:FindStudentsByDepartmentFrame
* 描述:按系部查找学生
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.gui;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.print.PrinterException;
import java.util.List;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import net.hw.student.bean.Student;
import net.hw.student.service.StudentService;
import net.hw.student.service.impl.StudentServiceImpl;
public class FindStudentsByDepartmentFrame extends JFrame {
/**
* 系部标签
*/
private JLabel lblInputDepartment;
/**
* 系部文本框
*/
private JTextField txtDepartment;
/**
* 面板
*/
private JPanel panel;
private JPanel pnlSouth;
private JPanel pnlCenter;
private JPanel pnlNorth;
/**
* 按钮
*/
private JButton btnQuery;
private JButton btnBrowseAll;
private JButton btnPrint;
private JButton btnExit;
/**
* 记录行集
*/
private Vector rows;
/**
* 表格列标题
*/
private Vector<String> colHead;
/**
* 表格
*/
private JTable table;
/**
* 滚动面板
*/
private JScrollPane scroller;
/**
* 当前记录行号
*/
private int currentRow;
/**
* 学生列表
*/
private List<Student> students;
/**
* 创建学生服务对象
*/
private StudentService studentService;
/**
* 构造方法
*
* @param title
*/
public FindStudentsByDepartmentFrame(String title) {
super(title);
intiGUI();
}
/**
* 初始化用户界面
*/
private void intiGUI() {
// 创建对象
panel = (JPanel) getContentPane();
pnlNorth = new JPanel();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
rows = new Vector();
colHead = new Vector();
lblInputDepartment = new JLabel("输入系部:");
txtDepartment = new JTextField(10);
txtDepartment.setHorizontalAlignment(JTextField.CENTER);
btnQuery = new JButton("查询[Q]");
btnQuery.setMnemonic(KeyEvent.VK_Q);
btnBrowseAll = new JButton("显示全部记录[A]");
btnBrowseAll.setMnemonic(KeyEvent.VK_A);
btnPrint = new JButton("打印[P]");
btnPrint.setMnemonic(KeyEvent.VK_D);
btnExit = new JButton("退出[X]");
btnExit.setMnemonic(KeyEvent.VK_X);
// 添加组件
panel.add(pnlSouth, BorderLayout.SOUTH);
panel.add(pnlCenter, BorderLayout.CENTER);
panel.add(pnlNorth, BorderLayout.NORTH);
pnlNorth.add(lblInputDepartment);
pnlNorth.add(txtDepartment);
pnlNorth.add(btnQuery);
pnlNorth.add(btnBrowseAll);
pnlSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));
pnlSouth.add(btnPrint);
pnlSouth.add(btnExit);
pnlCenter.setLayout(new BorderLayout());
// 创建标题边框对象
TitledBorder tb = new TitledBorder("查询结果");
pnlCenter.setBorder(tb);
// 创建学生服务对象
studentService = new StudentServiceImpl();
// 获取全部学生列表
students = studentService.findAllStudents();
// 填充表格数据
fillTableData();
// 设置窗口属性
setSize(600, 400);
setLocationRelativeTo(null);
setResizable(false);
setVisible(true);
// 【退出】按钮单击事件
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
dispose();
}
});
// 【打印】按钮单击事件
btnPrint.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
table.print();
} catch (PrinterException e1) {
e1.printStackTrace();
}
}
});
// 【查询】按钮单击事件
btnQuery.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
doQuery();
}
});
// 【显示全部记录】按钮单击事件
btnBrowseAll.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
// 获取全部学生记录
students = studentService.findAllStudents();
// 填充表格数据
fillTableData();
}
});
// 文本框按键事件
txtDepartment.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
doQuery();
}
}
});
// JTable单击事件
table.addMouseListener(new MouseAdapter() {
/* (non-Javadoc)
* @see java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent)
*/
/* (non-Javadoc)
* @see java.awt.event.MouseAdapter#mouseClicked(java.awt.event.MouseEvent)
*/
public void mouseClicked(MouseEvent e) {
// 获取当前行的行数
int row = table.rowAtPoint(e.getPoint());
// 选中鼠标单击的行
table.setRowSelectionInterval(row, row);
// 设置文本框内容
txtDepartment.setText(table.getValueAt(row, 4).toString());
}
});
}
/**
* 查询方法
*/
private void doQuery() {
// 获取查询系部
String department = txtDepartment.getText().trim();
if (!department.equals("")) {
// 按系部查询获取学生列表
students = studentService.findStudentsByDepartment(department);
// 填充表格
fillTableData();
} else {
JOptionPane.showMessageDialog(this, "请输入待查系部!", "警告", JOptionPane.WARNING_MESSAGE);
txtDepartment.requestFocus();
}
}
/**
* 填充表格方法
*/
private void fillTableData() {
// 填充表头
colHead.clear();
colHead.add("学号");
colHead.add("姓名");
colHead.add("性别");
colHead.add("年龄");
colHead.add("系部");
colHead.add("班级");
colHead.add("电话");
// 填充表记录
rows.clear();
for (Student student : students) {
Vector<String> currentRow = new Vector<String>();
currentRow.addElement(student.getId());
currentRow.addElement(student.getName());
currentRow.addElement(student.getSex());
currentRow.addElement(student.getAge() + "");
currentRow.addElement(student.getDepartment());
currentRow.addElement(student.getClazz());
currentRow.addElement(student.getTelephone());
// 将当前行添加到记录行集
rows.add(currentRow);
}
// 创建表格(参数1:记录集;参数2:表头)
table = new JTable(rows, colHead);
// 定义滚动面板
scroller = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
// 将滚动面板添加到中心面板
pnlCenter.add(scroller, BorderLayout.CENTER);
// 重绘窗体
repaint();
// 判断是否有记录行
if (rows.isEmpty()) {
JOptionPane.showMessageDialog(this, "没有符合条件的记录!", "错误提示", JOptionPane.WARNING_MESSAGE);
txtDepartment.setText("");
} else {
// 让滚动条移到最上方
scroller.getVerticalScrollBar().setValue(0);
}
}
}
(12)登录窗口LoginFrame(系统入口类)
/**
* 包名:net.hw.student.gui
* 类名:LoginFrame
* 描述:用户登录
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.gui;
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 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 net.hw.student.app.Application;
import net.hw.student.bean.User;
import net.hw.student.service.StatusService;
import net.hw.student.service.UserService;
import net.hw.student.service.impl.StatusServiceImpl;
import net.hw.student.service.impl.UserServiceImpl;
public class LoginFrame extends JFrame {
/**
* 用户名
*/
private String username;
/**
* 密码
*/
private String password;
/**
* 用户名标签
*/
private JLabel lblUsername;
/**
* 密码标签
*/
private JLabel lblPassword;
/**
* 用户名文本框
*/
private JTextField txtUsername;
/**
* 密码文本框
*/
private JPasswordField txtPassword;
/**
* 确定按钮
*/
private JButton btnOK;
/**
* 取消按钮
*/
private JButton btnCancel;
/**
* 注册按钮
*/
private JButton btnRegister;
/**
* 主面板
*/
private JPanel panel;
/**
* 第一行面板
*/
private JPanel panel1;
/**
* 第二行面板
*/
private JPanel panel2;
/**
* 第三行面板
*/
private JPanel panel3;
/**
* 构造方法
*
* @param title
*/
public LoginFrame(String title) {
super(title);
// 调用初始化界面方法
initGUI();
}
/**
* 初始化界面
*/
private void initGUI() {
// 实例化控件
lblUsername = new JLabel("用户名:");
lblPassword = new JLabel("密 码:");
txtUsername = new JTextField("", 15);
txtPassword = new JPasswordField("", 15);
btnOK = new JButton("确定[O]");
btnCancel = new JButton("取消[C]");
btnRegister = new JButton("注册[R]");
panel = (JPanel) getContentPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
// 设置主面板为网格布局
panel.setLayout(new GridLayout(3, 1));
// 将三行面板添加到主面板
panel.add(panel1);
panel.add(panel2);
panel.add(panel3);
// 将控件分别添加到三行面板
panel1.add(lblUsername);
panel1.add(txtUsername);
panel2.add(lblPassword);
panel2.add(txtPassword);
panel3.add(btnOK);
panel3.add(btnCancel);
panel3.add(btnRegister);
// 设置窗口与控件属性
setSize(250, 200); // 设置窗口大小
setLocationRelativeTo(null);// 让窗口居中
setResizable(false); // 窗口不可调整大小
pack(); // 使窗口恰好容纳组件
setVisible(true); // 让窗口可见
btnOK.setMnemonic(KeyEvent.VK_O); // 设置热键字母
btnCancel.setMnemonic(KeyEvent.VK_C);// 设置热键字母
btnRegister.setMnemonic(KeyEvent.VK_R);// 设置热键字母
// txtPassword.setEchoChar('*');//设置回显字符
/*
* 注册监听器,编写事件处理代码 采用匿名内部类方式来实现
*/
// 确定按钮单击事件
btnOK.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
login();
}
});
// 确定按钮按键事件
btnOK.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
login();
}
}
});
// 取消按钮单击事件
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
// 注册按钮单击事件
btnRegister.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// 隐藏登录窗口
Application.loginFrame.setVisible(false);
// 实例化注册窗口
Application.registerFrame = new RegisterFrame("注册");
}
});
// 用户名文本框按键事件
txtUsername.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
txtPassword.requestFocus();
}
}
});
// 密码文本框按键事件
txtPassword.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
login();
}
}
});
}
/**
* 登录方法
*/
private void login() {
// 获取用户名
username = txtUsername.getText().trim();
// 获取密码
password = new String(txtPassword.getPassword());
// 创建用户服务对象
UserService userService = new UserServiceImpl();
// 用户登录
User user = userService.login(username, password);
// 判断是否登录成功
if (user != null) {
// 隐藏登录窗口
Application.loginFrame.setVisible(false);
// 定义状态服务对象
StatusService statusService = new StatusServiceImpl();
// 保存用户标识
Application.id = user.getId();
// 保存用户名
Application.username = user.getUsernname();
// 提示用户登录成功
JOptionPane.showMessageDialog(null, "欢迎使用学生信息管理系统" + statusService.findStatusById(1).getVersion() + "!",
"学生信息管理系统", JOptionPane.INFORMATION_MESSAGE);
// 显示系统主窗口
Application.mainFrame = new MainFrame("学生信息管理系统" + statusService.findStatusById(1).getVersion());
// 释放登录窗口
Application.loginFrame.dispose();
} else {
// 隐藏登录窗口
Application.loginFrame.setVisible(false);
// 提示用户登录失败
JOptionPane.showMessageDialog(null, "用户名或密码错误,请重新输入!", "学生信息管理系统", JOptionPane.ERROR_MESSAGE);
// 显示登录窗口
Application.loginFrame.setVisible(true);
// 用户名文本全部选中
txtUsername.selectAll();
// 密码文本全部选中
txtPassword.selectAll();
// 用户名文本框获取焦点
txtUsername.requestFocus();
}
}
/**
* 主方法
*
* @param args
*/
public static void main(String[] args) {
// 设置应用程序登录窗口
Application.loginFrame = new LoginFrame("登录");
}
}
(13)系统主窗口MainFrame
/**
* 包名:net.hw.student.gui
* 类名:MainFrame
* 描述:系统主控窗口
* 通过菜单调用各功能模块
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.gui;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import net.hw.student.app.Application;
import net.hw.student.bean.Status;
import net.hw.student.service.StatusService;
import net.hw.student.service.impl.StatusServiceImpl;
public class MainFrame extends JFrame {
/**
* 菜单部分
*/
private JMenuBar mnbMain;
/**
* 设置菜单
*/
private JMenu mnuSet;
private JMenuItem mniSetCollegeInfo;
private JMenuItem mniSetStatusBar;
private JMenuItem mniChangePassword;
private JMenuItem mniExit;
/**
* 操作菜单
*/
private JMenu mnuOperate;
private JMenuItem mniAddStudent;
private JMenuItem mniBrowseStudent;
private JMenuItem mniEditStudent;
/**
* 删除菜单
*/
private JMenu mnuDelStu;
private JMenuItem mniDelStudentById;
private JMenuItem mniDelStudentsByClass;
private JMenuItem mniDelStudentsByDepartment;
/**
* 查询菜单
*/
private JMenu mnuFind;
private JMenuItem mniFindStudentById;
private JMenuItem mniFindStudentsByName;
private JMenuItem mniFindStudentsByClass;
private JMenuItem mniFindStudentsByDepartment;
/**
* 统计菜单
*/
private JMenu mnuCount;
private JMenuItem mniCountStudentsBySex;
private JMenuItem mniCountStudentsByClass;
private JMenuItem mniCountStudentsByDepartment;
/**
* 帮助菜单
*/
private JMenu mnuHelp;
private JMenuItem mniHelp;
private JMenuItem mniAbout;
/**
* 面板
*/
private JPanel panel;
private JPanel pnlMain;
private JPanel pnlStatus;
/**
* 状态栏标签
*/
private JLabel lblSystem;
private JLabel lblAuthor;
private JLabel lblAddress;
private JLabel lblTelephone;
private JLabel lblEmail;
/**
* 背景标签
*/
private JLabel lblBackground;
/**
* 状态对象
*/
private Status status;
/**
* 图标对象
*/
private ImageIcon imgCollege;
private ImageIcon imgExit;
private ImageIcon imgPassword;
private ImageIcon imgQuery;
private ImageIcon imgBrowse;
private ImageIcon imgCount;
private ImageIcon imgBackground;
/**
* 工具栏
*/
private JToolBar toolbar;
/**
* 按钮
*/
private JButton btnSetCollege;
private JButton btnChangePassword;
private JButton btnFindStudentById;
private JButton btnExit;
private JButton btnBrowseStudent;
private JButton btnCoutByDepartment;
/**
* 状态服务对象
*/
private StatusService statusService;
/**
* 构造方法
*
* @param title
*/
public MainFrame(String title) {
super(title);
initGUI();
}
/**
* 初始化用户界面
*/
private void initGUI() {
// 创建主菜单
mnbMain = new JMenuBar();
// 创建【设置】菜单及其菜单项
mnuSet = new JMenu("系统设置[S]");
mnuSet.setMnemonic(KeyEvent.VK_S);
mniSetCollegeInfo = new JMenuItem("学校信息");
mniSetStatusBar = new JMenuItem("状态栏信息");
mniChangePassword = new JMenuItem("修改密码");
mniExit = new JMenuItem("退出系统");
// 创建【操作】菜单及其菜单项
mnuOperate = new JMenu("数据操作[O]");
mnuOperate.setMnemonic(KeyEvent.VK_O);
mniAddStudent = new JMenuItem("增加学生表记录");
mnuDelStu = new JMenu("删除学生表记录");
mniEditStudent = new JMenuItem("编辑学生表记录");
mniBrowseStudent = new JMenuItem("浏览学生表记录");
// 创建【删除学生表记录】的子菜单
mniDelStudentById = new JMenuItem("按学号删除");
mniDelStudentsByClass = new JMenuItem("按班级删除");
mniDelStudentsByDepartment = new JMenuItem("按系部删除");
// 创建【查询】菜单及其菜单项
mnuFind = new JMenu("查询学生[Q]");
mnuFind.setMnemonic(KeyEvent.VK_Q);
mniFindStudentById = new JMenuItem("按学号查询");
mniFindStudentsByName = new JMenuItem("按姓名查询");
mniFindStudentsByClass = new JMenuItem("按班级查询");
mniFindStudentsByDepartment = new JMenuItem("按系部查询");
// 创建【统计】菜单及其菜单项
mnuCount = new JMenu("人数统计[C]");
mnuCount.setMnemonic(KeyEvent.VK_C);
mniCountStudentsBySex = new JMenuItem("按性别统计");
mniCountStudentsByClass = new JMenuItem("按班级统计");
mniCountStudentsByDepartment = new JMenuItem("按系部统计");
// 创建【帮助】菜单及其菜单项
mnuHelp = new JMenu("帮助[H]");
mnuHelp.setMnemonic(KeyEvent.VK_H);
mniHelp = new JMenuItem("帮助");
mniAbout = new JMenuItem("关于");
// 创建图标对象
imgCollege = new ImageIcon("images/college.png");
imgPassword = new ImageIcon("images/password.png");
imgQuery = new ImageIcon("images/query.png");
imgBrowse = new ImageIcon("images/browse.png");
imgCount = new ImageIcon("images/count.png");
imgExit = new ImageIcon("images/exit.png");
// 创建工具栏
toolbar = new JToolBar();
btnSetCollege = new JButton("设置学校", imgCollege);
btnSetCollege.setToolTipText("设置学校信息");
btnSetCollege.setVerticalTextPosition(AbstractButton.BOTTOM);
btnSetCollege.setHorizontalTextPosition(AbstractButton.CENTER);
btnChangePassword = new JButton("修改密码", imgPassword);
btnChangePassword.setToolTipText("修改用户密码");
btnChangePassword.setVerticalTextPosition(AbstractButton.BOTTOM);
btnChangePassword.setHorizontalTextPosition(AbstractButton.CENTER);
btnBrowseStudent = new JButton("浏览学生", imgBrowse);
btnBrowseStudent.setToolTipText("浏览学生记录");
btnBrowseStudent.setVerticalTextPosition(AbstractButton.BOTTOM);
btnBrowseStudent.setHorizontalTextPosition(AbstractButton.CENTER);
btnFindStudentById = new JButton("查询学生", imgQuery);
btnFindStudentById.setToolTipText("按学号查询学生记录");
btnFindStudentById.setVerticalTextPosition(AbstractButton.BOTTOM);
btnFindStudentById.setHorizontalTextPosition(AbstractButton.CENTER);
btnCoutByDepartment = new JButton("统计人数", imgCount);
btnCoutByDepartment.setToolTipText("按系部统计学生人数");
btnCoutByDepartment.setVerticalTextPosition(AbstractButton.BOTTOM);
btnCoutByDepartment.setHorizontalTextPosition(AbstractButton.CENTER);
btnExit = new JButton("退出系统", imgExit);
btnExit.setToolTipText("退出系统");
btnExit.setVerticalTextPosition(AbstractButton.BOTTOM);
btnExit.setHorizontalTextPosition(AbstractButton.CENTER);
toolbar.add(btnSetCollege);
toolbar.add(btnChangePassword);
toolbar.add(btnBrowseStudent);
toolbar.add(btnFindStudentById);
toolbar.add(btnCoutByDepartment);
toolbar.add(btnExit);
// 创建面板
panel = (JPanel) getContentPane();
pnlMain = new JPanel();
pnlStatus = new JPanel();
pnlStatus.setLayout(new GridLayout(1, 5));
// 创建背景图片
imgBackground = new ImageIcon("images/background.jpg");
// 创建背景标签
lblBackground = new JLabel(imgBackground);
// 创建状态栏标签
lblSystem = new JLabel();
lblAuthor = new JLabel();
lblAddress = new JLabel();
lblTelephone = new JLabel();
lblEmail = new JLabel();
// 设置菜单栏
setJMenuBar(mnbMain);
// 添加【设置】菜单
mnbMain.add(mnuSet);
mnuSet.add(mniSetCollegeInfo);
mnuSet.add(mniSetStatusBar);
mnuSet.add(mniChangePassword);
mnuSet.addSeparator();
mnuSet.add(mniExit);
// 添加【删除学生表记录】菜单
mnuDelStu.add(mniDelStudentById);
mnuDelStu.add(mniDelStudentsByClass);
mnuDelStu.add(mniDelStudentsByDepartment);
// 添加【操作】菜单
mnbMain.add(mnuOperate);
mnuOperate.add(mniAddStudent);
mnuOperate.add(mniEditStudent);
mnuOperate.add(mnuDelStu);
mnuOperate.add(mniBrowseStudent);
// 添加【查询】菜单
mnbMain.add(mnuFind);
mnuFind.add(mniFindStudentById);
mnuFind.add(mniFindStudentsByName);
mnuFind.add(mniFindStudentsByClass);
mnuFind.add(mniFindStudentsByDepartment);
// 添加【统计】菜单
mnbMain.add(mnuCount);
mnuCount.add(mniCountStudentsBySex);
mnuCount.add(mniCountStudentsByClass);
mnuCount.add(mniCountStudentsByDepartment);
// 添加【帮助】菜单
mnbMain.add(mnuHelp);
mnuHelp.add(mniHelp);
mnuHelp.add(mniAbout);
// 添加面板
panel.setLayout(new BorderLayout());
panel.add(toolbar, "North");
panel.add(pnlMain, "Center");
panel.add(pnlStatus, "South");
pnlMain.add(lblBackground);
pnlStatus.add(lblSystem);
pnlStatus.add(lblAuthor);
pnlStatus.add(lblAddress);
pnlStatus.add(lblTelephone);
pnlStatus.add(lblEmail);
// 非管理员不能设置状态栏
if (!Application.username.equals("admin")) {
mniSetStatusBar.setEnabled(false);
mniAddStudent.setEnabled(false);
mnuDelStu.setEnabled(false);
mniEditStudent.setEnabled(false);
}
setStatusBar(); // 设置状态栏信息
// 设置窗口属性
setSize(800, 640);
setVisible(true);
setLocationRelativeTo(null);
// 关闭窗口单击事件
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
exitSystem();
}
});
// 设置菜单
// 【设置学校信息】菜单项单击事件
mniSetCollegeInfo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SetCollegeInformationFrame("设置学校信息");
}
});
// 【设置状态栏信息】菜单项单击事件
mniSetStatusBar.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SetStatusBarFrame("设置状态栏信息");
}
});
// 【修改密码】菜单项单击事件
mniChangePassword.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new ChangePasswordFrame("修改密码");
}
});
// 【退出系统】菜单项单击事件
mniExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
exitSystem();
}
});
// 查询菜单
// 【按学号查询】菜单项单击事件
mniFindStudentById.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FindStudentByIdFrame("按学号查询学生记录");
}
});
// 【按姓名查询】菜单项单击事件
mniFindStudentsByName.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FindStudentByNameFrame("按姓名学生记录");
}
});
// 【按班级查询】菜单项单击事件
mniFindStudentsByClass.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FindStudentsByClassFrame("按班级查询学生记录");
}
});
// 【按系部查询】菜单项单击事件
mniFindStudentsByDepartment.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FindStudentsByDepartmentFrame("按系部查询学生记录");
}
});
// 统计菜单
// 【按性别统计人数】菜单项单击事件
mniCountStudentsBySex.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new CountStudentsBySexFrame("按性别统计学生人数");
}
});
// 【按班级统计人数】菜单项单击事件
mniCountStudentsByClass.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new CountStudentsByClassFrame("按班级统计学生人数");
}
});
// 【按系部统计人数】菜单项单击事件
mniCountStudentsByDepartment.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new CountStudentsByDepartmentFrame("按系部统计学生人数");
}
});
// 【增加学生记录】菜单项单击事件
mniAddStudent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new AddStudentFrame("增加学生表记录");
}
});
// 【按学号删除学生记录】菜单项单击事件
mniDelStudentById.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new DeleteStudentByIdFrame("按学号删除学生记录");
}
});
// 【按班级删除学生记录】菜单项单击事件
mniDelStudentsByClass.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new DeleteStudentsByClassFrame("按班级删除学生记录");
}
});
// 【按系部删除学生记录】菜单项单击事件
mniDelStudentsByDepartment.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new DeleteStudentsByDepartmentFrame("按系部删除学生记录");
}
});
// 【编辑学生记录】菜单项单击事件
mniEditStudent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new EditStudentFrame("编辑学生表记录");
}
});
// 【浏览学生记录】菜单项单击事件
mniBrowseStudent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
new BrowseStudentsFrame("浏览学生表记录");
}
});
// 【帮助】菜单单击事件
mniHelp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Runtime.getRuntime().exec("cmd /c start help/帮助文档.chm");
} catch (IOException e1) {
JOptionPane.showMessageDialog(null, e1.getMessage(), "学生信息管理系统", JOptionPane.ERROR_MESSAGE);
}
}
});
// 【关于】菜单单击事件
mniAbout.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"开发人员:" + status.getCollege() + "_" + status.getAuthor() + "\n联系电话:" + status.getTelephone()
+ "\n电子邮箱:" + status.getEmail(),
"学生信息管理系统" + status.getVersion(), JOptionPane.INFORMATION_MESSAGE);
}
});
// 工具栏按钮单击事件
// 【设置学校信息】按钮
btnSetCollege.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SetCollegeInformationFrame("设置学校信息");
}
});
// 【修改密码】按钮
btnChangePassword.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new ChangePasswordFrame("修改密码");
}
});
// 【浏览】按钮
btnBrowseStudent.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new BrowseStudentsFrame("浏览学生表记录");
}
});
// 【查询】按钮
btnFindStudentById.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new FindStudentByIdFrame("按学号查询学生记录");
}
});
// 【统计】按钮
btnCoutByDepartment.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new CountStudentsByDepartmentFrame("按系部统计学生人数");
}
});
// 【退出】按钮
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
exitSystem();
}
});
}
/**
* 退出系统
*/
protected void exitSystem() {
int choice = JOptionPane.showConfirmDialog(null, "你是否要退出系统?", "学生信息管理系统", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) {
System.exit(0);
} else {
// 关闭当前窗口
dispose();
Application.mainFrame = new MainFrame("学生信息管理系统" + status.getVersion());
}
}
/**
* 设置状态栏信息
*/
public void setStatusBar() {
// 创建状态服务对象
statusService = new StatusServiceImpl();
// 获取状态栏对象
status = statusService.findStatusById(1);
// 设置状态栏标签
lblSystem.setText(status.getCollege());
lblAuthor.setText("作者:" + status.getAuthor());
lblAddress.setText("地址:" + status.getAddress());
lblTelephone.setText("电话:" + status.getTelephone());
lblEmail.setText("邮箱:" + status.getEmail());
}
}
(14)注册窗口RegisterFrame
/**
* 包名:net.hw.student.gui
* 类名:RegisterFrame
* 描述:用户注册
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.gui;
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.sql.Timestamp;
import java.util.Date;
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 net.hw.student.app.Application;
import net.hw.student.bean.User;
import net.hw.student.service.UserService;
import net.hw.student.service.impl.UserServiceImpl;
public class RegisterFrame extends JFrame {
/**
* 标签
*/
private JLabel lblUsername;
private JLabel lblPassword;
private JLabel lblTelephone;
/**
* 文本框
*/
private JTextField txtUsername;
private JTextField txtTelephone;
private JPasswordField txtPassword;
/**
* 按钮
*/
private JButton btnSubmit;
private JButton btnCancel;
private JButton btnLogin;
/**
* 面板
*/
private JPanel panel;
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
private JPanel panel4;
private String username;
private String password;
private String telephone;
private Date registerTime;
/**
* 学生管理应用程序
*/
private static Application app;
/**
* 构造方法
*
* @param title
*/
public RegisterFrame(String title) {
super(title);
// 创建学生管理应用程序
app = new Application();
initGUI();
}
/**
* 初始化用户界面
*/
private void initGUI() {
// 创建对象
lblUsername = new JLabel("用户名:");
lblPassword = new JLabel("密 码:");
lblTelephone = new JLabel("电 话:");
txtUsername = new JTextField("", 15);
txtPassword = new JPasswordField("", 15);
txtTelephone = new JTextField("", 15);
btnSubmit = new JButton("提交[S]");
btnCancel = new JButton("取消[C]");
btnLogin = new JButton("登录[L]");
panel = (JPanel) getContentPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
// 添加组件
panel.setLayout(new GridLayout(4, 1));
panel.add(panel1);
panel.add(panel2);
panel.add(panel3);
panel.add(panel4);
panel1.add(lblUsername);
panel1.add(txtUsername);
panel2.add(lblPassword);
panel2.add(txtPassword);
panel3.add(lblTelephone);
panel3.add(txtTelephone);
panel4.add(btnSubmit);
panel4.add(btnCancel);
panel4.add(btnLogin);
// 设置属性
setSize(250, 200); // 设置窗口大小
setLocationRelativeTo(null);// 让窗口居中
setResizable(false); // 窗口不可调整大小
pack(); // 使窗口恰好容纳组件
setVisible(true); // 让窗口可见
btnSubmit.setMnemonic(KeyEvent.VK_O); // 设置热键字母
btnCancel.setMnemonic(KeyEvent.VK_C);// 设置热键字母
btnLogin.setMnemonic(KeyEvent.VK_R);// 设置热键字母
txtPassword.setEchoChar('*');// 设置回显字符
/*
* 注册监听器,编写事件处理代码 采用匿名内部类方式来实现
*/
// 提交按钮单击事件
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
register();
}
});
// 提交按钮按键事件
btnSubmit.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
register();
}
}
});
// 取消按钮单击事件
btnCancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// 显示登录窗口
Application.loginFrame.setVisible(true);
// 关闭当前窗口
dispose();
}
});
// 登录按钮按键事件
btnLogin.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// 显示登录窗口
Application.loginFrame.setVisible(true);
// 关闭当前窗口
dispose();
}
});
// 用户名文本框按键事件
txtUsername.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
txtPassword.requestFocus();
}
}
});
// 密码文本框按键事件
txtPassword.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
txtTelephone.requestFocus();
}
}
});
// 电话文本框按键事件
txtTelephone.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
btnSubmit.requestFocus();
}
}
});
}
/**
* 注册方法
*/
private void register() {
// 获取用户名
username = txtUsername.getText().trim();
// 获取密码
password = new String(txtPassword.getPassword());
// 获取电话
telephone = txtTelephone.getText().trim();
// 定义当前时间为注册时间
registerTime = new Timestamp(System.currentTimeMillis());
// 定义用户服务对象
UserService userService = new UserServiceImpl();
// 创建用户
User user = new User();
user.setUsernname(username);
user.setPassword(password);
user.setTelephone(telephone);
user.setRegisterTime(registerTime);
// 添加用户
int count = userService.addUser(user);
// 判断是否添加成功
if (count > 0) {
setVisible(false);
JOptionPane.showMessageDialog(null, "恭喜!注册成功!", "学生信息管理系统", JOptionPane.INFORMATION_MESSAGE);
setVisible(true);
} else {
JOptionPane.showMessageDialog(null, "遗憾!注册失败!", "学生信息管理系统", JOptionPane.INFORMATION_MESSAGE);
}
}
}
(15)设置学校信息窗口SetCollegeInformationFrame
/**
/**
* 包名:net.hw.student.gui
* 类名:SetCollegeInformationFrame
* 描述:设置学校信息
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.gui;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import net.hw.student.bean.College;
import net.hw.student.service.CollegeService;
import net.hw.student.service.impl.CollegeServiceImpl;
public class SetCollegeInformationFrame extends JFrame {
JPanel panel;
JPanel pnlNorth, p1, p2, p3, p4, p5;
JPanel pnlCenter;
JPanel pnlSouth;
JLabel lblName;
JLabel lblPresident;
JLabel lblStartTime;
JLabel lblTelephone;
JLabel lblEmail;
JLabel lblAddress;
JLabel lblProfile;
JTextField txtName;
JTextField txtPresident;
JTextField txtStartTime;
JTextField txtTelephone;
JTextField txtEMail;
JTextField txtAddress;
JTextArea txtProfile;
JScrollPane scrollPane;
JButton btnSave;
JButton btnExit;
/**
* 学校服务
*/
private CollegeService collegeService;
/**
* 学校对象
*/
private College college;
/**
* 学校标识符
*/
private int id = 1;
/**
* 构造方法
*
* @param title
*/
public SetCollegeInformationFrame(String title) {
super(title);
// 创建学校服务对象
collegeService = new CollegeServiceImpl();
initGUI();
}
/**
* 初始化用户界面
*/
private void initGUI() {
// 创建组件
panel = (JPanel) getContentPane();
pnlNorth = new JPanel();
p1 = new JPanel();
p2 = new JPanel();
p3 = new JPanel();
p4 = new JPanel();
p5 = new JPanel();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
lblName = new JLabel("学校名称:");
lblPresident = new JLabel("校 长:");
lblStartTime = new JLabel("建校时间:");
lblTelephone = new JLabel("联系电话:");
lblEmail = new JLabel("电子邮箱:");
lblAddress = new JLabel("通讯地址:");
lblProfile = new JLabel("学校简介:");
txtName = new JTextField(30);
txtPresident = new JTextField(11);
txtStartTime = new JTextField(12);
txtTelephone = new JTextField(11);
txtEMail = new JTextField(12);
txtAddress = new JTextField(30);
txtProfile = new JTextArea(5, 37);
txtProfile.setLineWrap(true);// 让文本区自动换行
scrollPane = new JScrollPane(txtProfile, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
btnSave = new JButton("保存[S]");
btnSave.setMnemonic(KeyEvent.VK_S);
btnExit = new JButton("退出[X]");
btnExit.setMnemonic(KeyEvent.VK_X);
// 添加组件
panel.setLayout(new BorderLayout());
panel.add(pnlNorth, "North");
panel.add(pnlCenter, "Center");
panel.add(pnlSouth, "South");
pnlNorth.setLayout(new GridLayout(5, 1));
pnlNorth.add(p1);
pnlNorth.add(p2);
pnlNorth.add(p3);
pnlNorth.add(p4);
pnlNorth.add(p5);
p1.add(lblName);
p1.add(txtName);
p2.add(lblPresident);
p2.add(txtPresident);
p2.add(lblStartTime);
p2.add(txtStartTime);
p3.add(lblTelephone);
p3.add(txtTelephone);
p3.add(lblEmail);
p3.add(txtEMail);
p4.add(lblAddress);
p4.add(txtAddress);
p5.add(lblProfile);
pnlCenter.add(scrollPane);
pnlSouth.add(btnSave);
pnlSouth.add(btnExit);
// 获取学校对象
college = collegeService.findCollegeById(id);
txtName.setText(college.getName());
txtPresident.setText(college.getPresident());
txtStartTime.setText(college.getStartTime().toString());
txtTelephone.setText(college.getTelephone());
txtEMail.setText(college.getEmail());
txtAddress.setText(college.getAddress());
txtProfile.setText(college.getProfile());
// 设置窗口属性
setVisible(true);
setSize(450, 350);
setResizable(false);
pack();
setLocationRelativeTo(null);
// 给【关闭】按钮注册监听器
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
// 关闭窗口
dispose();
}
});
// 给【保存】按钮注册监听器
btnSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
try {
// 修改学校对象属性
college.setName(txtName.getText());
college.setPresident(txtPresident.getText());
college.setStartTime(new Timestamp(
new SimpleDateFormat("yyyy-MM-dd")
.parse(txtStartTime.getText().toString()).getTime()));
college.setTelephone(txtTelephone.getText());
college.setEmail(txtEMail.getText());
college.setAddress(txtAddress.getText());
college.setProfile(txtProfile.getText());
// 更新学校信息
collegeService.updateCollege(college);
// 提示用户更新成功
JOptionPane.showMessageDialog(null, "更新数据成功!");
} catch (ParseException e) {
// 提示用户更新失败
JOptionPane.showMessageDialog(null, "更新数据失败!");
}
}
});
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// 关闭窗口
dispose();
}
});
}
}
(16)设置状态栏信息窗口SetStatusBarFrame
/**
* 包名:net.hw.student.gui
* 类名:SetStatusBarFrame
* 描述:设置状态栏
* 作者:华卫
* 日期:2017年1月3日
*/
package net.hw.student.gui;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import net.hw.student.app.Application;
import net.hw.student.bean.Status;
import net.hw.student.service.StatusService;
import net.hw.student.service.impl.StatusServiceImpl;
public class SetStatusBarFrame extends JFrame {
/**
* 面板
*/
private JPanel panel;
private JPanel panel1;
private JPanel panel2;
private JPanel panel3;
private JPanel panel4;
private JPanel panel5;
/**
* 标签
*/
private JLabel lblCollege;
private JLabel lblVersion;
private JLabel lblAuthor;
private JLabel lblTelephone;
private JLabel lblAddress;
private JLabel lblEmail;
/**
* 文本框
*/
private JTextField txtCollege;
private JTextField txtVersion;
private JTextField txtAuthor;
private JTextField txtTelephone;
private JTextField txtAddress;
private JTextField txtEmail;
/**
* 按钮
*/
private JButton btnSave;
private JButton btnExit;
/**
* 状态服务对象
*/
private StatusService statusService;
/**
* 构造方法
*
* @param title
*/
public SetStatusBarFrame(String title) {
super(title);
initGUI();
}
/**
* 初始化用户界面
*/
private void initGUI() {
// 创建组件
panel = (JPanel) getContentPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
panel5 = new JPanel();
lblCollege = new JLabel("校名:");
lblVersion = new JLabel("版本:");
lblAuthor = new JLabel("作者:");
lblTelephone = new JLabel("电话:");
lblAddress = new JLabel("地址:");
lblEmail = new JLabel("邮件:");
txtCollege = new JTextField(12);
txtVersion = new JTextField(12);
txtAuthor = new JTextField(12);
txtTelephone = new JTextField(12);
txtAddress = new JTextField(29);
txtEmail = new JTextField(29);
btnSave = new JButton("保存[S]");
btnSave.setMnemonic(KeyEvent.VK_S);
btnExit = new JButton("退出[X]");
btnExit.setMnemonic(KeyEvent.VK_X);
// 添加组件
panel.setLayout(new GridLayout(5, 1));
panel.add(panel1);
panel.add(panel2);
panel.add(panel3);
panel.add(panel4);
panel.add(panel5);
panel1.add(lblCollege);
panel1.add(txtCollege);
panel1.add(lblVersion);
panel1.add(txtVersion);
panel2.add(lblAuthor);
panel2.add(txtAuthor);
panel2.add(lblTelephone);
panel2.add(txtTelephone);
panel3.add(lblAddress);
panel3.add(txtAddress);
panel4.add(lblEmail);
panel4.add(txtEmail);
panel5.add(btnSave);
panel5.add(btnExit);
// 创建状态服务对象
statusService = new StatusServiceImpl();
// 按标识符获取状态对象
Status status = statusService.findStatusById(1);
if (status != null) {
txtCollege.setText(status.getCollege());
txtVersion.setText(status.getVersion());
txtAuthor.setText(status.getAuthor());
txtAddress.setText(status.getAuthor());
txtTelephone.setText(status.getTelephone());
txtAddress.setText(status.getAddress());
txtEmail.setText(status.getEmail());
}
// 设置窗口属性
setResizable(false);
pack();
setLocationRelativeTo(null);
setVisible(true);
// 【关闭】按钮事件处理
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
dispose();
}
});
// 【保存】按钮事件处理
btnSave.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 获取状态信息对象
Status status = statusService.findStatusById(1);
// 修改属性值
status.setCollege(txtCollege.getText());
status.setVersion(txtVersion.getText());
status.setAuthor(txtAuthor.getText());
status.setTelephone(txtTelephone.getText());
status.setAddress(txtAddress.getText());
status.setEmail(txtEmail.getText());
// 更新状态记录
int count = statusService.updateStatus(status);
// 判断是否更新成功
if (count > 0) {
Application.mainFrame.setStatusBar();
Application.mainFrame.setTitle("学生信息管理系统" + status.getVersion());
}
}
});
}
}
上一篇: 学生信息管理系统(Java实训)
下一篇: B树与B+树