java学生管理系统源码
更新时间:2024-07-10 00:18:01 阅读量: 综合文库 文档下载
1. 项目内容及知识应用
1.1项目完成内容
? 首先实现登录模块,分为教室登录和学生登录
? 登录界面有一个下拉条分别为学生和教师,选中哪一个就会登录到相应的
界面
? 学生管理系统分为两大块:学生和教师两个模块 ? 学生登录后进入学生界面,学生界面有菜单项和按钮
? 学生界面里面有查询自己的成绩,查询自己的详细信息,还可以修改密码,
修改自己的信息,菜单项还有帮助等按钮
? 教师界面里面可以增加学生,显示所有学生的成绩,查找学生,查询学生
的详细信息,删除学生的成绩和删除学生,还有刷新和退出等功能 ? 教室界面当点击选中学生时在上面的标签面板会显示学生的详细信息,当
查询学生的详细信息时会之间进入该学生的学生面板,教师就可以对学生的信息进行修改
? 刚登录教师界面时系统启用了另外一个线程,会默认加载所有的学生成绩
和信息,当教师点击“显示所有成绩”时就会马上把学生成绩的面板显示在界面上,所有教师就无需等待系统加载
? 教师界面当老师点击刷新时,会再启动一个线程来重新把学生的信息加载
到内存然后显示到界面上
? 教师界面的成绩表中还有总分和平均分,系统加载的时候会自动计算每一
个学生的总分和平均分
? 教师还可以直接在成绩表中修改学生的成绩,用以来会很方便,修改成绩
后要点击刷新按钮来刷新总分和平均分
? 教室界面还增加了班级的分类,可以查看所有的班级,也可以查看一个班级
的学生信息
? 学生管理系统还增加了上传照片的功能,每个学生都可以上传自己的照片,
老师也可以帮学生上传照片
1.2知识(点)应用以及经验总结
? 该学生管理系统采用了分层的思想而且参考了MVC设计模式,但是都做得
不够完美。
? 该系统分为业务逻辑层、实体层和显示层:
? 业务逻辑层(com.ccniit.student.manage)有StudentManage、GradeManage、
PasswordNotFountException
、
StduentNotFountException
和
GradeNotFountException。先写了一个工具类(DBConnection),负责与数据库的连接,业务逻辑层联系数据库实体、显示层。进行综合的控制与处理。这里自定义了几个异常类,主要是抛出无法找到相应类的异常。这样就可以返回多个结果。
? 实体层(com.ccniit.student)有两个类Student和Grade,是根据数据库里
面的表来实现的,字段就是实体里面的属性还有所有属性的get和set方法,也可以写几个构造方法,没有其他的方法。业务逻辑层就操作这两个类。
? 显示层(com.ccniit.student.view)有Login、StduentGrade、Teacher、
ImagePanel、ShowImage、Uploading这六个类。主要是负责显示给客户,用于和客户交互信息。所有学生成绩用一个表来显示,Teacher类有另外一个线程来读取所有学生的成绩,在表中教师可以直接在表中修改学生的成绩,修改过后系统会自动修改数据库的数据,来实现动态修改。 ? 显示层主要是添加的内部匿名监听,由于会有重复监听所以大部分监听
都分别写了一个方法,来调用这个方法。
? 每个学生都可以上传自己的照片,在有照片的情况下,老师可以看到学
生的照片这样可以让老师更快的认识每一个学生,点击学生的照片可以放大该学生的照片,点击别的地方该放大的照片就会消失
? 该系统主要用到的知识有:
? 流程控制 ? 类和对象 ? 类的继承 ? 接口和多态
? 数组 ? 顺序表 ? 数据库编程 ? 常用的GUI组件 ? JTable ? 多线程 ? 事件处理
2. 项目实现
系统流程图
Uploading / ShowImage / ImagePanel (显示层辅助类) Stduent / Grade (数据模型) Login / Teacher / StduentGrade (显示层) StduentManage/GradeManage (逻辑处理层) DBConnection 数据库 (数据持久层)
类 DBConnection Student Grade StduentManage GradeManage StudentNotFoundException GradeNotFoundException 包 com.ccniit.student.util com.ccniit.student com.ccniit.student com.ccniit.student.manage com.ccniit.student.manage com.ccniit.student.manage com.ccniit.student.manage 层 逻辑 模型 模型 逻辑 逻辑 逻辑 逻辑 逻辑 显示 显示 显示 显示 显示 显示 功能 连接数据库 保存学生对象 保存成绩对象 操作学生对象 操作成绩对象 当学生找不到时抛出此异常 当成绩找不到时抛出此异常 当密码找不到时抛出此异常 显示登录界面 显示教师界面 显示学生界面 读取图片 显示放大的图片 上传图片 PasswordNotFoundException com.ccniit.student.manage Login com.ccniit.student.view Teacher StudentGrade ImagePanel ShowImage Uploading
com.ccniit.student.view com.ccniit.student.view com.ccniit.student.view com.ccniit.student.view com.ccniit.student.view 3. 项目源码
com.ccniit.student..Stduent类
说明:此类是一个学生的模型类,提供了学生的属性和get、set方法,便于StudentManage来操作还提供了几个构造方法。
package com.ccniit.student;
import java.sql.Date; import java.util.List;
public class Student {
private String studentID; private String studentName; private String password; private String sex; private Date birth; private String phone; private String qq;
private String homeAddr; private Date entranceTime; private String photo; private String classes;
private List
public Student() { }
public Student(String studentID, String studentName, String password, String sex,
Date birth, String phone, String qq, String homeAddr, Date entranceTime, String photo, String classes) { this.studentID = studentID;
this.studentName = studentName; this.password = password; this.sex = sex;
this.birth = birth; this.phone = phone; this.qq = qq;
this.homeAddr = homeAddr;
this.entranceTime = entranceTime; this.photo = photo;
this.classes = classes; }
public Student(String studentID, String studentName, String sex, Date birth, String phone, String qq, String homeAddr, Date entranceTime, String photo, String classes) { this.studentID = studentID;
this.studentName = studentName; this.sex = sex;
this.birth = birth; this.phone = phone; this.qq = qq;
this.homeAddr = homeAddr;
this.entranceTime = entranceTime; this.photo = photo;
this.classes = classes; }
public Student(String studentID, String studentName,String password, String sex,
Date birth, String phone, String qq, String homeAddr,
Date entranceTime, String photo, String classes, List
this.studentID = studentID;
this.studentName = studentName; this.password = password; this.sex = sex;
this.birth = birth; this.phone = phone; this.qq = qq;
this.homeAddr = homeAddr;
this.entranceTime = entranceTime; this.photo = photo;
this.classes = classes; this.grades = grades; }
public String getStudentID() { return studentID; }
public void setStudentID(String studentID) { this.studentID = studentID; }
public String getStudentName() { return studentName; }
public void setStudentName(String studentName) { this.studentName = studentName; }
public String getSex() { return sex; }
public void setSex(String sex) { this.sex = sex; }
public Date getBirth() { return birth; }
public void setBirth(Date birth) { this.birth = birth; }
public String getPhone() { return phone; }
public void setPhone(String phone) { this.phone = phone; }
public String getQq() { return qq; }
public void setQq(String qq) { this.qq = qq; }
public String getHomeAddr() { return homeAddr; }
public void setHomeAddr(String homeAddr) { this.homeAddr = homeAddr; }
public Date getEntranceTime() { return entranceTime; }
public void setEntranceTime(Date entranceTime) { this.entranceTime = entranceTime; }
public List
public void setGrades(List
}
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public String getPhoto() { return photo; }
public void setPhoto(String photo) { this.photo = photo; }
public String getClasses() { return classes; }
public void setClasses(String classes) { this.classes = classes; }
com.ccniit.student.Grade类
说明: 此类是成绩模型类,提供了成绩的属性和get、set方法和几个构造方法,便于GradeManage来操作。
package com.ccniit.student;
public class Grade {
private String studentID; private String courseName; private int semester; private int schoolYear; private double grade;
public Grade() {}
public Grade(String studentID, String courseName) { this.studentID = studentID;
this.courseName = courseName; }
public Grade(String studentID, String courseName, int semester, int schoolYear, double grade) { this.studentID = studentID; this.courseName = courseName; this.semester = semester;
this.schoolYear = schoolYear; this.grade = grade; }
public String getStudentID() { return studentID; }
public void setStudentID(String studentID) { this.studentID = studentID; }
public String getCourseName() { return courseName; }
public void setCourseName(String courseName) { this.courseName = courseName; }
public int getSemester() { return semester; }
public void setSemester(int semester) { this.semester = semester; }
public int getSchoolYear() { return schoolYear; }
public void setSchoolYear(int schoolYear) { this.schoolYear = schoolYear; }
}
public double getGrade() { return grade; }
public void setGrade(double grade) { this.grade = grade; }
com.ccniit.student.util.DBConnection类
说明:这个类的功能是与底层数据库创建连接给逻辑处理层,还有关闭连接的相关功能。
package com.ccniit.student.util;
import java.sql.Connection; import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;
//管理与数据库的连接
public class DBConnection { static { try {
Class.forName(\); } catch (ClassNotFoundException e) { e.printStackTrace(); } }
private DBConnection() { }
public static Connection getConn() throws SQLException { Connection conn = DriverManager.getConnection(
\studentManage\,
\, \); return conn; }
public static void closeConn(Connection conn) { if (conn != null) { try {
conn.close();
} catch (SQLException e) { e.printStackTrace(); } } }
public static PreparedStatement getPstmt(Connection conn, String sql)
throws SQLException {
PreparedStatement pstmt = conn.prepareStatement(sql); return pstmt; }
public static void closePstmt(PreparedStatement pstmt) { if (pstmt != null) { try {
pstmt.close();
} catch (SQLException e) { e.printStackTrace(); } } }
public static ResultSet executeQuery(PreparedStatement pstmt) throws SQLException {
ResultSet rs = pstmt.executeQuery(); return rs; }
public static void closeRs(ResultSet rs) { if (rs != null) { try {
rs.close();
} catch (SQLException e) { e.printStackTrace(); } } } }
com.ccniit.student.manage.StudentManage类
说明:此类是逻辑处理层里面的学生管理类,有添加学生、删除学生、修改学生信息、检查学生是否登录成功、查找学生等方法,显示层可以直接调用这些方法来实现需要的功能。其中查找方法是有方法的组合。
package com.ccniit.student.manage;
import java.sql.Connection; import java.sql.Date;
import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List;
import com.ccniit.student.Student;
import com.ccniit.student.util.DBConnection;
public class StudentManage { // 添加学生
public static boolean add(Student s) { Connection conn = null;
PreparedStatement pstmt = null; boolean flag = false; try {
conn = DBConnection.getConn();
String sql = \password, sex, birth, phone, qq, homeAddr, entranceTime, photo, classes) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)\;
pstmt = DBConnection.getPstmt(conn, sql); pstmt.setString(1, s.getStudentID()); pstmt.setString(2, s.getStudentName()); pstmt.setString(3, s.getPassword()); pstmt.setString(4, s.getSex()); pstmt.setDate(5, s.getBirth()); pstmt.setString(6, s.getPhone()); pstmt.setString(7, s.getQq());
pstmt.setString(8, s.getHomeAddr()); pstmt.setDate(9, s.getEntranceTime()); pstmt.setString(10, s.getPhoto()); pstmt.setString(11, s.getClasses()); if (pstmt.executeUpdate() > 0) {
flag = true; }
} catch (SQLException e) { e.printStackTrace(); } finally {
DBConnection.closePstmt(pstmt); DBConnection.closeConn(conn); }
return flag; }
/*
* public static void main(String[] args) { // Calendar c = * Calendar.getInstance(); // c.set(2011, 7, 28); //
* System.out.println(add(new Student(\骆巍\男\new //
* Date(c // .getTimeInMillis()), \东软学院\
* System.out.println(add(new Student(\骆巍\\男\
* Date.valueOf(\都江堰成都东软学院\
* // System.out.println(delete(\ * Calendar.getInstance(); // c.set(2011, 7, 28); // * System.out.println(c.getTime()); } */
// 删除学生
public static boolean delete(String id) { Connection conn = null;
PreparedStatement pstmt = null; boolean flag = false; try {
conn = DBConnection.getConn();
String sql = \; pstmt = DBConnection.getPstmt(conn, sql); pstmt.setString(1, id);
if (pstmt.executeUpdate() > 0) { flag = true; }
} catch (SQLException e) { e.printStackTrace(); } finally {
DBConnection.closePstmt(pstmt);
DBConnection.closeConn(conn); }
return flag; }
// 修改学生信息
public static boolean update(Student s) { Connection conn = null;
PreparedStatement pstmt = null; boolean flag = false; try {
conn = DBConnection.getConn(); String sql = \student set studentName = ?, password = ?, sex = ?, birth = ?, phone = ?, qq = ?, homeAddr = ?, entranceTime = ?, photo = ?, classes = ? where studentID = ?\;
pstmt = DBConnection.getPstmt(conn, sql); pstmt.setString(1, s.getStudentName()); pstmt.setString(2, s.getPassword()); pstmt.setString(3, s.getSex()); pstmt.setDate(4, s.getBirth()); pstmt.setString(5, s.getPhone()); pstmt.setString(6, s.getQq());
pstmt.setString(7, s.getHomeAddr()); pstmt.setDate(8, s.getEntranceTime()); pstmt.setString(9, s.getPhoto()); pstmt.setString(10, s.getClasses()); pstmt.setString(11, s.getStudentID()); if (pstmt.executeUpdate() > 0) { flag = true; }
} catch (SQLException e) { e.printStackTrace(); } finally {
DBConnection.closePstmt(pstmt); DBConnection.closeConn(conn); }
return flag; }
// 检查学生登录是否成功
public static Student check(String studentID, String password) throws StudentNotFoundException, PasswordNotCorrectException { Student student = null;
Connection conn = null;
PreparedStatement pstmt = null; ResultSet rs = null; try {
conn = DBConnection.getConn();
String sql = \; pstmt = DBConnection.getPstmt(conn, sql); pstmt.setString(1, studentID);
rs = DBConnection.executeQuery(pstmt); if (!rs.next()) {
throw new StudentNotFoundException(); } else if
(!rs.getString(\).trim().equals(password)) {
// System.out.println(rs.getString(\ // System.out.println(password);
throw new PasswordNotCorrectException(); } else {
student = new Student(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getDate(5), rs.getString(6), rs.getString(7), rs.getString(8),
rs.getDate(9), rs.getString(10), rs.getString(11)); }
} catch (SQLException e) { e.printStackTrace(); } finally {
DBConnection.closeRs(rs);
DBConnection.closePstmt(pstmt); DBConnection.closeConn(conn); }
return student; }
// 按照studentID来查找学生,返回查找到的学生信息,没有找到则抛出异常 public static Student findByID(String studentID) throws StudentNotFoundException {
String condition = \ + studentID; List
throw new StudentNotFoundException(); }
return students.get(0); }
// 按照姓名来查找学生,返回查找到的学生人数,学生信息将添加在List容器里面
public static int findByName(List
// 按照生日来查找学生,返回查找到的学生人数,学生信息将添加在List容器里面
public static int findByBirth(List
String condition = \ + birth; return find(students, condition); }
//查询出所有的学生信息
public static int findAll(List
// 一个私有方法,查找的核心方法,condition是sq语言的查找条件
private static int find(List
PreparedStatement pstmt = null; ResultSet rs = null; int record = 0; try {
conn = DBConnection.getConn();
String sql = \ + condition; pstmt = DBConnection.getPstmt(conn, sql); rs = DBConnection.executeQuery(pstmt); while (rs.next()) {
students.add(new Student(rs.getString(1), rs.getString(2), rs
.getString(3), rs.getString(4), rs.getDate(5), rs .getString(6), rs.getString(7), rs.getString(8), rs
.getDate(9), rs.getString(10), rs.getString(11)));
record++; }
} catch (SQLException e) { e.printStackTrace();
}
}
} finally {
DBConnection.closeRs(rs);
DBConnection.closePstmt(pstmt); DBConnection.closeConn(conn); }
return record;
public static Date shiftDate(String date) { return Date.valueOf(date); }
// 得到所有的班级
public static Object[] getClasses() { Connection conn = null;
PreparedStatement pstmt = null; ResultSet rs = null;
List
conn = DBConnection.getConn();
String sql = \; pstmt = DBConnection.getPstmt(conn, sql); rs = DBConnection.executeQuery(pstmt); while (rs.next()) {
String c = rs.getString(1); if (c == null) { continue; }
classes.add(c); }
} catch (SQLException e) { e.printStackTrace(); } finally {
DBConnection.closeRs(rs);
DBConnection.closePstmt(pstmt); DBConnection.closeConn(conn); }
return classes.toArray(); }
com.ccniit.student.manage.GradeManage类
说明:此类是逻辑处理层里面的成绩管理类,有添加成绩、删除成绩、修改成绩、查找成绩等方法,显示层可以直接调用这些方法来实现需要的功能。其中查找方法是有方法的组合。
package com.ccniit.student.manage;
import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List;
import com.ccniit.student.Grade;
import com.ccniit.student.util.DBConnection;
public class GradeManage { // 添加学生成绩信息
public static boolean add(Grade grade) { Connection conn = null;
PreparedStatement pstmt = null; boolean flag = false; try {
conn = DBConnection.getConn();
String sql = \semester, schoolYear, grade) values(?, ?, ?, ?, ?)\; pstmt = DBConnection.getPstmt(conn, sql); pstmt.setString(1, grade.getStudentID()); pstmt.setString(2, grade.getCourseName()); pstmt.setInt(3, grade.getSemester()); pstmt.setInt(4, grade.getSchoolYear()); pstmt.setDouble(5, grade.getGrade()); if (pstmt.executeUpdate() > 0) { flag = true; }
} catch (SQLException e) { e.printStackTrace(); } finally {
DBConnection.closePstmt(pstmt); DBConnection.closeConn(conn); }
return flag; }
public static boolean addGrades(String studentID) {
add(new Grade(studentID, \语言高级程序设计\, 2, 2012, 0)); add(new Grade(studentID, \大学英语\, 2, 2012, 0)); add(new Grade(studentID, \高等数学\, 2, 2012, 0)); add(new Grade(studentID, \沟通与演讲\, 2, 2012, 0)); add(new Grade(studentID, \数据结构\, 2, 2012, 0));
add(new Grade(studentID, \数据库原理与应用\, 2, 2012, 0)); add(new Grade(studentID, \体育\, 2, 2012, 0)); return true; }
// 删除学生成绩信息
public static boolean delete(String id) { Connection conn = null;
PreparedStatement pstmt = null; boolean flag = false; try {
conn = DBConnection.getConn();
String sql = \; pstmt = DBConnection.getPstmt(conn, sql); pstmt.setString(1, id);
if (pstmt.executeUpdate() > 0) { flag = true; }
} catch (SQLException e) { e.printStackTrace(); } finally {
DBConnection.closePstmt(pstmt); DBConnection.closeConn(conn); }
return flag; }
// 修改学生成绩信息
public static boolean update(Grade grade) { Connection conn = null;
PreparedStatement pstmt = null; boolean flag = false; try {
conn = DBConnection.getConn();
String sql = \grade = ? where studentID = ? and courseName = ?\; pstmt = DBConnection.getPstmt(conn, sql);
pstmt.setInt(1, grade.getSemester()); pstmt.setInt(2, grade.getSchoolYear()); pstmt.setDouble(3, grade.getGrade());
pstmt.setString(4, grade.getStudentID()); pstmt.setString(5, grade.getCourseName()); if (pstmt.executeUpdate() > 0) { flag = true; }
} catch (SQLException e) { e.printStackTrace(); } finally {
DBConnection.closePstmt(pstmt); DBConnection.closeConn(conn); }
return flag; }
public static boolean updateGrades(String studentID) { update(new Grade(studentID, \语言高级程序设计\, 2, 2012, 0)); update(new Grade(studentID, \大学英语\, 2, 2012, 0)); update(new Grade(studentID, \高等数学\, 2, 2012, 0)); update(new Grade(studentID, \沟通与演讲\, 2, 2012, 0)); update(new Grade(studentID, \数据结构\, 2, 2012, 0));
update(new Grade(studentID, \数据库原理与应用\, 2, 2012, 0)); update(new Grade(studentID, \体育\, 2, 2012, 0)); return true; }
// 按照studentID来查找学生成绩信息,返回查找到的成绩信息,没有找到则抛出异常
public static int findByID(List
// 按照姓名来查找学生成绩信息,返回查找到的成绩信息数,成绩信息将添加在List容器里面
public static int findByName(List
return find(grades, condition); }
// 按照studentID和courseName来查找学生成绩信息,返回查找到的成绩信息,没有找到则抛出异常
public static Grade findByIDAddCourseName(String studentID, String courseName) throws GradeNotFoundException { String condition = \ + studentID + \ + courseName + \; List
throw new GradeNotFoundException(); }
return grade.get(0); }
// 查询出所有的学生成绩
public static int findAll(List
return find(grades, condition); }
// 一个私有方法,查找的核心方法,condition是sq语言的查找条件
private static int find(List
PreparedStatement pstmt = null; ResultSet rs = null; int record = 0; try {
conn = DBConnection.getConn();
String sql = \ + condition; pstmt = DBConnection.getPstmt(conn, sql); rs = DBConnection.executeQuery(pstmt); while (rs.next()) {
grades.add(new Grade(rs.getString(1), rs.getString(2), rs
.getInt(3), rs.getInt(4), rs.getDouble(5))); record++; }
} catch (SQLException e) { e.printStackTrace(); } finally {
DBConnection.closeRs(rs);
DBConnection.closePstmt(pstmt); DBConnection.closeConn(conn); }
return record;
{ }
}
public static boolean updateSchoolSemester(int school, int semester) }
Connection conn = null;
PreparedStatement pstmt = null; boolean flag = false; try {
conn = DBConnection.getConn();
String sql = \grade set semester = ?, schoolYear = ?\; pstmt = DBConnection.getPstmt(conn, sql); pstmt.setInt(1, semester); pstmt.setInt(2, school);
if (pstmt.executeUpdate() > 0) { flag = true; }
} catch (SQLException e) { e.printStackTrace(); } finally {
DBConnection.closePstmt(pstmt); DBConnection.closeConn(conn); }
return flag;
com.ccniit.student.view.Login类
说明:此类是显示层的登录界面,此界面类似一个对话框,但是他是继承JFrame,分为两个模块:学生登录和教师登录。有一个下拉条可以选择是学生登录还是教师登录,当选择学生登录时,会登录进入学生界面,当选择教师登录时会进入教师界面。
package com.ccniit.student.view;
import java.awt.Font;
import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel;
import javax.swing.JOptionPane; import javax.swing.JPanel;
import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import com.ccniit.student.Student;
import com.ccniit.student.manage.PasswordNotCorrectException; import com.ccniit.student.manage.StudentManage;
import com.ccniit.student.manage.StudentNotFoundException; /*
* 此系统为学生管理系统0.0.4的升级版本,在上一个版本基础上对系统进行了大量优化和改进,使用户有更佳的体验 */
public class Login extends JFrame {
private Font fontLogo = new Font(\楷体\, 1, 20);
private JLabel jlLogo = new JLabel(\学生管理系统登录界面\); private Font font = new Font(\楷体\, 1, 25); private JLabel jlLogin = new JLabel(\教师登录\); String[] post = { \教师\, \学生\ };
private JComboBox jcbPost = new JComboBox(post); private JLabel jlID = new JLabel(\教师号:\); private JTextField jtfID = new JTextField(20); private JLabel jlPassword = new JLabel(\密码:\);
private JPasswordField jpfPassword = new JPasswordField(20); private JButton jbLogin = new JButton(\登录\); private JButton jbCancel = new JButton(\退出\); // 设置背景图片
private JLabel jlImage = new JLabel(); private ImageIcon iiImage;
public Login() {
setTitle(\学生管理系统0.0.5-登录\); setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE); setResizable(false);
// setLayout(new FlowLayout(FlowLayout.CENTER,100,30)); setLayout(null);
iiImage = new ImageIcon(\); jlImage.setIcon(iiImage);
}
jlImage.setSize(400, 300); jlImage.setLocation(0, 0);
getLayeredPane().add(jlImage, new Integer(Integer.MIN_VALUE)); JPanel jp = (JPanel) getContentPane(); jp.setOpaque(false); init();
public void init() {
jlLogo.setLocation(100, 5); jlLogo.setSize(250, 40); jlLogo.setFont(fontLogo);
add(jlLogo);
jlLogin.setFont(font);
jlLogin.setLocation(140, 30); jlLogin.setSize(150, 50); add(jlLogin);
jcbPost.setLocation(150, 75); jcbPost.setSize(80, 25);
jcbPost.setFocusable(false); // 添加登录界面的角色选择监听
jcbPost.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
if (jcbPost.getSelectedItem().equals(\学生\)) { jlLogin.setText(\学生登录\); jlID.setText(\学号:\); } else {
jlLogin.setText(\教师登录\); jlID.setText(\教师号:\); } } });
add(jcbPost);
jlID.setLocation(80, 120); jlID.setSize(50, 30); add(jlID);
jtfID.setLocation(130, 120); jtfID.setSize(150, 25); add(jtfID);
jlPassword.setLocation(80, 160); jlPassword.setSize(50, 30);
add(jlPassword);
jpfPassword.setLocation(130, 160); jpfPassword.setSize(150, 25); add(jpfPassword);
// 监听登录按钮
jbLogin.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
// 由于一般就是一个教师修改学生信息,所以教师没有建表,所有的教师都通过一个id号和密码登录
if (jcbPost.getSelectedItem().equals(\教师\)) { if (jtfID.getText().trim().equals(\) &&
String.valueOf(jpfPassword.getPassword()).trim()
.equals(\)) { setVisible(false);
new Teacher().setVisible(true); } else {
JOptionPane.showMessageDialog(Login.this,
\登录失败!教师号:jiaoshi 密码:jiaoshi.\); } } else {
String id = jtfID.getText().trim(); String password =
String.valueOf(jpfPassword.getPassword()) .trim();
if (\.equals(id)) {
JOptionPane.showMessageDialog(Login.this, \请输入学号!\);
return;
} else if (\.equals(password)) {
JOptionPane.showMessageDialog(Login.this, \请输入密码!\);
return; }
try {
// 检查学生登录是否正确,会抛出下面的两种异常,返回登录的学生信息,传递给StudentGrade类
Student student = StudentManage.check(id, password);
setVisible(false);
new StudentGrade(student, false).setVisible(true);
} catch (StudentNotFoundException e1) {
JOptionPane.showMessageDialog(Login.this, \学号不存在!\);
} catch (PasswordNotCorrectException e1) {
JOptionPane.showMessageDialog(Login.this, \密码错误!\);
} } } });
jbLogin.setLocation(100, 200); jbLogin.setSize(80, 30); add(jbLogin);
jbCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } });
jbCancel.setLocation(200, 200); jbCancel.setSize(80, 30); add(jbCancel); }
public static void main(String[] args) { try {
// 设置和win7的布局一样
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) { e.printStackTrace();
} catch (InstantiationException e) { e.printStackTrace();
} catch (IllegalAccessException e) { e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) { e.printStackTrace(); }
new Login().setVisible(true); } }
com.ccniit.student.view.StduentGrade类
说明:此类是显示层的学生成绩界面,有菜单栏、内容窗口、功能按钮,此界面的功能是查询登录学生的成绩,查询学生的个人详细信息,修改个人信息,修改密码等功能。还新创建了一个线程来读取学生的成绩和信息,此线程是一个内部类,有一个修改密码的小窗口是一个内部类继承了JDialog,修改信息的内部类小窗口继承了JDialog。学生还可以上传自己的照片,修改自己上传的照片,上传后学生就能看到自己的照片,但是学生不能修改自己的班级和入学时间,这两项老师可以修改。部分界面如下图所示:
package com.ccniit.student.view;
import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File;
import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.List;
import javax.swing.ButtonGroup; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel;
import javax.swing.JPasswordField; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextArea; import javax.swing.JTextField;
import com.ccniit.student.Grade; import com.ccniit.student.Student;
import com.ccniit.student.manage.GradeManage; import com.ccniit.student.manage.StudentManage;
public class StudentGrade extends JFrame { private Student student;
private List
public static String imagePath = \; // 相对路径
// 菜单相关组件
private JMenuBar jmb = new JMenuBar(); // 菜单条 private JMenu jmFile = new JMenu(\文件 \); private JMenuItem jmiExit = new JMenuItem(\退出\); private JMenu jmQuery = new JMenu(\查询 \);
private JMenuItem jmiQueryGrade = new JMenuItem(\成绩查询\); private JMenuItem jmiQueryInfo = new JMenuItem(\信息查询...\); private JMenu jmUpdate = new JMenu(\修改 \);
private JMenuItem jmiUpdateInfo = new JMenuItem(\修改信息...\); private JMenuItem jmiUpdatePassword = new JMenuItem(\修改密码...\); private JMenu jmHelp = new JMenu(\帮助 \);
private JMenuItem jmiHelp = new JMenuItem(\帮助...\); private JMenuItem jmiAbout = new JMenuItem(\关于...\);
// 成绩信息显示相关组件
private JLabel jlWelcome; // 显示欢迎的标签 private Font font = new Font(\楷体\, 0, 17); private JTable table; // 成绩信息表
private JPanel jpCenter = new JPanel(new GridLayout(2, 1)); private JScrollPane jsp;
private UpdatePassword updatePassword; private UpdateInfo updateInfo;
// 下面部分的相关组件
private JPanel jpDown = new JPanel(
new FlowLayout(FlowLayout.CENTER, 20, 20)); // 下面部分的JPanel
private JButton jbQueryGrade = new JButton(\成绩查询\); private JButton jbQueryInfo = new JButton(\信息查询\); private JButton jbUpdateInfo = new JButton(\修改信息\);
private JButton jbUpdatePassword = new JButton(\修改密码\); private JButton jbExit = new JButton(\退出 \);
public StudentGrade(Student student, boolean isUpdateGradeble) { this.student = student;
this.isUpdateGradeble = isUpdateGradeble; setTitle(\学生管理系统0.0.4-学生界面\);
}
setSize(800, 600);
setLocationRelativeTo(null); if (!isUpdateGradeble)
setDefaultCloseOperation(EXIT_ON_CLOSE);
new Thread(new LoadThread()).start(); // 开始读取学生成绩的线程 init();
private void init() { // 查询学生成绩
jmiQueryGrade.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { queryGrade(e); } });
jmQuery.add(jmiQueryGrade);
jmiQueryInfo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { queryInfo(e); } });
jmQuery.add(jmiQueryInfo);
jmiUpdateInfo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { updateInfo(e); } });
jmUpdate.add(jmiUpdateInfo);
jmiUpdatePassword.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { updatePassword(e); } });
jmUpdate.add(jmiUpdatePassword);
jmiExit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (isUpdateGradeble) setVisible(false); else
System.exit(0);
} });
jmFile.add(jmiExit); jmb.add(jmFile); jmb.add(jmQuery); jmb.add(jmUpdate);
jmiHelp.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(StudentGrade.this,
\学生管理系统-学生界面,用于学生查询个人成绩和个信息,可以修改个人信息和密码.\); } });
jmHelp.add(jmiHelp);
jmiAbout.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane
.showMessageDialog(StudentGrade.this,
\学生管理系统-学生界面\\n欢迎使用学生管理系统\\n作者: 骆巍\\n当前版本: 0.0.5\\n发行时间: 2012-05-24\); } });
jmHelp.add(jmiAbout); jmb.add(jmHelp); setJMenuBar(jmb);
jlWelcome = new JLabel(\学号: \ + student.getStudentID() + \姓名: \
+ student.getStudentName() + \班级: \ + student.getClasses()
+ \电话: \ + student.getPhone()); jlWelcome.setFont(font);
add(jlWelcome, BorderLayout.NORTH);
jpCenter.add(new ImagePanel(imagePath + student.getPhoto())); add(jpCenter);
jbQueryGrade.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { queryGrade(e); } });
}
jpDown.add(jbQueryGrade);
jbQueryInfo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { queryInfo(e); } });
jpDown.add(jbQueryInfo);
jbUpdateInfo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { updateInfo(e); } });
jpDown.add(jbUpdateInfo);
jbUpdatePassword.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { updatePassword(e); } });
jpDown.add(jbUpdatePassword);
jbExit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (isUpdateGradeble) setVisible(false); else
System.exit(0); } });
jpDown.add(jbExit);
add(jpDown, BorderLayout.SOUTH);
private void queryGrade(ActionEvent e) { jpCenter.add(jsp); setVisible(true); }
private void updatePassword(ActionEvent e) { if (updatePassword == null)
updatePassword = new UpdatePassword(); updatePassword.setVisible(true);
}
private void queryInfo(ActionEvent e) { JOptionPane.showMessageDialog( this,
\学号: \ + student.getStudentID() + \姓名: \ + student.getStudentName() + \性别: \ + student.getSex() + \班级: \ + student.getClasses()
+ \生日: \ + student.getBirth() + \电话: \ + student.getPhone() + \QQ: \ + student.getQq() + \家庭地址: \ + student.getHomeAddr() + \入学时间: \
+ student.getEntranceTime()); }
private void updateInfo(ActionEvent e) { if (updateInfo == null) {
updateInfo = new UpdateInfo(); }
updateInfo.setVisible(true); }
// 声明一个线程来默认读取学生的成绩信息
private class LoadThread implements Runnable { public void run() {
int records = GradeManage.findByID(grades, student.getStudentID());
Object[] columnNames = { \课程名称\, \学期\, \学年\, \分数\ }; Object[][] rowData = new Object[records][columnNames.length];
for (int i = 0; i < grades.size(); i++) { Grade grade = grades.get(i);
rowData[i][0] = grade.getCourseName(); rowData[i][1] = grade.getSemester(); rowData[i][2] = grade.getSchoolYear(); rowData[i][3] = grade.getGrade(); }
table = new JTable(rowData, columnNames); table.setRowHeight(20); table.setFont(font);
table.setEnabled(false);
jsp = new JScrollPane(table); }
}
private class UpdatePassword extends JDialog { // 修改密码相关组件
private JLabel jlOldPassword = new JLabel(\当前密码: \);
private JPasswordField jpfOldPassword = new JPasswordField(20); private JLabel jlNewPassword = new JLabel(\新密码: \);
private JPasswordField jpfNewPassword = new JPasswordField(20); private JLabel jlConfirmPassword = new JLabel(\确认密码:\); private JPasswordField jpfConfirmPassword = new JPasswordField(20);
private JButton jbQueryPassword = new JButton(\确认修改\); private JButton jbCancel = new JButton(\取消 \);
public UpdatePassword() {
super(StudentGrade.this, \修改密码\, true); setSize(300, 300);
setLayout(new FlowLayout(FlowLayout.CENTER, 30, 30)); setLocationRelativeTo(StudentGrade.this); // 设置窗体显示在中间
setResizable(false); init(); }
private void init() { add(jlOldPassword); add(jpfOldPassword); add(jlNewPassword); add(jpfNewPassword); add(jlConfirmPassword); add(jpfConfirmPassword);
jbQueryPassword.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String oldPassword = String.valueOf(
jpfOldPassword.getPassword()).trim(); String newPassword = String.valueOf(
jpfNewPassword.getPassword()).trim(); String confirmPassword = String.valueOf(
jpfConfirmPassword.getPassword()).trim(); // System.out.println(student.getPassword()); if (\.equals(oldPassword)) {
JOptionPane.showMessageDialog(UpdatePassword.this,
\请输入当前密码!\); } else if (!oldPassword
.equals(student.getPassword().trim())) {
JOptionPane.showMessageDialog(UpdatePassword.this, \当前密码错误!\); } else {
if (newPassword.length() < 6) {
JOptionPane.showMessageDialog(UpdatePassword.this, \密码长度不能少于6位!\);
} else if (newPassword.equals(confirmPassword)) { student.setPassword(newPassword); if (StudentManage.update(student)) { JOptionPane.showMessageDialog(
UpdatePassword.this, \修改成功!\); setVisible(false); } else {
JOptionPane.showMessageDialog(
UpdatePassword.this, \修改失败!\); } } else {
JOptionPane.showMessageDialog(UpdatePassword.this, \两次密码不一致!\); } } } });
add(jbQueryPassword);
jbCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setVisible(false); } });
add(jbCancel); } }
private class UpdateInfo extends JDialog {
private JLabel jlName = new JLabel(\姓名:\); private JTextField jtfName = new JTextField(student.getStudentName()
.trim(), 20);
private JLabel jlSex = new JLabel(\性别: \); private ButtonGroup bgSex = new ButtonGroup();
private JRadioButton jrbMan = new JRadioButton(\男\); private JRadioButton jrbWoman = new JRadioButton(\女\); private JLabel jlBirth = new JLabel(\出生日期:\); private JTextField jtfBirth = new JTextField( student.getBirth() == null ? \ : student.getBirth().toString(), 20);
private JLabel jlPhone = new JLabel(\电话号码:\); private JTextField jtfPhone = new JTextField(student.getPhone().trim(), 20);
private JLabel jlQQ = new JLabel(\); private JTextField jtfQQ = new JTextField(student.getQq().trim(), 20);
private JLabel jlEntranceTime = new JLabel(\入学时间:\); private JTextField jtfEntranceTime = new JTextField(student .getEntranceTime().toString(), 20);
private JLabel jlClass = new JLabel(\班级: \); private JTextField jtfClass = new JTextField(student.getClasses(), 20);
private JTextField jtfPhoto = new JTextField(40); private JButton jbPhoto = new JButton(\上传照片\);
private JLabel jlAddr = new JLabel(\家庭地址: \); private JTextArea jtaAddr = new JTextArea(student.getHomeAddr().trim(), 2, 35);
private JButton jbQueryInfo = new JButton(\确认修改\); private JButton jbCancel = new JButton(\取消 \);
private Uploading upload = new Uploading(this); // 创建上传照片的类
public UpdateInfo() {
super(StudentGrade.this, \修改个人信息\, true); setSize(300, 550);
setLayout(new FlowLayout(FlowLayout.CENTER, 30, 20)); setLocationRelativeTo(StudentGrade.this); // 设置窗体显示在中间
setResizable(false); init(); }
private void init() { add(jlName); add(jtfName); add(jlSex);
if (student.getSex().equals(\男\)) { jrbMan.setSelected(true); } else {
jrbWoman.setSelected(true); }
bgSex.add(jrbMan); bgSex.add(jrbWoman); add(jrbMan); add(jrbWoman); add(jlBirth); add(jtfBirth); add(jlPhone); add(jtfPhone); add(jlQQ); add(jtfQQ);
add(jlEntranceTime);
jtfEntranceTime.setEditable(isUpdateGradeble); add(jtfEntranceTime);
add(jlClass);
jtfClass.setEditable(isUpdateGradeble); // 设置限权 add(jtfClass);
jtfPhoto.setEditable(false);
jtfPhoto.setText(student.getPhoto()); add(jtfPhoto);
// 添加上传照片的监听
jbPhoto.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try {
File file = upload.choose(); if (file != null)
jtfPhoto.setText(file.getPath()); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } } });
add(jbPhoto);
add(jlAddr);
jtaAddr.setLineWrap(true); add(jtaAddr);
jbQueryInfo.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String name = jtfName.getText().trim(); if (\.equals(name)) {
JOptionPane.showMessageDialog(UpdateInfo.this, \请输入自己的姓名!\); return; }
student.setStudentName(name);
student.setSex(jrbMan.isSelected() ? \男\ : \女\); String date = jtfBirth.getText().trim(); if (\.equals(date)) {
student.setBirth(null);
} else if (date.matches(\)) {
student.setBirth(StudentManage.shiftDate(date)); } else {
JOptionPane.showMessageDialog(UpdateInfo.this, \输入出生日期格式不正确!\\n正确格式为:2011-08-12\);
return; }
if (isUpdateGradeble) { String entranceTime = jtfEntranceTime.getText().trim();
if (\.equals(entranceTime)) { // student.setBirth(null); } else if (entranceTime
.matches(\)) { student.setEntranceTime(StudentManage .shiftDate(entranceTime)); } else {
JOptionPane.showMessageDialog(UpdateInfo.this,
\输入入学时间格式不正确!\\n正确格式为:2011-08-12\);
return;
\);
\);
} }
student.setPhone(jtfPhone.getText().trim()); student.setQq(jtfQQ.getText().trim());
String classes = jtfClass.getText().trim(); if (\.equals(classes)) {
JOptionPane.showMessageDialog(UpdateInfo.this, \学生班级不能为空!\); return; }
student.setClasses(classes);
student.setPhoto(student.getStudentID().trim() + student.setHomeAddr(jtaAddr.getText().trim()); // 上传照片 try {
if (upload.getInputStream() != null) { upload.upload(imagePath
+ student.getStudentID().trim() + }
} catch (FileNotFoundException e1) {
JOptionPane.showMessageDialog(UpdateInfo.this, \上传照片失败,找不到文件!\); e1.printStackTrace(); return;
} catch (IOException e1) {
JOptionPane.showMessageDialog(UpdateInfo.this, \上传照片失败,上传错误!\); e1.printStackTrace(); return; } finally {
upload.colseStream(); // 关闭文件流 }
if (StudentManage.update(student)) {
JOptionPane.showMessageDialog(UpdateInfo.this, \个人信息修改成功!\); setVisible(false); } else {
JOptionPane.showMessageDialog(UpdateInfo.this,
求!\);
} } }
\个人信息修改失败!可能是修改内容不符合要
} } });
add(jbQueryInfo);
jbCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { setVisible(false); } });
add(jbCancel);
com.ccniit.student.view.ImagePanel类
说明:此类是得到一个图片的路径然后把这个图片读取出来,显示在调用这个类的组件里面,这个类继承了JPanel,可以直接添加在窗口组件上面。这个类是用来读取学生照片,照片会根据窗口的大小来方法或缩小图片,但是图片不会失真 关键代码:
package com.ccniit.student.view;
import java.awt.Graphics;
import javax.swing.ImageIcon; import javax.swing.JPanel;
// 初始化图片的类
public class ImagePanel extends JPanel { private ImageIcon image; private int width; private int height;
private int imageWidth; private int imageHeight; private double rateImage; private double rateFrame;
ImagePanel(String path) {
image = new ImageIcon(path);
imageWidth = image.getIconWidth(); imageHeight = image.getIconHeight();
}
rateImage = (double) imageHeight / imageWidth; }
public ImageIcon getImageIcon() { return this.image; }
protected void paintComponent(Graphics g) { int frameWidth = this.getWidth(); int frameHeight = this.getHeight();
rateFrame = (double) frameHeight / frameWidth; if (rateFrame < rateImage) { height = frameHeight;
width = (int) (height / rateImage); } else {
width = frameWidth;
height = (int) (width * rateImage); }
// System.out.println(width + \
g.drawImage(image.getImage(), 0, 0, width, height, this); }
com.ccniit.student.view.ShowImage类
说明:这个类是显示放大过后的图片,当老师点击学生的图片的会new这个类的对象来,然后把这个图片重新显示在一个比较大的窗口里面,所以图片就会放大。
package com.ccniit.student.view;
import javax.swing.JDialog;
public class ShowImage extends JDialog { public ShowImage(ImagePanel image) { setSize(700, 700); setLocation(600, 0); add(image); } }
com.ccniit.student.view.Uploading类
说明:此类是上传照片的类,进入主界面的添加学生或修改学生成绩的时候就会new这个
正在阅读:
java学生管理系统源码07-10
2018届高考数学二轮复习专题检测(一)集合与常用逻辑用语文01-07
企业价值评估习题练习05-03
对公司管理的几点建议10-19
水利水电工程概论结课论文11-16
企业价值评估论文05-18
怎么给word文档加密?04-24
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 源码
- 管理系统
- 学生
- java
- 国贸练习题及答案
- 世界著名金融学家个人主页资源概览
- 小学六年级数学下册全册教学反思(新课标人教版)
- 2012天津会计从业资格证电算化考试多选题
- 五年级下册综合实践教案
- 用友T3不能连接SQL2005数据库的解决方案
- 汽车维护与保养期末考试试卷(B)13级1、2、3班
- 八(2)班校园安全教育讲稿
- 盐都区2013年中小学青年教师教学基本功大赛获奖名单doc
- 2018年北师大版(一起)英语六上Unit1练习卷(带解析)3
- 湖北省鄂州市2015年中考数学试题及答案(Word版)
- 届高三生物二轮复习第一部分知识落实篇专题六生物群体的稳
- 光伏电站组件清洗方案
- 探索活动(二)三角形的面积
- 《流行软件》实验指导书 - new
- 大学英语三级阅读
- 北理工中特大作业
- 景观园林绿化设计招标文件范本(免费)
- 苏科版九年级物理(上)期末试卷及答案
- 镇消防安全会议纪要