教学管理系统很适合初学者对于所学语言的练习。本文是javaSE 中用文件流写的,这个也可以用数据库写。
这个项目有 1.学生 2.老师 3.教务人员 4.管理员四个角色分别担任不同的任务。
1.学生 有属性 id, 密码,性别,年龄,和一个存放成绩的集合(因为一个学生可能会有多个科目,所以用集合来存放学生的所学科目)。
2.老师 有属性 id,密码,性别,年龄,和一成绩类的对象(考虑到老师只教授一科目,所以在老师类中实现一个科目类的对象)。
3.教务人员 有属性 id,密码(教务人员没有考虑自身的信息,主要对老师和学生的管理)。
4.管理员 对学生老师和教务人员进行帐号注册(例如学校的教学网第一次登录不需要注册直接用学号登录然后改密码,这样防止外人注册进入到系统)
只有一个系统管理员,用来添加学生,教师和教务人员帐号,方便学习这里帐号id为111,密码为111。
还要有科目类 有属性 科目名,科目编号,科目分数。
package com.file;
import java.io.Serializable;
import java.util.ArrayList;
public class Student implements Serializable {
private String id = null; //设为私有属性,区分老师类的属性
private String password = null;
private String name = null;
private String age = null;
private String sex = null;
public ArrayList arr_course = new ArrayList();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Student() {
super();
}
public Student(String id, String password) {
super();
this.id = id;
this.password = password;
}
public Student(String id, String password, String name, String age,
String sex) {
super();
this.id = id;
this.password = password;
this.name = name;
this.age = age;
this.sex = sex;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
package com.file;
import java.io.Serializable;
public class Teacher implements Serializable{
private String id = null;
private String name = null;
private String password = null;
private String age = null;
private String sex = null;
private Course teaching_course = null;
public Teacher(String id, String password) {
super();
this.id = id;
this.password = password;
}
@Override
public String toString() {
return "Teacher [id=" + id + ", name=" + name + ", password="
+ password + ", age=" + age + ", sex=" + sex
+ ", teaching_course=" + teaching_course + "]";
}
public Teacher(String id, String name, String password, String age,
String sex, Course teaching_course) {
super();
this.id = id;
this.name = name;
this.password = password;
this.age = age;
this.sex = sex;
this.teaching_course = teaching_course;
}
public Teacher() {
super();
// TODO Auto-generated constructor stub
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Course getTeaching_course() {
return teaching_course;
}
public void setTeaching_course(Course teaching_course) {
this.teaching_course = teaching_course;
}
}
package com.file;
import java.io.Serializable;
public class Manage implements Serializable{
private String id = null;
private String name = null;
private String password = null;
public Manage() {
super();
// TODO Auto-generated constructor stub
}
public Manage(String id, String name, String password) {
super();
this.id = id;
this.name = name;
this.password = password;
}
public Manage(String id, String password) {
super();
this.id = id;
this.password = password;
}
@Override
public String toString() {
return "Manage [id=" + id + ", name=" + name + ", password=" + password
+ "]";
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.file;
import java.io.Serializable;
public class Course implements Serializable{
public String courseId = null; //课程编号
public String courseName = null; //课程名
public String courseMark = null; //课程分数
public String getCourseId() {
return courseId;
}
public void setCourseId(String courseId) {
this.courseId = courseId;
}
public String getCourseName() {
return courseName;
}
public Course(String courseName) {
super();
this.courseName = courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getCourseMark() {
return courseMark;
}
public void setCourseMark(String courseMark) {
this.courseMark = courseMark;
}
public Course(String courseId, String courseName, String courseMark) {
super();
this.courseId = courseId;
this.courseName = courseName;
this.courseMark = courseMark;
}
public Course() {
super();
// TODO Auto-generated constructor stub
}
public int searchCourse(){
return 0;
}
@Override
public String toString() {
return "Course [courseId=" + courseId + ", courseName=" + courseName
+ ", courseMark=" + courseMark + "]";
}
}
在实现时用集合来实现所以对学生老师教务人员创建三个集合
package com.file;
import java.util.ArrayList;
public class DB {
public static ArrayList arrStu = new ArrayList();//学生
public static ArrayList arrTea = new ArrayList();//老师
public static ArrayList arrMan= new ArrayList();//教务人员
}
为了方便使用给学生老师和教务人员各建一个输入输出类,代码都类似。
package com.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class IOStreamStudent {
public static File file = new File("stu.txt");
public static void writeToFile() {
FileOutputStream fo = null;
ObjectOutputStream objout = null;
try {
fo = new FileOutputStream(file);
objout = new ObjectOutputStream(fo);
objout.writeObject(DB.arrStu);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
objout.flush();
objout.close();
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("Write Student to File" + " " + file);
}
@SuppressWarnings({ "resource", "unchecked" })
public static ArrayList readFromFile(){
ArrayList arr = null;
ObjectInputStream objin = null;
FileInputStream fin = null;
try{
fin = new FileInputStream(file);
objin = new ObjectInputStream(fin);
arr = (ArrayList) objin.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return arr;
}
}
package com.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class IOStreamTeacher {
public static File file = new File("tea.txt");
public static void writeToFile() {
FileOutputStream fo = null;
ObjectOutputStream objout = null;
try {
fo = new FileOutputStream(file);
objout = new ObjectOutputStream(fo);
objout.writeObject(DB.arrTea);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
objout.flush();
objout.close();
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("Write Teacher to File" + " " + file);
}
@SuppressWarnings({ "resource", "unchecked" })
public static ArrayList readFromFile(){
ArrayList arr = null;
ObjectInputStream objin = null;
FileInputStream fin = null;
try{
fin = new FileInputStream(file);
objin = new ObjectInputStream(fin);
arr = (ArrayList) objin.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return arr;
}
}
package com.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class IOStreamManage {
public static File file = new File("manage.txt");
public static void writeToFile() {
FileOutputStream fo = null;
ObjectOutputStream objout = null;
try {
fo = new FileOutputStream(file);
objout = new ObjectOutputStream(fo);
objout.writeObject(DB.arrMan);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
objout.flush();
objout.close();
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("Write Manage to File" + " " + file);
}
@SuppressWarnings({ "resource", "unchecked" })
public static ArrayList readFromFile(){
ArrayList arr = null;
ObjectInputStream objin = null;
FileInputStream fin = null;
try{
fin = new FileInputStream(file);
objin = new ObjectInputStream(fin);
arr = (ArrayList) objin.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return arr;
}
}
在当前项目下创建stu.txt tea.txt manage.txt三个文件用来保存数据。
因为是只有管理员才能注册新的用户,所以此页面没有注册按钮,需要注册时点击管理员按钮进入Register类进行注册。
如果要更改为每个随意注册只需添加注册按钮和相应的监听,然后转到Register类即可。
package com.file;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Login extends JFrame{
static String administration_id = "111";
static String administration_password = "111";
String zhiwu;
JFrame jf;
JLabel jl_id,jl_password,jl3;
public static JTextField jt_id,jt_password;
JPanel jp1,jp2,jp3,jp4,jp_group,jp_img;
JButton jb_login;
JRadioButton radioButton_student,radioButton_teacher,radioButton_manage,radioButton_administration;
ButtonGroup group;
ImageIcon img = null;
public Login(){
jf = new JFrame();
jf.setSize(500,700);
jf.setLocation(500,100);
jf.setDefaultCloseOperation(EXIT_ON_CLOSE);
jf.setLayout(new GridLayout(6,2));
Icon img = new ImageIcon("tu1.gif");
jp_img = new JPanel();
jp_img.add(new JLabel(img));
jl_id = new JLabel("用户名");
jt_id = new JTextField(15);
jp1 =new JPanel();
jp1.add(jl_id);
jp1.add(jt_id);
jl_password = new JLabel("密码");
jt_password = new JTextField(15);
jp2 = new JPanel();
jp2.add(jl_password);
jp2.add(jt_password);
jl3 = new JLabel("学生管理系统");
jp3 = new JPanel();
jp3.add(jl3);
jp_group = new JPanel();
radioButton_student = new JRadioButton("学生");
jp_group.add(radioButton_student);
radioButton_teacher = new JRadioButton("老师");
jp_group.add(radioButton_teacher);
radioButton_manage = new JRadioButton("教务");
jp_group.add(radioButton_manage);
radioButton_administration = new JRadioButton("管理员");
jp_group.add(radioButton_administration);
group = new ButtonGroup(); //创建按钮组保证3个按钮一次只能按下一个
group.add(radioButton_student);
group.add(radioButton_teacher);
group.add(radioButton_manage);
group.add(radioButton_administration);
jb_login = new JButton("登录");
jp4 = new JPanel();
jp4.add(jb_login);
jf.add(jp_img);
jf.add(jp3);
jf.add(jp1);
jf.add(jp2);
jf.add(jp_group);
jf.add(jp4);
jf.setVisible(true);
//登录按钮监听
jb_login.addActionListener(new ActionListener() {
@SuppressWarnings("unused")
public void actionPerformed(ActionEvent e) {
boolean flow = true;
//学生
if (radioButton_student.isSelected()) {
if (IOStreamStudent.file.length() == 0) {
JOptionPane.showMessageDialog(null, "用户名不存在", "提示信息",JOptionPane.ERROR_MESSAGE);
} else {
ArrayList arr = IOStreamStudent.readFromFile();
Iterator it = arr.iterator();
while (it.hasNext()) {
Student newstu = it.next();
if (newstu.getId().equals(jt_id.getText())) {
if (newstu.getPassword().equals(jt_password.getText())) {
new StudentLogin(newstu);
} else {
JOptionPane.showMessageDialog(null,"密码或用户名错误");
}
flow = false;
}
}if(flow){
JOptionPane.showMessageDialog(null, "用户名不存在");
}
}
}
//老师
if (radioButton_teacher.isSelected()) {
if (IOStreamTeacher.file.length() == 0) {
JOptionPane.showMessageDialog(null, "用户名不存在", "提示信息",JOptionPane.ERROR_MESSAGE);
} else {
ArrayList arr = IOStreamTeacher.readFromFile();
Iterator it = arr.iterator();
while (it.hasNext()) {
Teacher newtea = it.next();
if (newtea.getId().equals(jt_id.getText())) {
if (newtea.getPassword().equals(jt_password.getText())) {
new TeacherLogin(newtea);
} else {
JOptionPane.showMessageDialog(null,"密码或用户名错误");
}
flow = false;
}
}
if (flow) {
JOptionPane.showMessageDialog(null, "用户名不存在");
}
}
}
//教务
if (radioButton_manage.isSelected()) {
if(IOStreamManage.file.length() == 0){
JOptionPane.showMessageDialog(null, "用户名不存在","提示信息",JOptionPane.ERROR_MESSAGE);
}else{
ArrayList arr = IOStreamManage.readFromFile();
Iterator it = arr.iterator();
while (it.hasNext()) {
Manage newman = it.next();
if (newman.getId().equals(jt_id.getText())) {
if (newman.getPassword().equals(jt_password.getText())) {
new ManageLogin(newman);
} else {
JOptionPane.showMessageDialog(null, "密码或用户名错误");
}
flow = false;
}
}if(flow){
JOptionPane.showMessageDialog(null, "用户名不存在");
}
}
}
//系统管理员
if (radioButton_administration.isSelected()) {
if (administration_id.equals(jt_id.getText())) {
if (administration_password.equals(jt_password.getText())) {
new Register();
} else {
JOptionPane.showMessageDialog(null, "密码错误");
}
} else {
JOptionPane.showMessageDialog(null, "请重新登录");
}
}
}
});
}
//学生查寻返回元素下标
public static int searchStudent(){
int index = -1;
String userid = jt_id.getText();
if(IOStreamStudent.file.length() == 0){
return index;
}else{
ArrayList arr = IOStreamStudent.readFromFile();
for(int i=0; iif(userid.equals(arr.get(i).getId())){
index = i;
return index;
}
}
}
return index;
}
//老师查询返回元素下标
public static int searchTeacher(){
int index = -1;
String userid = jt_id.getText();
if(IOStreamTeacher.file.length() == 0){
return index;
}else{
ArrayList arr = IOStreamTeacher.readFromFile();
for(int i=0; iif(userid.equals(arr.get(i).getId())){
index = i;
return index;
}
}
}
return index;
}
//管理员查询返回下标
public static int searchManage(){
int index = -1;
String userid = jt_id.getText();
if(IOStreamManage.file.length() == 0){
return index;
}else{
ArrayList arr = IOStreamManage.readFromFile();
for(int i=0; iif(userid.equals(arr.get(i).getId())){
index = i;
return index;
}
}
}
return index;
}
}
在登录页面点击管理员按钮后进入到此页面,进行创建角色的创建。
package com.file;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.ButtonGroup;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Register {
JFrame jf;
JLabel jl_id, jl_password, jl_age, jl_sex, jl5;
JTextField jt_id, jt_password, jt_age, jt_sex;
JPanel jp1, jp2, jp3, jp4, jp5, jp6, jp_group, jp_img;
JButton jb1;
ButtonGroup group;
JRadioButton radioButton_student, radioButton_teacher, radioButton_manage;
public Register() {
jf = new JFrame("注册界面");
jf.setSize(500, 700);
jf.setLocation(1300,100);
jf.setVisible(true);
jf.setLayout(new GridLayout(7, 2));
Icon img = new ImageIcon("tu1.gif");
jp_img = new JPanel();
jp_img.add(new JLabel(img));
jl_id = new JLabel("用户名");
jt_id = new JTextField(15);
jp1 = new JPanel();
jp1.add(jl_id);
jp1.add(jt_id);
jl_password = new JLabel("密码");
jt_password = new JTextField(15);
jp2 = new JPanel();
jp2.add(jl_password);
jp2.add(jt_password);
jp_group = new JPanel();
radioButton_student = new JRadioButton("学生");
jp_group.add(radioButton_student);
radioButton_teacher = new JRadioButton("老师");
jp_group.add(radioButton_teacher);
radioButton_manage = new JRadioButton("教务");
jp_group.add(radioButton_manage);
group = new ButtonGroup();
group.add(radioButton_student);
group.add(radioButton_teacher);
group.add(radioButton_manage);
jb1 = new JButton("完成");
jp5 = new JPanel();
jp5.add(jb1);
jl5 = new JLabel("注册成功");
jp6 = new JPanel();
jf.add(jp_img);
jf.add(jp1);
jf.add(jp2);
jf.add(jp_group);
jf.add(jp5);
jf.add(jp6);
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//学生
if (radioButton_student.isSelected()) {
Student stu = new Student(jt_id.getText(), jt_password
.getText());
if (IOStreamStudent.file.length() == 0) {
DB.arrStu.add(stu);
IOStreamStudent.writeToFile();
jp6.add(jl5);
jf.setVisible(true);
jt_id.setText("");
jt_password.setText("");
} else {
DB.arrStu.clear();
DB.arrStu = IOStreamStudent.readFromFile();
Iterator it = DB.arrStu.iterator();
boolean flag = true;
while (it.hasNext()) {
Student newstu = it.next();
if (newstu.getId().equals(jt_id.getText())) {
flag = false;
JOptionPane.showMessageDialog(null,
"此用户名已存在,请重新输入");
}
}
if (flag) {
if (!(jt_id.getText().isEmpty() || jt_password
.getText().isEmpty())) {
DB.arrStu.add(stu);
IOStreamStudent.writeToFile();
jp6.add(jl5);
jf.setVisible(true);
jt_id.setText("");
jt_password.setText("");
} else {
JOptionPane.showMessageDialog(null,
"密码,用户名不能为空");
}
}
}
}
//老师
if (radioButton_teacher.isSelected()) {
Teacher tea = new Teacher(jt_id.getText(), jt_password
.getText());
if (IOStreamTeacher.file.length() == 0) {
DB.arrTea.add(tea);
IOStreamTeacher.writeToFile();
jp6.add(jl5);
jf.setVisible(true);
jt_id.setText("");
jt_password.setText("");
} else {
DB.arrTea.clear();
DB.arrTea = IOStreamTeacher.readFromFile();
Iterator it = DB.arrTea.iterator();
boolean flag = true;
while (it.hasNext()) {
Teacher newtea = it.next();
if (newtea.getId().equals(jt_id.getText())) {
flag = false;
JOptionPane.showMessageDialog(null,
"此用户名已存在,请重新输入");
}
}
if (flag) {
if (!(jt_id.getText().isEmpty() || jt_password
.getText().isEmpty())) {
DB.arrTea.add(tea);
IOStreamTeacher.writeToFile();
jp6.add(jl5);
jf.setVisible(true);
jt_id.setText("");
jt_password.setText("");
} else {
JOptionPane.showMessageDialog(null,
"密码,用户名不能为空");
}
}
}
}
//管理员
if (radioButton_manage.isSelected()) {
Manage man = new Manage(jt_id.getText(), jt_password
.getText());
if (IOStreamManage.file.length() == 0) {
DB.arrMan.add(man);
IOStreamManage.writeToFile();
jp6.add(jl5);
jf.setVisible(true);
jt_id.setText("");
jt_password.setText("");
} else {
DB.arrMan.clear();
DB.arrMan = IOStreamManage.readFromFile();
Iterator it = DB.arrMan.iterator();
boolean flag = true;
while (it.hasNext()) {
Manage newman = it.next();
if (newman.getId().equals(jt_id.getText())) {
flag = false;
JOptionPane.showMessageDialog(null,
"此用户名已存在,请重新输入");
}
}
if (flag) {
if (!(jt_id.getText().isEmpty() || jt_password
.getText().isEmpty())) {
DB.arrMan.add(man);
IOStreamManage.writeToFile();
jp6.add(jl5);
jf.setVisible(true);
jt_id.setText("");
jt_password.setText("");
} else {
JOptionPane.showMessageDialog(null,
"密码,用户名不能为空");
}
}
}
}
}
});
}
}
每个角色的显示界面不一样所以创建3个类,显示界面里包括了需要的内容。
创建菜单选项,下设信息,成绩。
package com.file;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
public class StudentLogin {
JFrame jf;
JLabel jl_name, jl11, jl_age, jl21, jl_sex, jl31, jl_ReviseOK,jl_old_password,jl_new_password1,jl_new_password2,jl_inpassword_err;
JTextField jt_name, jt_age, jt_sex;
JPasswordField jp_old_password,jpassword1,jpassword2;
JPanel jp1, jp2, jp3, jp4, jp_jb, jp_img,jp_scroll,jp_center;
JButton jb,jb_revise_password;
static JTable table;
JScrollPane scrollPane;
static DefaultTableModel model;
JMenuBar jmb;
JMenuItem jmb1find,jmb2find,jmb1revisePassword;
public StudentLogin(final Student stu) {
jf = new JFrame("欢迎 " + stu.getId() + " 同学,你好!");
jf.setSize(1000, 700);
jf.setLocation(800, 100);
jf.setLayout(new BorderLayout(0,0));
Icon img = new ImageIcon("tu2.jpg");
jf.add(new JLabel(img),BorderLayout.NORTH);
jmb = new JMenuBar(); //创建菜单
jf.setJMenuBar(jmb);
JMenu jmb1 = new JMenu("个人信息");
jmb.add(jmb1);
jmb1find = new JMenuItem("查看个人信息");
jmb1.add(jmb1find);
jmb1revisePassword = new JMenuItem("修改账户密码");
jmb1.add(jmb1revisePassword);
JMenu jmb2 = new JMenu("我的成绩");
jmb.add(jmb2);
jmb2find = new JMenuItem("查看所有");
jmb2.add(jmb2find);
jp_center = new JPanel();//中心的面板
jf.setVisible(true);
jmb1find.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jp_center.removeAll();
jp_center.setLayout(new GridLayout(7,2));
jf.add(jp_center,BorderLayout.CENTER);
jl_name = new JLabel("姓名");
jt_name = new JTextField(20);
jt_name.setText(stu.getName());
jp1 = new JPanel();
jp1.add(jl_name);
jp1.add(jt_name);
jl_age = new JLabel("年龄");
jt_age = new JTextField(20);
jt_age.setText(stu.getAge());
jp2 = new JPanel();
jp2.add(jl_age);
jp2.add(jt_age);
jl_sex = new JLabel("性别");
jt_sex = new JTextField(20);
jt_sex.setText(stu.getSex());
jp3 = new JPanel();
jp3.add(jl_sex);
jp3.add(jt_sex);
jl_ReviseOK = new JLabel("修改成功");
jp4 = new JPanel();
jb = new JButton("修改信息");
jp_jb = new JPanel();
jp_jb.add(jb);
jp_center.add(jp1);
jp_center.add(jp2);
jp_center.add(jp3);
jp_center.add(jp_jb);
jp_center.add(jp4);
jf.setVisible(true);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
stu.setName(jt_name.getText());
stu.setAge(jt_age.getText());
stu.setSex(jt_sex.getText());
Student newstu = stu;
DB.arrStu.clear();
DB.arrStu = IOStreamStudent.readFromFile();
DB.arrStu.set(Login.searchStudent(), newstu);
IOStreamStudent.writeToFile();
jp4.add(jl_ReviseOK);
jf.setVisible(true);
}
});
}
});
jmb1revisePassword.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jp_center.removeAll();
jp_center.setLayout(new GridLayout(6,2));
jf.add(jp_center, BorderLayout.CENTER);
jl_old_password = new JLabel("原密码");
jp_old_password = new JPasswordField(20);
jl_new_password1 = new JLabel("新密码");
jpassword1 = new JPasswordField(20);
jl_new_password2 = new JLabel("再次输入新密码");
jpassword2 = new JPasswordField(20);
jb_revise_password = new JButton("保存");
jl_inpassword_err = new JLabel("密码输入错误,请重新输入");
jl_inpassword_err.setForeground(Color.red);
jl_inpassword_err.setFont(new Font("宋体",Font.ITALIC,16));
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
final JPanel jp4 = new JPanel();
final JPanel jp5 = new JPanel();
JPanel jp_jb = new JPanel();
jp1.add(jl_old_password);
jp1.add(jp_old_password);
jp_center.add(jp1);
jp2.add(jl_new_password1);
jp2.add(jpassword1);
jp_center.add(jp2);
jp3.add(jl_new_password2);
jp3.add(jpassword2);
jp_center.add(jp3);
jp_center.add(jp4);
jp_jb.add(jb_revise_password);
jp_center.add(jp_jb);
jp_center.add(jp5);
jf.setVisible(true);
jb_revise_password.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (String.valueOf(jp_old_password.getPassword()).equals(stu.getPassword())
&& (!(String.valueOf(jpassword2.getPassword())).equals(""))
&& ((String.valueOf(jpassword1.getPassword())).equals(String.valueOf(jpassword2.getPassword())))) {
stu.setPassword(String.valueOf(jpassword2.getPassword()));
Student newstu = stu;
DB.arrStu.clear();
DB.arrStu = IOStreamStudent.readFromFile();
DB.arrStu.set(Login.searchStudent(), newstu);
IOStreamStudent.writeToFile();
jp5.removeAll();
jp5.add(new JLabel("修改密码成功"));
jf.setVisible(true);
} else {
jp4.removeAll();
jp4.add(jl_inpassword_err);
jf.setVisible(true);
}
}
});
}
});
jmb2find.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("查看所有成绩");
jp_center.removeAll();
jp_center.setLayout(new GridLayout());
scrollPane = new JScrollPane();
jp_center.add(scrollPane);
jf.add(jp_center, BorderLayout.CENTER);
table = new JTable(); //创建一个表显示数据
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
table.setRowHeight(30);
JTableHeader header = table.getTableHeader(); // 获取 JTable 的头的对象
header.setFont(new Font("微软雅黑", Font.PLAIN, 16));
header.setPreferredSize(new Dimension(header.getWidth(), 35)); // 设置此组件的首选大小
scrollPane.setViewportView(table);
model = (DefaultTableModel) table.getModel();
model.setColumnIdentifiers(new Object[] { "课程编号", "课程名", "成绩","任课老师" });
CourseManage.findOneStudentAllCourseMark(stu);
jf.setVisible(true);
}
});
}
}
看图,顺便把学生信息修改。
这里成绩是没有的,因为老师还没有分配科目。
老师这里包含了个人信息显示和修改,修改学生成绩和查看所有学生成绩
package com.file;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
public class TeacherLogin {
static JFrame jf;
JLabel jl_name,jl11,jl_age,jl21,jl_sex,jl31,jl4,jl_course,jl_old_password,jl_new_password1,jl_new_password2,jl_inpassword_err;
JTextField jt_name,jt_age,jt_sex;
JPasswordField jp_old_password,jpassword1,jpassword2;
JPanel jp1,jp2,jp3,jp4,jp_jb;
static JPanel jp_center,jp_south;
JPanel jp_course;
JButton jb,jb_revise,jb_revise_password;
static JScrollPane scrollPane;
static JTable table;
static DefaultTableModel model;
JMenuBar jmb;
JMenuItem jmb1find,jmb2find,jmb2revise,jmb1revisePassword;
JMenu jmb1,jmb2;
public TeacherLogin(){}
public TeacherLogin(final Teacher tea){
jf = new JFrame(tea.getId()+" 老师,您好!");
jf.setSize(1000,700);
jf.setLocation(800,100);
jf.setLayout(new BorderLayout(0,0));
Icon img = new ImageIcon("tu2.jpg");
JLabel jl = new JLabel(img);
jf.add(jl,BorderLayout.NORTH);
jmb = new JMenuBar();
jf.setJMenuBar(jmb);
JMenu jmb1 = new JMenu("个人信息");
jmb.add(jmb1);
jmb1find = new JMenuItem("查看个人信息");
jmb1.add(jmb1find);
jmb1revisePassword = new JMenuItem("修改账户密码");
jmb1.add(jmb1revisePassword);
JMenu jmb2 = new JMenu("学生成绩");
jmb.add(jmb2);
jmb2find = new JMenuItem("查看所有");
jmb2.add(jmb2find);
jmb2revise = new JMenuItem("修改成绩");
jmb2.add(jmb2revise);
jp_center = new JPanel();
jp_south = new JPanel();
jf.setVisible(true);
//查看并修改个人信息
jmb1find.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jp_center.removeAll();
jp_center.setLayout(new GridLayout(7,2));
jf.add(jp_center,BorderLayout.CENTER);
jl_name = new JLabel("姓名");
jt_name = new JTextField(20);
jt_name.setText(tea.getName());
jp1 =new JPanel();
jp1.add(jl_name);
jp1.add(jt_name);
jl_age = new JLabel("年龄");
jt_age = new JTextField(20);
jt_age.setText(tea.getAge());
jp2 =new JPanel();
jp2.add(jl_age);
jp2.add(jt_age);
jl_sex = new JLabel("性别");
jt_sex = new JTextField(20);
jt_sex.setText(tea.getSex());
jp3 =new JPanel();
jp3.add(jl_sex);
jp3.add(jt_sex);
if( tea.getTeaching_course() == null ){
jl_course = new JLabel("教授课程 "+"无");
jl_course.setForeground(Color.red);
jl_course.setFont(new Font("宋体",Font.ITALIC,16));
}else if(tea.getTeaching_course().equals("无")){
jl_course = new JLabel("教授课程 "+"无");
jl_course.setForeground(Color.red);
jl_course.setFont(new Font("宋体",Font.ITALIC,16));
} else {
jl_course = new JLabel("教授课程 "+tea.getTeaching_course().getCourseName());
}
jp_course = new JPanel();
jp_course.add(jl_course);
jl4 = new JLabel("修改成功");
jp4 = new JPanel();
jb = new JButton("修改信息");
jp_jb = new JPanel();
jp_jb.add(jb);
jp_center.add(jp1);
jp_center.add(jp2);
jp_center.add(jp3);
jp_center.add(jp_course);
jp_center.add(jp_jb);
jp_center.add(jp4);
jf.setVisible(true);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(tea.toString());
tea.setName(jt_name.getText());
tea.setAge(jt_age.getText());
tea.setSex(jt_sex.getText());
if(tea.getTeaching_course() == null){
tea.setTeaching_course(new Course());
}else{
tea.setTeaching_course(tea.getTeaching_course());
}
Teacher newtea = tea;
DB.arrTea.clear();
DB.arrTea = IOStreamTeacher.readFromFile();
DB.arrTea.set(Login.searchTeacher(),newtea);
IOStreamTeacher.writeToFile();
jp4.add(jl4);
jf.setVisible(true);
}
});
}
});
//修改密码
jmb1revisePassword.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jp_center.removeAll();
jp_center.setLayout(new GridLayout(6,2));
jf.add(jp_center, BorderLayout.CENTER);
jl_old_password = new JLabel("原密码");
jp_old_password = new JPasswordField(20);
jl_new_password1 = new JLabel("新密码");
jpassword1 = new JPasswordField(20);
jl_new_password2 = new JLabel("再次输入新密码");
jpassword2 = new JPasswordField(20);
jb_revise_password = new JButton("保存");
jl_inpassword_err = new JLabel("密码输入错误,请重新输入");
jl_inpassword_err.setForeground(Color.red);
jl_inpassword_err.setFont(new Font("宋体",Font.ITALIC,16));
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
final JPanel jp4 = new JPanel();
final JPanel jp5 = new JPanel();
JPanel jp_jb = new JPanel();
jp1.add(jl_old_password);
jp1.add(jp_old_password);
jp_center.add(jp1);
jp2.add(jl_new_password1);
jp2.add(jpassword1);
jp_center.add(jp2);
jp3.add(jl_new_password2);
jp3.add(jpassword2);
jp_center.add(jp3);
jp_center.add(jp4);
jp_jb.add(jb_revise_password);
jp_center.add(jp_jb);
jp_center.add(jp5);
jf.setVisible(true);
jb_revise_password.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (String.valueOf(jp_old_password.getPassword()).equals(tea.getPassword())
&& (!(String.valueOf(jpassword2.getPassword())).equals(""))
&& ((String.valueOf(jpassword1.getPassword())).equals(String.valueOf(jpassword2.getPassword())))) {
tea.setPassword(String.valueOf(jpassword2.getPassword()));
Teacher newstu = tea;
DB.arrTea.clear();
DB.arrTea = IOStreamTeacher.readFromFile();
DB.arrTea.set(Login.searchTeacher(), newstu);
IOStreamTeacher.writeToFile();
jp5.removeAll();
jp5.add(new JLabel("修改密码成功"));
jf.setVisible(true);
} else {
jp4.removeAll();
jp4.add(jl_inpassword_err);
jf.setVisible(true);
}
}
});
}
});
//查看所有学生这门课成绩
jmb2find.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("查看所有学生成绩");
TeacherLogin.Excel();
model = (DefaultTableModel) table.getModel();
model.setColumnIdentifiers(new Object[] { "学生学号","学生姓名","《"+tea.getTeaching_course().getCourseName()+"》 成绩"});
CourseManage.allStudentOneCourse_mark(tea);
jf.setVisible(true);
}
});
//老师给学生打分
jmb2revise.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("修改成绩");
if (tea.getTeaching_course().courseName == null
|| tea.getTeaching_course().courseName.equals("null")) {
JOptionPane.showMessageDialog(null, "请先让教务人员为您分配科目", "",
JOptionPane.WARNING_MESSAGE);
} else {
TeacherLogin.Excel();
model = (DefaultTableModel) table.getModel();
model.setColumnIdentifiers(new Object[] { "学生学号","学生姓名","《"+tea.getTeaching_course().getCourseName()+"》 成绩"});
CourseManage.allStudentOneCourse_mark(tea);
jb_revise = new JButton("修改成绩");
jp_south.add(jb_revise);
jf.add(jp_south,BorderLayout.SOUTH);
jf.setVisible(true);
jb_revise.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
CourseManage.teacherReviseCourse_mark(tea);
}
});
}
}
});
}
//表格
public static void Excel(){
jp_center.removeAll();
jp_south.removeAll();
jp_center.setLayout(new GridLayout());
scrollPane = new JScrollPane();
jp_center.add(scrollPane);
jf.add(jp_center,BorderLayout.CENTER);
table = new JTable();
// 将表的选择模式设置为只允许单个选择、单个连续间隔选择或多间隔选择。
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setShowGrid(true);
table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
table.setRowHeight(30);
JTableHeader header = table.getTableHeader(); // 获取 JTable 的头的对象
header.setFont(new Font("微软雅黑", Font.PLAIN, 16));
header.setPreferredSize(new Dimension(header.getWidth(), 35)); // 设置此组件的首选大小
scrollPane.setViewportView(table);
}
}
看图,显示信息,因为教务人员没有给老师分配教授的科目,所以科目一栏为无。
查看学生成绩,这里只有注册的学生1,点击修改成绩,显示错误,因为教务人员没有给老师分配教授的科目。
有菜单选项自己信息,学生(查看,修改,删除),老师(查看,修改,删除)。
package com.file;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
public class ManageLogin {
static JFrame jf;
JLabel jl_name, jl11, jl21,jl31, jl4,jl_password;
JPasswordField jpassword;
JTextField jt_name;
JPanel jp1, jp2, jp4, jp_jb, jp_img,jp_scroll;
static JPanel jp_center,jp_south;
JButton jb,jb_revise_teacher;
static JTable table;
static JScrollPane scrollPane;
static DefaultTableModel model;
JMenuBar jmb;
JMenuItem jmb1findStudentMessage,jmb2findTeacherMessage,jmb1reviseStudentMessage,jmb2revise,jmb0message,jmb1deleteStudent,jmb2deleteTeacher;
public ManageLogin(final Manage man) {
jf = new JFrame("欢迎 " + man.getId() + " 教务管理员,你好!");
jf.setSize(1000, 700);
jf.setLocation(800, 100);
jf.setLayout(new BorderLayout(0,0));
Icon img = new ImageIcon("tu2.jpg");
jf.add(new JLabel(img),BorderLayout.NORTH);
jmb = new JMenuBar();
jf.setJMenuBar(jmb);
JMenu jmb0 = new JMenu("我的信息");
jmb.add(jmb0);
jmb0message = new JMenuItem("查看和修改");
jmb0.add(jmb0message);
JMenu jmb1 = new JMenu("学生管理");
jmb.add(jmb1);
jmb1findStudentMessage = new JMenuItem("查看学生信息");
jmb1.add(jmb1findStudentMessage);
jmb1reviseStudentMessage = new JMenuItem("修改学生信息");
jmb1.add(jmb1reviseStudentMessage);
jmb1deleteStudent = new JMenuItem("删除学生信息");
jmb1.add(jmb1deleteStudent);
JMenu jmb2 = new JMenu("老师管理");
jmb.add(jmb2);
jmb2findTeacherMessage = new JMenuItem("查看老师信息");
jmb2.add(jmb2findTeacherMessage);
jmb2revise = new JMenuItem("修改老师信息");
jmb2.add(jmb2revise);
jmb2deleteTeacher = new JMenuItem("删除老师信息");
jmb2.add(jmb2deleteTeacher);
jp_center = new JPanel();//中心的面板
jp_south = new JPanel();//下方的面板
jf.setVisible(true);
//查看和修改自己信息
jmb0message.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jp_center.removeAll();
jp_center.setLayout(new GridLayout(7,2));
jf.add(jp_center,BorderLayout.CENTER);
jl_name = new JLabel("姓名");
jt_name = new JTextField(20);
jt_name.setText(man.getName());
jp1 = new JPanel();
jp1.add(jl_name);
jp1.add(jt_name);
jl_password = new JLabel("密码");
jpassword = new JPasswordField(20);
jpassword.setText(man.getPassword());
jp2 = new JPanel();
jp2.add(jl_password);
jp2.add(jpassword);
jl4 = new JLabel("修改成功");
jp4 = new JPanel();
jb = new JButton("修改信息");
jp_jb = new JPanel();
jp_jb.add(jb);
jp_center.add(jp1);
jp_center.add(jp2);
jp_center.add(jp_jb);
jp_center.add(jp4);
jf.setVisible(true);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
man.setName(jt_name.getText());
man.setPassword(String.valueOf(jpassword.getPassword()));
Manage newman = man;
DB.arrMan.clear();
DB.arrMan = IOStreamManage.readFromFile();
DB.arrMan.set(Login.searchManage(), newman);
IOStreamManage.writeToFile();
jp4.add(jl4);
jf.setVisible(true);
}
});
}
});
//查看学生信息
jmb1findStudentMessage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("查看所有学生信息");
ManageLogin.Excel();
model = (DefaultTableModel) table.getModel();
model.setColumnIdentifiers(new Object[] { "用户名", "姓名", "年龄","性别" });
CourseManage.findAllStudentMessage();
jp_center.add(scrollPane);
jf.setVisible(true);
}
});
//修改学生信息
jmb1reviseStudentMessage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
CourseManage.jmb1reviseStudentMessage();
}
});
//删除学生
jmb1deleteStudent.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
CourseManage.deleteStudent();
}
});
//查看老师信息
jmb2findTeacherMessage.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("查看所有老师信息");
ManageLogin.Excel();
model = (DefaultTableModel) table.getModel();
model.setColumnIdentifiers(new Object[] { "用户名", "姓名", "年龄", "性别", "课程编号" ,"教授课程" });
CourseManage.findAllTeacherMessage();
jp_center.add(scrollPane);
jf.setVisible(true);
}
});
//修改老师信息
jmb2revise.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ManageLogin.Excel();
model = (DefaultTableModel) table.getModel();
model.setColumnIdentifiers(new Object[] { "用户名", "姓名", "年龄","性别", "课程编号", "教授课程" });
CourseManage.findAllTeacherMessage();
jb_revise_teacher = new JButton("修改");
jp_south.add(jb_revise_teacher);
jf.add(jp_south,BorderLayout.SOUTH);
jf.setVisible(true);
jb_revise_teacher.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
CourseManage.teacherReviseJudge();
}
});
}
});
//删除老师
jmb2deleteTeacher.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
CourseManage.deleteTeacher();
}
});
}
//表格模型
public static void Excel(){
jp_center.removeAll();
jp_south.removeAll();
jp_center.setLayout(new GridLayout());
scrollPane = new JScrollPane();
jp_center.add(scrollPane);
jf.add(jp_center, BorderLayout.CENTER);
jf.setVisible(true);
table = new JTable();
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
table.setRowHeight(30);
JTableHeader header = table.getTableHeader(); // 获取 JTable 的头的对象
header.setFont(new Font("微软雅黑", Font.PLAIN, 16));
header.setPreferredSize(new Dimension(header.getWidth(), 35)); // 设置此组件的首选大小
scrollPane.setViewportView(table);
}
}
查看学生
修改学生信息
删除学生
查看老师,此时老师没有安排课程
修改老师信息,顺便安排课程,这里安排老师1上《高等数学》,这里要是想要修改别的都可以加
修改之后,老师科目已经安排,老师1教高等数学
这时登录老师1的界面给学生修改成绩,科目栏已经变为《高等数学》。
登录学生1端口查看成绩
package com.file;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class CourseManage {
static JFrame jf ;
static JPanel jp ;
static JLabel jl ;
static JButton jb ;
static JTextField jt_name,jt_id,jt_pass;
//老师查找所有学生单科成绩
public static void allStudentOneCourse_mark(Teacher tea) {
ArrayList arr = IOStreamStudent.readFromFile();
Iterator itarr = arr.iterator();
String course_mark = null;// 查找的课程
while (itarr.hasNext()) {
Student stu = itarr.next(); //一个循环内不能存在2个.next()方法
String id = stu.getId();
String name = stu.getName();
for(int i = 0;i
if (tea.getTeaching_course().getCourseName()
.equals(stu.arr_course.get(i).getCourseName())) {
course_mark = stu.arr_course.get(i).getCourseMark();
break;
}
}
TeacherLogin.model.addRow(new Object[] { id, name, course_mark });
TeacherLogin.table.setModel(TeacherLogin.model);
}
}
//老师修改学生单科成绩
public static void teacherReviseCourse_mark(Teacher tea) {
if (tea.getTeaching_course().courseName == null
|| tea.getTeaching_course().courseName.equals("null")) {
JOptionPane.showMessageDialog(null, "请先让教务人员为您分配科目", "",
JOptionPane.WARNING_MESSAGE);
} else {
int index = 0;
String student_course_mark = null;
Course newcourse = null;
index = TeacherLogin.table.getSelectedRow();
if (index == -1) {
JOptionPane.showMessageDialog(null, "请选择要修改成绩的学生", "",
JOptionPane.WARNING_MESSAGE);
} else {
String id = (String) TeacherLogin.table.getValueAt(index, 0);
DB.arrStu.clear();
DB.arrStu = IOStreamStudent.readFromFile();
student_course_mark = JOptionPane.showInputDialog("成绩=", 0);
for (int i = 0; i < DB.arrStu.size(); i++) {
if (id.equals(DB.arrStu.get(i).getId())) {
if (DB.arrStu.get(i).arr_course.size() == 0) { //成绩集合为空,添加第一个数据
newcourse = new Course(tea.getTeaching_course()
.getCourseId(), tea.getTeaching_course()
.getCourseName(), student_course_mark);
DB.arrStu.get(i).arr_course.add(newcourse);
} else {
for (int j = 0; j < DB.arrStu.get(i).arr_course.size(); j++) { //这门课程已经创建,进行修改
if (DB.arrStu.get(i).arr_course.get(j).getCourseName()
.equals(tea.getTeaching_course().getCourseName())) {
newcourse = new Course(
tea.getTeaching_course()
.getCourseId(), tea
.getTeaching_course()
.getCourseName(),
student_course_mark);
DB.arrStu.get(i).arr_course.set(j,newcourse);
break;
} else { //这门课程没有创建时,进行添加
newcourse = new Course(tea.getTeaching_course()
.getCourseId(), tea.getTeaching_course()
.getCourseName(), student_course_mark);
DB.arrStu.get(i).arr_course.add(newcourse);
}
}
}
IOStreamStudent.writeToFile();
TeacherLogin.model.removeRow(TeacherLogin.table.getSelectedRow());
TeacherLogin.model.insertRow(index,new Object[] { DB.arrStu.get(i).getId(),DB.arrStu.get(i).getName(),student_course_mark });
TeacherLogin.table.setModel(TeacherLogin.model);
break;
}
}
}
}
}
//一个学生的所有成绩
public static void findOneStudentAllCourseMark(Student stu) {
ArrayList arrStu = IOStreamStudent.readFromFile();
Iterator it = arrStu.iterator();
String teacher_name = null;
if(stu.arr_course == null) {
JOptionPane.showMessageDialog(null, "无成绩,请等待老师给定", "",JOptionPane.WARNING_MESSAGE);
} else {
ArrayList arrTea = IOStreamTeacher.readFromFile();
while(it.hasNext()) {
Student newstu = it.next();
if(newstu.getId().equals(stu.getId())) {
for(int i = 0;i
String course_id = newstu.arr_course.get(i).getCourseId();
String course_name = newstu.arr_course.get(i).getCourseName();
String course_mark = newstu.arr_course.get(i).getCourseMark();
for(int j = 0;j
if(course_name.equals(arrTea.get(j).getTeaching_course().getCourseName())) {
teacher_name = arrTea.get(j).getName();
StudentLogin.model.addRow(new Object[] {course_id,course_name,course_mark,teacher_name});
StudentLogin.table.setModel(StudentLogin.model);
}
}
}
break;
}
}
}
}
//查看所有学生信息
public static void findAllStudentMessage(){
ArrayList arr = IOStreamStudent.readFromFile();
Iterator it = arr.iterator();//next()方法不能在循环内使用2次
while(it.hasNext()){
Student stu = it.next();
String id = stu.getId();
String name = stu.getName();
String sex = stu.getSex();
String age = stu.getAge();
ManageLogin.model.addRow(new Object[] {id,name,sex,age});
ManageLogin.table.setModel(ManageLogin.model);
}
}
//修改学生信息
public static void jmb1reviseStudentMessage(){
jf = new JFrame("修改学生信息");
jf.setLocation(1000, 200);
jf.setSize(400, 300);
jf.setLayout(new GridLayout(4, 1));
jp = new JPanel();
jl = new JLabel("输入要修改的用户名:");
jt_id = new JTextField(15);
jp.add(jl);
jp.add(jt_id);
jf.add(jp);
jl = new JLabel("用户密码:");
jt_pass = new JTextField(15);
jp = new JPanel();
jp.add(jl);
jp.add(jt_pass);
jf.add(jp);
jb = new JButton("确定修改");
jp = new JPanel();
jp.add(jb);
jf.add(jp);
jf.setVisible(true);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jf.dispose();
DB.arrStu.clear();
DB.arrStu = IOStreamStudent.readFromFile();
for(int i = 0;i
if(DB.arrStu.get(i).getId().equals(jt_id.getText())){
DB.arrStu.get(i).setId(jt_id.getText());
DB.arrStu.get(i).setPassword(jt_pass.getText());
IOStreamStudent.writeToFile();
}
}
}
});
}
//删除学生账户
public static void deleteStudent(){
String id = JOptionPane.showInputDialog("要删除的用户名", 0);
DB.arrStu.clear();
DB.arrStu = IOStreamStudent.readFromFile();
for(int i = 0;i
if(DB.arrStu.get(i).getId().equals(id)){
DB.arrStu.remove(i);
IOStreamStudent.writeToFile();
}
}
}
//删除老师账户
public static void deleteTeacher(){
String id = JOptionPane.showInputDialog("要删除的用户名", 0);
DB.arrTea.clear();
DB.arrTea = IOStreamTeacher.readFromFile();
for(int i = 0;i
if(DB.arrTea.get(i).getId().equals(id)){
DB.arrTea.remove(i);
IOStreamTeacher.writeToFile();
}
}
}
//查看所有老师信息
@SuppressWarnings("unused")
public static void findAllTeacherMessage() {
if (IOStreamTeacher.file.length() == 0) {
ManageLogin.model.addRow(new Object[] { "无" });
ManageLogin.table.setModel(ManageLogin.model);
} else {
ArrayList arr = IOStreamTeacher.readFromFile();
Iterator it = arr.iterator();
while (it.hasNext()) {
Teacher tea = it.next();
String id = tea.getId();
String name = tea.getName();
String sex = tea.getSex();
String age = tea.getAge();
if (tea.getTeaching_course() == null) {
ManageLogin.model.addRow(new Object[] { id, name, sex, age,
"无","无" });
ManageLogin.table.setModel(ManageLogin.model);
} else {
String teaching_course_id = tea.getTeaching_course()
.getCourseId();
String teaching_course_name = tea.getTeaching_course().getCourseName();
ManageLogin.model.addRow(new Object[] { id, name, sex, age,
teaching_course_id, teaching_course_name });
ManageLogin.table.setModel(ManageLogin.model);
}
}
}
}
//教务修改老师信息
//第一步教务修改老师信息判断条件
public static void teacherReviseJudge() {
int index = 0;
index = ManageLogin.table.getSelectedRow();
if (index == -1) {
JOptionPane.showMessageDialog(null, "请选择要修改的老师", "",
JOptionPane.WARNING_MESSAGE);
} else {
CourseManage.teacherReviseFrame(index);
}
}
//第二步教务修改老师信息面板
public static void teacherReviseFrame(final int index) {
jf = new JFrame("修改老师信息");
jf.setLocation(1000, 200);
jf.setSize(300, 400);
jf.setLayout(new GridLayout(4, 1));
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
JPanel jp4 = new JPanel();
jl = new JLabel("修改用户密码:");
jt_pass = new JTextField(15);
jp1.add(jl);
jp1.add(jt_pass);
jf.add(jp1);
jl = new JLabel("安排课程编号");
jt_id = new JTextField(15);
jp2.add(jl);
jp2.add(jt_id);
jf.add(jp2);
jl = new JLabel("课程名称");
jt_name = new JTextField(15);
jp3.add(jl);
jp3.add(jt_name);
jf.add(jp3);
jb = new JButton("确定修改");
jp4.add(jb);
jf.add(jp4);
jf.setVisible(true);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jf.dispose();
CourseManage.teacherRevise(index);
}
});
}
//第三步教务修改老师信息
public static void teacherRevise(int index) {
String teacher_course_id = null;
String teacher_course_name = null;
String id = (String) ManageLogin.table.getValueAt(index, 0);
DB.arrTea.clear();
DB.arrTea = IOStreamTeacher.readFromFile();
teacher_course_id = jt_id.getText();
teacher_course_name = jt_name.getText();
for (int i = 0; i < DB.arrTea.size(); i++) {
if (id.equals(DB.arrTea.get(i).getId())) {
if(!(jt_pass.getText().equals(""))){
DB.arrTea.get(i).setPassword(jt_pass.getText());
}
DB.arrTea.get(i).getTeaching_course()
.setCourseName(teacher_course_name);
DB.arrTea.get(i).getTeaching_course()
.setCourseId(teacher_course_id);
IOStreamTeacher.writeToFile();
ManageLogin.model.removeRow(index);
ManageLogin.model.insertRow(index, new Object[] {
DB.arrTea.get(i).getId(), DB.arrTea.get(i).getName(),
DB.arrTea.get(i).getAge(), DB.arrTea.get(i).getSex(),
teacher_course_id, teacher_course_name });
ManageLogin.table.setModel(ManageLogin.model);
break;
}
}
}
}
package com.file;
/*2017年12月
* 《教学管理系统》有学生,教师,教务3个角色。
* 只有一个系统管理员,用来添加学生,教师和教务人员帐号,帐号id为111,密码为111,请妥善保存。
*
*/
public class MainText {
public static void main(String[] args) {
new Login();
}
}