在我以下发布的代码中,代码很短的用图片,代码很长的就用的是代码,没有用截图,小小的偷懒一下。
第一天实训 (2019年6月26日)
java实训数据库表的截图。
IJ软件操作。
第一步
第二步
第三步
第四步
学校实体college
按alt+insert键生成
再按一次alt+insert键
如上操作编写Status实体
如上操作编写Student实体
如上操作编写User实体
连接数据库
第二天实训(2019年6月27日)
建Dao包,新建四个数据访问接口
创建四个数据访问接口实现类
第一个CollegeDaoImpl
第一个测试类TestCollegeDaoImpl
第一个test运行结果
第二个test运行结果
第三天实训(2019年6月28日)
第二个数据访问接口类StatusDaoImpl
package net.fqq.student.dao.impl;
/**
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class StatusDaoImpl implements StatusDao{
@Override
public Status findById(int id) {
// 声明状态对象
Status status = null;
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = "SELECT * FROM t_status WHERE id = ?";
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setInt(1, id);
// 5. 执行SQL查询,返回结果集
ResultSet rs = pstmt.executeQuery();
// 6. 判断结果集是否有记录
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"));
}
// 7. 关闭预备语句对象
pstmt.close();
// 8. 关闭结果集对象
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回状态对象
return status;
}
@Override
public int update(Status status) {
return 0;
}
}
第二个测试类TestStatusDaoImpl
package net.fqq.student.test;
import net.fqq.student.bean.Status;
import net.fqq.student.dao.StatusDao;
import net.fqq.student.dao.impl.StatusDaoImpl;
import org.junit.Test;
/**
package net.fqq.student.dao.impl;
import net.fqq.student.bean.Student;
import net.fqq.student.dao.StudentDao;
import net.fqq.student.dbutil.ConnectionManager;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
/**
功能:学生数据访问接口实现类
作者:冯其强
日期:2019年6月28日
/
public class StudentDaoImpl implements StudentDao {
/*
插入学生记录
@param student
@return 插入记录数
*/
@Override
public int insert(Student student) {
// 定义插入记录数
int count = 0;
// 1. 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “insert into t_student (id, name, sex, age, department, class, telephone)”
+ " values (?, ?, ?, ?, ?, ?, ?)";
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
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());
// 5. 执行SQL,返回插入记录数
count = pstmt.executeUpdate();
// 6. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回插入记录数
return count;
}
/**
按学号删除学生记录
@param id
@return 删除记录数
*/
@Override
public int deleteById(String id) {
// 定义删除记录数
int count = 0;
// 1. 获取数据库连接
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “delete from t_student where id = ?”;
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, id);
// 5. 执行SQL,返回删除记录数
count = pstmt.executeUpdate();
// 6. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回删除记录数
return count;
}
/**
按班级删除学生记录
@param clazz
@return 删除记录数
*/
@Override
public int deleteByClass(String clazz) {
// 定义删除记录数
int count = 0;
// 1. 获取数据库连接
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “delete from t_student where class = ?”;
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, clazz);
// 5. 执行SQL,返回删除记录数
count = pstmt.executeUpdate();
// 6. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回删除记录数
return count;
}
/**
按系部删除学生记录
@param department
@return 删除记录数
*/
@Override
public int deleteByDepartment(String department) {
// 定义删除记录数
int count = 0;
// 1. 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “delete from t_student where department = ?”;
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, department);
// 5. 执行SQL,返回删除记录数
count = pstmt.executeUpdate();
// 6. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回删除记录数
return count;
}
/**
更新学生记录
@param student
@return 更新记录数
*/
@Override
public int update(Student student) {
// 定义更新记录数
int count = 0;
// 1. 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “update t_student set name = ?, sex = ?, age = ?,”
+ " department = ?, class = ?, telephone = ? where id = ?";
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
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());
// 5. 执行SQL,返回更新记录数
count = pstmt.executeUpdate();
// 6. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回更新记录数
return count;
}
/**
按学号查询学生记录
@param id
@return 学生实体
*/
@Override
public Student findById(String id) {
// 声明学生对象
Student student = null;
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “select * from t_student where id = ?”;
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, id);
// 5. 执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
// 6. 判断结果集是否有记录
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;
}
/**
按姓名查询学生记录
@param name
@return 学生列表
*/
@Override
public List findByName(String name) {
// 声明学生列表
List students = new ArrayList();
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “select * from t_student where name like ?”;
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, name + “%”);
// 5. 执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
// 6. 遍历结果集
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);
}
// 7. 关闭结果集
rs.close();
// 8. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回学生列表
return students;
}
/**
按班级查询学生记录
@param clazz
@return 学生列表
*/
@Override
public List findByClass(String clazz) {
// 声明学生列表
List students = new ArrayList();
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “select * from t_student where class like ?”;
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, clazz + “%”);
// 5. 执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
// 6. 遍历结果集
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);
}
// 7. 关闭结果集
rs.close();
// 8. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回学生列表
return students;
}
/**
按系部查询学生记录
@param department
@return 学生列表
*/
@Override
public List findByDepartment(String department) {
// 声明学生列表
List students = new ArrayList();
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “select * from t_student where department like ?”;
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, department + “%”);
// 5. 执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
// 6. 遍历结果集
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);
}
// 7. 关闭结果集
rs.close();
// 8. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回学生列表
return students;
}
/**
查询全部学生记录
@return 学生列表
*/
@Override
public List findAll() {
// 声明学生列表
List students = new ArrayList();
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “select * from t_student”;
try {
// 3. 创建语句对象
Statement stmt = conn.createStatement();
// 4. 执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
// 5. 遍历结果集
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);
}
// 6. 关闭结果集
rs.close();
// 7. 关闭语句对象
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回学生列表
return students;
}
/**
按性别统计学生人数
@return 统计结果向量
*/
@Override
public Vector findRowsBySex() {
// 定义行集向量
Vector rows = new Vector();
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “select sex as ‘性别’, count(*) as ‘人数’”
+ " from t_student group by sex order by sex desc";
try {
// 3. 创建语句对象
Statement stmt = conn.createStatement();
// 4. 执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
// 5. 遍历结果集
while (rs.next()) {
// 定义当前行向量
Vector currentRow = new Vector();
// 利用当前记录字段值设置当前行向量的元素值
currentRow.addElement(rs.getString(“性别”));
currentRow.addElement(rs.getInt(“人数”) + “”);
// 将当前行向量添加到行集向量
rows.addElement(currentRow);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回行集向量
return rows;
}
/**
按班级统计学生人数
@return 统计结果向量
*/
@Override
public Vector findRowsByClass() {
// 定义行集向量
Vector rows = new Vector();
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “select class as ‘班级’, count(*) as ‘人数’”
+ " from t_student group by class order by class desc";
try {
// 3. 创建语句对象
Statement stmt = conn.createStatement();
// 4. 执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
// 5. 遍历结果集
while (rs.next()) {
// 定义当前行向量
Vector currentRow = new Vector();
// 利用当前记录字段值设置当前行向量的元素值
currentRow.addElement(rs.getString(“班级”));
currentRow.addElement(rs.getInt(“人数”) + “”);
// 将当前行向量添加到行集向量
rows.addElement(currentRow);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回行集向量
return rows;
}
/**
按系部统计学生人数
@return 统计结果向量
*/
@Override
public Vector findRowsByDepartment() {
// 定义行集向量
Vector rows = new Vector();
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “select department as ‘系部’, count(*) as ‘人数’”
+ " from t_student group by department order by department desc";
try {
// 3. 创建语句对象
Statement stmt = conn.createStatement();
// 4. 执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
// 5. 遍历结果集
while (rs.next()) {
// 定义当前行向量
Vector currentRow = new Vector();
// 利用当前记录字段值设置当前行向量的元素值
currentRow.addElement(rs.getString(“系部”));
currentRow.addElement(rs.getInt(“人数”) + “”);
// 将当前行向量添加到行集向量
rows.addElement(currentRow);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回行集向量
return rows;
}
}
第三个测试类TestStudentDaoImp
第三个测试类TestStudentDaoIm结果
第四个数据访问接口实现类UserDaoImpl
package net.fqq.student.dao.impl;
import net.fqq.student.bean.User;
import net.fqq.student.dao.UserDao;
import net.fqq.student.dbutil.ConnectionManager;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
功能:用户数据访问接口实现类
作者:冯其强
日期:2019年6月28日
/
public class UserDaoImpl implements UserDao {
/*
插入用户记录
@param user
@return 插入记录数
*/
@Override
public int insert(User user) {
// 定义插入记录数
int count = 0;
// 1. 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “insert into t_user (username, password, telephone, register_time)”
+ " values (?, ?, ?, ?)";
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.setString(3,user.getTelephone());
pstmt.setTimestamp(4, new Timestamp(user.getRegisterTime().getTime()));
// 5. 执行SQL,返回插入记录数
count = pstmt.executeUpdate();
// 6. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回插入记录数
return count;
}
/**
按id删除用户记录
@param id
@return 删除记录数
*/
@Override
public int deleteById(int id) {
// 定义删除记录数
int count = 0;
// 1. 获取数据库连接
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “delete from t_user where id = ?”;
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setInt(1, id);
// 5. 执行SQL,返回删除记录数
count = pstmt.executeUpdate();
// 6. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回删除记录数
return count;
}
/**
更新用户记录
@param user
@return 更新记录数
*/
@Override
public int update(User user) {
// 定义更新记录数
int count = 0;
// 1. 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “update t_user set username = ?, password = ?, telephone = ?,”
+ " register_time = ? where id = ?";
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getTelephone());
pstmt.setTimestamp(4, new Timestamp(user.getRegisterTime().getTime()));
pstmt.setInt(5, user.getId());
// 5. 执行SQL,返回更新记录数
count = pstmt.executeUpdate();
// 6. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回更新记录数
return count;
}
/**
按id查询用户
@param id
@return 用户实体
*/
@Override
public User findById(int id) {
// 声明用户对象
User user = null;
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “select * from t_user where id = ?”;
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setInt(1, id);
// 5. 执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
// 6. 判断结果集是否有记录
if (rs.next()) {
// 创建用户实体
user = new User();
// 利用当前记录各字段值设置用户实体属性
user.setId(rs.getInt(“id”));
user.setUsername(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;
}
/**
查询所有用户
@return 用户列表
*/
@Override
public List findAll() {
// 声明用户列表
List users = new ArrayList();
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “select * from t_user”;
try {
// 3. 创建语句对象
Statement stmt = conn.createStatement();
// 4. 执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
// 5. 遍历结果集
while (rs.next()) {
// 创建用户实体
User user = new User();
// 利用当前记录各字段值设置用户实体属性
user.setId(rs.getInt(“id”));
user.setUsername(rs.getString(“username”));
user.setPassword(rs.getString(“password”));
user.setTelephone(rs.getString(“telephone”));
user.setRegisterTime(rs.getTimestamp(“register_time”));
// 将实体添加到用户列表
users.add(user);
}
// 6. 关闭结果集
rs.close();
// 7. 关闭语句对象
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回用户列表
return users;
}
/**
用户登录
@param username
@param password
@return 登录用户实体
*/
@Override
public User login(String username, String password) {
// 声明用户对象
User user = null;
// 1. 获取数据库连接
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “select * from t_user where username = ? and password = ?”;
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, username);
pstmt.setString(2, password);
// 5. 执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
// 6. 判断结果集是否有记录
if (rs.next()) {
// 实例化用户
user = new User();
// 利用当前记录各字段值设置用户实体属性
user.setId(rs.getInt(“id”));
user.setUsername(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;
}
}
第四个测试类TestUserDaoImpl
第四个测试类TestUserDaoImpl结果
第四天(2019年6月29日)
一、服务接口
(1)学校服务接口CollegeService
(2)状态服务接口StatusService
(3)学生服务接口StudentService
(4)用户服务接口UserService
二、服务接口实现类
(1)学校服务接口实现类CollegeServiceImpl
在net.fqq.student.test包里创建测试类TestCollegeServiceImpl:
测试类TestCollegeServiceImpl结果:
(2)状态服务接口实现类StatusServiceImpl
在net.fqq.student.test包里创建测试类TestStatusServiceImpl:
测试类TestStatusServiceImpl结果:
没有运行成功,不知道是什么原因。
(3)学生服务接口实现类StudentServiceImpl
在net.hw.student.test包里创建测试类TestStudentServiceImpl:
测试类TestStudentServiceImpl结果:
其余测试方法没有做,做不来。
在net.hw.student.test包里创建测试类TestUserServiceImpl:
测试类TestUserServiceImpl结果1:
测试类TestUserServiceImpl结果2:
到这里这个学生管理系统告一段落,下一周,嗯~~,就是7月1日,做图形界面,进入新一轮敲代码,哎!
第五天(2019年7月1日)
一、应用程序类
二、窗口界面类(JFrame)
1.主界面窗口MainFrame
这是第五天和第七天一起写的代码
package net.fqq.student.gui;
import net.fqq.student.app.Application;
import net.fqq.student.bean.Status;
import net.fqq.student.service.StatusService;
import net.fqq.student.service.impl.StatusServiceImpl;
import javax.swing.;
import java.awt.;
import java.awt.event.*;
import java.io.IOException;
/**
功能:主界面窗口
通过菜单系统
调用各个功能模块
作者:冯其强
日期:2019年7月1日
/
public class MainFrame extends JFrame {
/*
/**
/**
/**
初始化图形用户界面
*/
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);
btnCountByDepartment = new JButton(“统计人数”, imgCount);
btnCountByDepartment.setToolTipText(“按系部统计学生人数”);
btnCountByDepartment.setVerticalTextPosition(AbstractButton.BOTTOM);
btnCountByDepartment.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(btnCountByDepartment);
toolbar.add(btnExit);
// 创建面板
panel = (JPanel) getContentPane();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
pnlSouth.setLayout(new FlowLayout(FlowLayout.LEFT));
// 创建背景图片
imgBackground = new ImageIcon(“images/background.jpg”);
// 创建背景标签
lblBackground = new JLabel(imgBackground);
// 创建状态栏标签
lblStatusBar = 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(pnlCenter, “Center”);
panel.add(pnlSouth, “South”);
pnlCenter.add(lblBackground);
pnlSouth.add(lblStatusBar);
// 非管理员不能设置状态栏
if (!Application.username.equals(“admin”)) {
mniSetStatusBar.setEnabled(false);
mniAddStudent.setEnabled(false);
mnuDelStu.setEnabled(false);
mniEditStudent.setEnabled(false);
}
// 设置状态栏信息
setStatusBar();
// 创建状态服务对象
statusService = new StatusServiceImpl();
// 获取状态对象
status = statusService.findStatusById(1);
// 设置窗口尺寸
setSize(800, 640);
// 设置窗口可见
setVisible(true);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口标题
setTitle(“学生信息管理系统” + status.getVersion());
// 注册窗口监听器,继承窗口适配器,编写事件处理方法
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
exitSystem();
}
});
// 设置菜单
// 【设置学校信息】菜单项单击事件
mniSetCollegeInfo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SetCollegeInfoFrame("");
}
});
// 【设置状态栏信息】菜单项单击事件
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 FindStudentsByNameFrame("");
}
});
// 【按班级查询】菜单项单击事件
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 SetCollegeInfoFrame("");
}
});
// 【修改密码】按钮
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("");
}
});
// 【统计】按钮
btnCountByDepartment.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new CountStudentsByDepartmentFrame("");
}
});
// 【退出】按钮
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
exitSystem();
}
});
}
/**
/**
/**
运行效果最后一起发
2.用户注册窗口RegisterFrame
package net.fqq.student.gui;
import net.fqq.student.app.Application;
import net.fqq.student.bean.User;
import net.fqq.student.service.UserService;
import net.fqq.student.service.impl.UserServiceImpl;
import javax.swing.;
import java.awt.;
import java.awt.event.*;
import java.sql.Timestamp;
import java.util.Date;
/**
功能:注册窗口
作者:冯其强
日期:2019年7月1日
/
public class RegisterFrame extends JFrame {
/*
private String username;
private String password;
private String telephone;
private Date registerTime;
/**
/**
初始化图形用户界面
*/
private void initGUI() {
// 创建组件(面板与控件)
panel = (JPanel) getContentPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
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]”);
// 将控件添加到四个小面板
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);
// 设置大面板布局,将四个小面板添加到大面板
panel.setLayout(new GridLayout(4, 1));
panel.add(panel1);
panel.add(panel2);
panel.add(panel3);
panel.add(panel4);
// 设置窗口大小
setSize(250, 200);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口刚好容纳组件
pack();
// 设置窗口标题
setTitle(“用户注册”);
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作(卸载当前窗口)
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// 注册窗口监听器
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// 卸载当前窗口——注册窗口
dispose();
// 让登录窗口可见
Application.loginFrame.setVisible(true);
}
});
// 【提交】按钮单击事件处理
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
register();
}
});
// 【提交】按钮按键事件处理
btnSubmit.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
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() == KeyEvent.VK_ENTER) {
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() == KeyEvent.VK_ENTER) {
btnSubmit.requestFocus();
}
}
});
}
/**
注册方法
*/
private void register() {
// 获取用户名
username = txtUsername.getText().trim();
// 获取密码
password = new String(txtPassword.getPassword());
// 获取电话
telephone = txtTelephone.getText().trim();
// 定义当前时间为注册时间
registerTime = new Timestamp(System.currentTimeMillis());
// 对用户名进行非空校验
if (username.equals("")) {
JOptionPane.showMessageDialog(null, “用户名不能为空!”, “学生信息管理系统”, JOptionPane.WARNING_MESSAGE);
txtUsername.requestFocus();
return;
}
// 对密码进行非空校验
if (txtPassword.equals("")) {
JOptionPane.showMessageDialog(null, “密码不能为空!”, “学生信息管理系统”, JOptionPane.WARNING_MESSAGE);
txtPassword.requestFocus();
return;
}
// 定义用户服务对象
UserService userService = new UserServiceImpl();
// 创建用户
User user = new User();
user.setUsername(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);
}
}
/**
效果如下:
3、用户登录窗口LoginFrame
package net.fqq.student.gui;
import net.fqq.student.app.Application;
import net.fqq.student.bean.Status;
import net.fqq.student.bean.User;
import net.fqq.student.service.StatusService;
import net.fqq.student.service.UserService;
import net.fqq.student.service.impl.StatusServiceImpl;
import net.fqq.student.service.impl.UserServiceImpl;
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
/**
功能:登录窗口
作者:冯其强
日期:2019年7月1日
*/
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, panel1, panel2, panel3;
/**
/**
初始化图形用户界面
*/
private void initGUI() {
// 实例化组件(面板与控件)
panel = (JPanel) getContentPane();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
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]”);
// 将控件添加到三个小面板
panel1.add(lblUsername);
panel1.add(txtUsername);
panel2.add(lblPassword);
panel2.add(txtPassword);
panel3.add(btnOK);
panel3.add(btnCancel);
panel3.add(btnRegister);
// 设置主面板为三行一列的网格布局
panel.setLayout(new GridLayout(3, 1));
// 将三个小面板依次添加到主面板
panel.add(panel1);
panel.add(panel2);
panel.add(panel3);
// 设置按钮热键字母
btnOK.setMnemonic(KeyEvent.VK_O);
btnCancel.setMnemonic(KeyEvent.VK_C);
btnRegister.setMnemonic(KeyEvent.VK_R);
// 设置密码框回显字符
txtPassword.setEchoChar(’*’);
// 设置窗口大小
setSize(250, 200);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口刚好容纳组件
pack();
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 【确定】按钮单击事件处理
btnOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
login();
}
});
// 【确定】按钮按键事件处理
btnOK.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
// 按下回车键,调用登录方法
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
login();
}
}
});
// 【用户名】文本框按键事件处理
txtUsername.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
// 判断是否按下回车键
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
// 让密码文本框获取焦点
txtPassword.requestFocus();
}
}
});
// 【注册】按钮单击事件处理
btnRegister.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 隐藏登录窗口
Application.loginFrame.setVisible(false);
// 创建注册窗口
Application.registerFrame = new RegisterFrame(“用户注册”);
}
});
// 【密码】文本框按键事件处理
txtPassword.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
// 判断是否按了回车键
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
login();
}
}
});
// 【取消】按钮单击事件处理
btnCancel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
/**
登录方法
*/
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();
// 获取状态对象
Status status = statusService.findStatusById(1);
// 保存登录用户的信息
Application.id = user.getId();
Application.username = user.getUsername();
Application.password = user.getPassword();
// 提示用户登录成功
JOptionPane.showMessageDialog(null,
“欢迎使用学生信息管理系统” + status.getVersion(),
“用户登录”, JOptionPane.INFORMATION_MESSAGE);
// 显示系统主窗口
Application.mainFrame = new MainFrame("");
// 释放登录窗口
Application.loginFrame.dispose();
} else {
// 隐藏登录窗口
Application.loginFrame.setVisible(false);
// 提示用户登录失败,要重新输入用户名或密码
JOptionPane.showMessageDialog(null,
“用户名或密码错误,请重新输入!”,
“用户登录”, JOptionPane.ERROR_MESSAGE);
// 显示登录窗口
Application.loginFrame.setVisible(true);
// 用户名文本框内容全选
txtUsername.selectAll();
// 密码文本框内容全选
txtPassword.selectAll();
// 用户名文本框获取焦点
txtUsername.requestFocus();
}
}
/**
第六天(2019年7月2日)
4.设置学校信息窗口SetCollegeInfoFrame
package net.fqq.student.gui;
import net.fqq.student.bean.College;
import net.fqq.student.service.CollegeService;
import net.fqq.student.service.impl.CollegeServiceImpl;
import javax.swing.;
import java.awt.;
import java.awt.event.*;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
/**
功能:设置学校信息
作者:冯其强
日期:2019年7月2日
/
public class SetCollegeInfoFrame extends JFrame {
/*
/**
/**
初始化用户界面
*/
private void initGUI() {
// 创建组件(面板与控件)
panel = (JPanel) getContentPane();
pnlNorth = new JPanel();
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
panel5 = 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(panel1);
pnlNorth.add(panel2);
pnlNorth.add(panel3);
pnlNorth.add(panel4);
pnlNorth.add(panel5);
// 将控件依次添加到五个小面板
panel1.add(lblName);
panel1.add(txtName);
panel2.add(lblPresident);
panel2.add(txtPresident);
panel2.add(lblStartTime);
panel2.add(txtStartTime);
panel3.add(lblTelephone);
panel3.add(txtTelephone);
panel3.add(lblEmail);
panel3.add(txtEMail);
panel4.add(lblAddress);
panel4.add(txtAddress);
panel5.add(lblProfile);
// 将滚动面板添加到中面板
pnlCenter.add(scrollPane);
// 将两个按钮添加到南面板
pnlSouth.add(btnSave);
pnlSouth.add(btnExit);
// 创建学校服务对象
collegeService = new CollegeServiceImpl();
// 获取学校对象
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());
// 设置窗口大小
setSize(450, 350);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口刚好容纳组件
pack();
// 设置窗口标题
setTitle(“设置学校信息”);
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作(卸载当前窗口)
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// 【关闭】按钮单击事件处理
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, “更新数据失败!”);
}
}
});
}
/**
效果如下:
5.修改用户密码窗口ChangePasswordFrame
package net.fqq.student.gui;
import net.fqq.student.app.Application;
import net.fqq.student.bean.User;
import net.fqq.student.service.UserService;
import net.fqq.student.service.impl.UserServiceImpl;
import javax.swing.;
import java.awt.;
import java.awt.event.*;
/**
功能:修改用户密码
作者:冯其强
日期:2019年7月2日
/
public class ChangePasswordFrame extends JFrame {
/*
/**
/**
初始化用户界面
*/
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);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口刚好容纳组件
pack();
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口标题
setTitle(“修改用户密码”);
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作(卸载当前窗口)
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// 设置控件属性
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();
}
});
// 【确定】按钮按键事件处理
btnOK.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 10) {
changePassword();
}
}
});
// 【用户名】文本框按键事件处理
txtUsername.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtPassword.requestFocus();
}
}
});
// 【密码】文本框按键事件处理
txtPassword.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtNewPassword1.requestFocus();
}
}
});
// 【新密码1】文本框按键事件处理
txtNewPassword1.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtNewPassword2.requestFocus();
}
}
});
// 【新密码2】文本框按键事件处理
txtNewPassword2.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
btnOK.requestFocus();
}
}
});
}
/**
修改密码方法
*/
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);
// 修改用户的密码属性
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);
}
}
}
/**
6.浏览学生窗口BrowseStudentsFrame
package net.fqq.student.gui;
import net.fqq.student.bean.Student;
import net.fqq.student.service.StudentService;
import net.fqq.student.service.impl.StudentServiceImpl;
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.List;
/**
功能:浏览学生记录
作者:冯其强
日期:2019年7月2日
/
public class BrowseStudentsFrame extends JFrame {
/*
/**
/**
/**
初始化用户界面
*/
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);
// 设置窗口默认关闭操作(卸载当前窗口)
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// 创建学生服务对象
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();
}
});
}
/**
/**
7.添加学生窗口AddStudentFrame
package net.fqq.student.gui;
import net.fqq.student.bean.Student;
import net.fqq.student.service.StudentService;
import net.fqq.student.service.impl.StudentServiceImpl;
import javax.swing.;
import java.awt.;
import java.awt.event.*;
import java.util.List;
/**
功能:添加学生记录
作者:冯其强
日期:2019年7月2日
/
public class AddStudentFrame extends JFrame {
/*
/**
/**
初始化用户界面
*/
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);
// 设置窗口属性
setSize(500, 300);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口刚好容纳组件
pack();
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口标题
setTitle(“添加学生记录”);
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作(卸载当前窗口)
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// 【确定】按钮单击事件
btnOK.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// 学号非空校验
if (txtId.getText().trim().equals("")) {
JOptionPane.showMessageDialog(null, “学号不能为空!”, “增加学生记录”, JOptionPane.WARNING_MESSAGE);
txtId.requestFocus();
return;
}
// 年龄数字校验
if (!isNumber(txtAge.getText().trim())) {
JOptionPane.showMessageDialog(null, "注意:年龄全由数字构成!", "增加学生记录", JOptionPane.WARNING_MESSAGE);
txtAge.setText("");
txtAge.requestFocus();
return;
}
// 创建学生实体
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);
}
}
});
// 【取消】按钮单击事件处理
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() == KeyEvent.VK_ENTER) {
if (!txtId.getText().trim().equals("")) {
txtName.requestFocus();
}
}
}
});
// 【姓名】文本框按键事件处理
txtName.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtSex.requestFocus();
}
}
});
// 【性别】文本框按键事件处理
txtSex.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtAge.requestFocus();
}
}
});
// 【年龄】文本框按键事件处理
txtAge.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtDepartment.requestFocus();
}
}
});
// 【系部】文本框按键事件处理
txtDepartment.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtClass.requestFocus();
}
}
});
// 【班级】文本框按键事件处理
txtClass.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtTelephone.requestFocus();
}
}
});
}
/**
/**
8.编辑学生窗口EditStudentFrame
package net.fqq.student.gui;
import net.fqq.student.bean.Student;
import net.fqq.student.service.StudentService;
import net.fqq.student.service.impl.StudentServiceImpl;
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.List;
/**
功能:编辑学生记录
作者:冯其强
日期:2019年7月2日
/
public class EditStudentFrame extends JFrame {
/*
/**
/**
初始化用户界面
*/
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);
// 设置窗口默认关闭操作(卸载当前窗口)
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// 创建学生服务对象
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) {
// 设置窗口标题
setTitle(“编辑学生表记录” + " && 当前记录:" + currentRow);
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();
// 设置窗口标题
setTitle("浏览学生表记录" + " && 当前记录:" + currentRow);
} 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() == KeyEvent.VK_ENTER) {
txtName.requestFocus();
}
}
});
// 【姓名】文本框按键事件
txtName.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtSex.requestFocus();
}
}
});
// 【性别】文本框按键事件
txtSex.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtAge.requestFocus();
}
}
});
// 【年龄】文本框按键事件
txtAge.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtDepartment.requestFocus();
}
}
});
// 【系部】文本框按键事件
txtDepartment.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtClass.requestFocus();
}
}
});
// 【班级】文本框按键事件
txtClass.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
txtTelephone.requestFocus();
}
}
});
}
/**
// 判断一个字符串是否全是数字
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;
}
/**
/**
第七天 (2019年7月2日)
这是最后一天做实训,明天就要上台讲了,但是我一点都不慌,因为我做完了的,所以说我心里面一点也不慌,哈哈哈!
9.按学号删除学生窗口DeleteStudentByIdFrame
package net.fqq.student.gui;
import net.fqq.student.bean.Student;
import net.fqq.student.service.StudentService;
import net.fqq.student.service.impl.StudentServiceImpl;
import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.;
import java.awt.event.*;
import java.util.Vector;
import java.util.List;
/**
功能:按学号删除学生记录
作者:冯其强
日期:2019年7月3日
/
public class DeleteStudentByIdFrame extends JFrame {
/*
/**
/**
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, 350);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口标题
setTitle("按学号删除学生记录");
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作(卸载当前窗口)
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// 【退出】按钮单击事件
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() == KeyEvent.VK_ENTER) {
doQuery();
}
}
});
// 表格单击事件
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 fillTableData() {
// 填充表头
colHead.clear();
colHead.add(“学号”);
colHead.add(“姓名”);
colHead.add(“性别”);
colHead.add(“年龄”);
colHead.add(“系部”);
colHead.add(“班级”);
colHead.add(“电话”);
// 填充表记录
rows.clear();
for (Student student : students) {
Vector currentRow = new Vector();
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);
}
}
/**
效果如下:
10.按班级删除学生窗口DeleteStudentsByClass
package net.fqq.student.gui;
import net.fqq.student.bean.Student;
import net.fqq.student.service.StudentService;
import net.fqq.student.service.impl.StudentServiceImpl;
import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.;
import java.awt.event.*;
import java.util.Vector;
import java.util.List;
/**
功能:按班级删除学生记录
作者:冯其强
日期:2019年7月3日
/
public class DeleteStudentsByClassFrame extends JFrame {
/*
/**
/**
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, 350);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口标题
setTitle("按班级删除学生记录");
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作(卸载当前窗口)
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// 【退出】按钮单击事件
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() == KeyEvent.VK_ENTER) {
doQuery();
}
}
});
// 表格单击事件
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// 获取当前行的行数
int row = table.rowAtPoint(e.getPoint());
if (row <= table.getRowCount()) {
// 选中鼠标单击的行
table.setRowSelectionInterval(row, row);
// 设置文本框内容
txtClass.setText(table.getValueAt(row, 5).toString());
}
}
});
}
/**
/**
填充表格方法
*/
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 currentRow = new Vector();
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.按系部删除学生窗口DeleteStudentsByDepartment
package net.fqq.student.gui;
import net.fqq.student.bean.Student;
import net.fqq.student.service.StudentService;
import net.fqq.student.service.impl.StudentServiceImpl;
import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.;
import java.awt.event.*;
import java.util.Vector;
import java.util.List;
/**
功能:按系部删除学生记录
作者:冯其强
日期:2019年7月3日
/
public class DeleteStudentsByDepartmentFrame extends JFrame {
/*
/**
/**
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, 350);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口标题
setTitle("按系部删除学生记录");
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作(卸载当前窗口)
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// 【退出】按钮单击事件
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() == KeyEvent.VK_ENTER) {
doQuery();
}
}
});
// 表格单击事件
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 fillTableData() {
// 填充表头
colHead.clear();
colHead.add(“学号”);
colHead.add(“姓名”);
colHead.add(“性别”);
colHead.add(“年龄”);
colHead.add(“系部”);
colHead.add(“班级”);
colHead.add(“电话”);
// 填充表记录
rows.clear();
for (Student student : students) {
Vector currentRow = new Vector();
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.按学号查询学生窗口FindStudentById
package net.fqq.student.gui;
import net.fqq.student.bean.Student;
import net.fqq.student.service.StudentService;
import net.fqq.student.service.impl.StudentServiceImpl;
import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.;
import java.awt.event.*;
import java.awt.print.PrinterException;
import java.util.Vector;
import java.util.List;
/**
功能:按学号查询学生记录
作者:冯其强
日期:2019年7月2日
/
public class FindStudentByIdFrame extends JFrame {
/*
/**
/**
初始化用户界面
*/
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, 350);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口标题
setTitle(“按学号查询学生记录”);
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作(卸载当前窗口)
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// 【退出】按钮单击事件
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() == KeyEvent.VK_ENTER) {
doQuery();
}
}
});
// 表格单击事件
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 fillTableData() {
// 填充表头
colHead.clear();
colHead.add(“学号”);
colHead.add(“姓名”);
colHead.add(“性别”);
colHead.add(“年龄”);
colHead.add(“系部”);
colHead.add(“班级”);
colHead.add(“电话”);
// 填充表记录
rows.clear();
for (Student student : students) {
Vector currentRow = new Vector();
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);
}
}
/**
效果如下:
13.按姓名查询学生窗口FindStudentsByName
package net.fqq.student.gui;
/**
import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.;
import java.awt.event.*;
import java.awt.print.PrinterException;
import java.util.Vector;
import java.util.List;
public class FindStudentsByNameFrame 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 colHead;
/**
* 表格
*/
private JTable table;
/**
* 滚动面板
*/
private JScrollPane scroller;
/**
* 当前记录行号
*/
private int currentRow;
/**
* 学生列表
*/
private List students;
/**
* 创建学生服务对象
*/
private StudentService studentService;
/**
* 构造方法
*
* @param title
*/
public FindStudentsByNameFrame(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, 350);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口标题
setTitle("按姓名查询学生记录");
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作(卸载当前窗口)
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// 【退出】按钮单击事件
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() == KeyEvent.VK_ENTER) {
doQuery();
}
}
});
// 表格单击事件
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.clear();
colHead.add("姓名");
colHead.add("姓名");
colHead.add("性别");
colHead.add("年龄");
colHead.add("系部");
colHead.add("班级");
colHead.add("电话");
// 填充表记录
rows.clear();
for (Student student : students) {
Vector currentRow = new Vector();
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);
}
}
/**
* 主方法
*
* @param args
*/
public static void main(String[] args) {
new FindStudentsByNameFrame("");
}
}
效果如下:
14.按班级查询学生窗口FindStudentsByClass
package net.fqq.student.gui;
import net.fqq.student.bean.Student;
import net.fqq.student.service.StudentService;
import net.fqq.student.service.impl.StudentServiceImpl;
import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.;
import java.awt.event.*;
import java.awt.print.PrinterException;
import java.util.Vector;
import java.util.List;
/**
功能:按班级查询学生记录
作者:冯其强
日期:2019年7月3日
/
public class FindStudentsByClassFrame extends JFrame {
/*
/**
/**
/**
初始化用户界面
*/
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, 350);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口标题
setTitle(“按姓名查询学生记录”);
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作(卸载当前窗口)
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// 【退出】按钮单击事件
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() == KeyEvent.VK_ENTER) {
doQuery();
}
}
});
// 表格单击事件
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 fillTableData() {
// 填充表头
colHead.clear();
colHead.add(“班级”);
colHead.add(“班级”);
colHead.add(“性别”);
colHead.add(“年龄”);
colHead.add(“系部”);
colHead.add(“班级”);
colHead.add(“电话”);
// 填充表记录
rows.clear();
for (Student student : students) {
Vector currentRow = new Vector();
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);
}
}
/**
效果如下:
15.按系部查询学生窗口FindStudentsByDepartment
package net.fqq.student.gui;
import net.fqq.student.bean.Student;
import net.fqq.student.service.StudentService;
import net.fqq.student.service.impl.StudentServiceImpl;
import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.;
import java.awt.event.*;
import java.awt.print.PrinterException;
import java.util.Vector;
import java.util.List;
/**
功能:按系部查询学生记录
作者:冯其强
日期:2019年7月3日
/
public class FindStudentsByDepartmentFrame extends JFrame {
/*
/**
/**
/**
初始化用户界面
*/
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, 350);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口标题
setTitle(“按系部查询学生记录”);
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作(卸载当前窗口)
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// 【退出】按钮单击事件
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() == KeyEvent.VK_ENTER) {
doQuery();
}
}
});
// 表格单击事件
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 fillTableData() {
// 填充表头
colHead.clear();
colHead.add(“系部”);
colHead.add(“系部”);
colHead.add(“性别”);
colHead.add(“年龄”);
colHead.add(“系部”);
colHead.add(“系部”);
colHead.add(“电话”);
// 填充表记录
rows.clear();
for (Student student : students) {
Vector currentRow = new Vector();
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);
}
}
/**
效果如下:
16.按性别统计人数窗口CountStudentsBySex
package net.fqq.student.gui;
import net.fqq.student.service.StudentService;
import net.fqq.student.service.impl.StudentServiceImpl;
import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.;
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 java.util.List;
/**
功能:按性别统计学生人数
作者:冯其强
日期:2019年7月3日
/
public class CountStudentsBySexFrame extends JFrame {
/*
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);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口标题
setTitle(“按性别统计学生人数”);
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作(卸载当前窗口)
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// 【退出】按钮单击事件
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();
}
}
});
}
/**
效果如下:
17.按班级统计人数窗口CountStudentsByClass
package net.fqq.student.gui;
import net.fqq.student.service.StudentService;
import net.fqq.student.service.impl.StudentServiceImpl;
import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.print.PrinterException;
import java.util.Vector;
/**
功能:按班级统计学生人数
作者:冯其强
日期:2019年7月3日
/
public class CountStudentsByClassFrame extends JFrame {
/*
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);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口标题
setTitle(“按班级统计学生人数”);
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作(卸载当前窗口)
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// 【退出】按钮单击事件
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();
}
}
});
}
/**
效果如下:
18.按系部统计人数窗口CountStudentsByDepartment
package net.fqq.student.gui;
import net.fqq.student.service.StudentService;
import net.fqq.student.service.impl.StudentServiceImpl;
import javax.swing.;
import javax.swing.border.TitledBorder;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.print.PrinterException;
import java.util.Vector;
/**
功能:按系部统计学生人数
作者:冯其强
日期:2019年7月3日
/
public class CountStudentsByDepartmentFrame extends JFrame {
/*
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);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口标题
setTitle(“按系部统计学生人数”);
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作(卸载当前窗口)
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// 【退出】按钮单击事件
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();
}
}
});
}
/**
效果如下:
19.设置状态栏信息窗口SetStatusBarFrame
package net.fqq.student.gui;
import net.fqq.student.app.Application;
import net.fqq.student.bean.Status;
import net.fqq.student.service.StatusService;
import net.fqq.student.service.impl.StatusServiceImpl;
import javax.swing.;
import java.awt.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
/**
功能:设置状态栏信息
作者:冯其强
日期:2019年7月3日
/
public class SetStatusBarFrame extends JFrame {
/*
/**
/**
初始化用户界面
*/
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());
}
// 设置窗口大小
setSize(450, 350);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口不可调整大小
setResizable(false);
// 设置窗口刚好容纳组件
pack();
// 设置窗口标题
setTitle(“设置状态栏信息”);
// 设置窗口可见
setVisible(true);
// 设置窗口默认关闭操作(卸载当前窗口)
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// 【关闭】按钮事件处理
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());
}
}
});
}
}
20.系统主窗口MainFrame(V1.1)
package net.fqq.student.gui;
import net.fqq.student.app.Application;
import net.fqq.student.bean.Status;
import net.fqq.student.service.StatusService;
import net.fqq.student.service.impl.StatusServiceImpl;
import javax.swing.;
import java.awt.;
import java.awt.event.*;
import java.io.IOException;
/**
功能:主界面窗口
通过菜单系统
调用各个功能模块
作者:冯其强
日期:2019年7月1日
/
public class MainFrame extends JFrame {
/*
/**
/**
/**
初始化图形用户界面
*/
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);
btnCountByDepartment = new JButton(“统计人数”, imgCount);
btnCountByDepartment.setToolTipText(“按系部统计学生人数”);
btnCountByDepartment.setVerticalTextPosition(AbstractButton.BOTTOM);
btnCountByDepartment.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(btnCountByDepartment);
toolbar.add(btnExit);
// 创建面板
panel = (JPanel) getContentPane();
pnlCenter = new JPanel();
pnlSouth = new JPanel();
pnlSouth.setLayout(new FlowLayout(FlowLayout.LEFT));
// 创建背景图片
imgBackground = new ImageIcon(“images/background.jpg”);
// 创建背景标签
lblBackground = new JLabel(imgBackground);
// 创建状态栏标签
lblStatusBar = 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(pnlCenter, “Center”);
panel.add(pnlSouth, “South”);
pnlCenter.add(lblBackground);
pnlSouth.add(lblStatusBar);
// 非管理员不能设置状态栏
if (!Application.username.equals(“admin”)) {
mniSetStatusBar.setEnabled(false);
mniAddStudent.setEnabled(false);
mnuDelStu.setEnabled(false);
mniEditStudent.setEnabled(false);
}
// 设置状态栏信息
setStatusBar();
// 创建状态服务对象
statusService = new StatusServiceImpl();
// 获取状态对象
status = statusService.findStatusById(1);
// 设置窗口尺寸
setSize(800, 640);
// 设置窗口可见
setVisible(true);
// 设置窗口屏幕居中
setLocationRelativeTo(null);
// 设置窗口标题
setTitle(“学生信息管理系统” + status.getVersion());
// 注册窗口监听器,继承窗口适配器,编写事件处理方法
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
exitSystem();
}
});
// 设置菜单
// 【设置学校信息】菜单项单击事件
mniSetCollegeInfo.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new SetCollegeInfoFrame("");
}
});
// 【设置状态栏信息】菜单项单击事件
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 FindStudentsByNameFrame("");
}
});
// 【按班级查询】菜单项单击事件
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 SetCollegeInfoFrame("");
}
});
// 【修改密码】按钮
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("");
}
});
// 【统计】按钮
btnCountByDepartment.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new CountStudentsByDepartmentFrame("");
}
});
// 【退出】按钮
btnExit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
exitSystem();
}
});
}
/**
/**
/**
好的,这个学生信息管理系统分到这里就结束了,做完这个系统,感觉是眼酸手痛,但是很有成就感,虽然说有一些不是自己敲的,但大部分还是自己敲的,感觉很不错!