学生选课系统----StudentWindow

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了学生选课系统----StudentWindow相关的知识,希望对你有一定的参考价值。

  

/**
 * @author 李佩
 * 
 * @version v1
 * 
 * @time 2016/12/2 22:21
 * 
 * @program 画出学生选课系统的学生界面
 *
 */
package com.csms.windows;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.List;
import com.csms.dao.CourseArrangeDAO;
import com.csms.dao.SelectCourseDAO;
import com.csms.dao.StudentDAO;
import com.csms.entity.Student;
import com.csms.entity.CourseArrange;
import com.csms.entity.SelectCourse;

public class StudentWindow extends JFrame implements ActionListener {
	// 选课系统的学生窗口界面的大小
	private static final int WINDOWWIDE = 922;
	private static final int WINDOWHIGH = 570;
	// 从登录界面通过取出输入的用户名和用户密码,并通过查询取得该用户的所有的属性
	public Student student = null;
	// 判断打印课程的标志,flag!=1的时候就不能打印了,只能打印一次
	private static int courseFlag = 1;
	// 判断刷新选课的标志,flag!=1的时候就不能打印了,只能打印一次
	private static int flushFlag = 1;
	// 设置界面的table组件的变量
	private JTable table = null;
	private DefaultTableModel tableModel;
	private JButton addFlushBtn = new JButton("刷新课程"); // 定义按钮
	private JButton addSelectBtn = new JButton("确认选课"); // 定义按钮
	private JTextField courseIDText = null;

	// Student界面设置总函数
	public StudentWindow() {
		// 取得用户的所有信息
		student = new StudentDAO().searchStudentAll(LoginWindow.getLoginUserName(), LoginWindow.getLoginPassword());
		// 1.左部信息显示区
		this.rigthInformation(student);

		// 2.添加右边的信息栏边框
		this.liftInformation(student);

		// 3.设置窗口的属性
		PublicWindowSet.windowAttribute(this, WINDOWWIDE, WINDOWHIGH, null, "学生界面窗口");
	}

	// 左边窗口信息栏
	public void liftInformation(Student student) {
		// 1).添加一个文本标签
		Icon lan = new ImageIcon("src//images//icons//lan.png");
		JLabel label = new JLabel(lan);
		label.setBounds(-20, 2, 250, 50);
		this.add(label);

		// 2).添加一个照片显示的窗口
		// addPhotoLabel,addLabel也是PublicWindowSet类中封装好的标签添加函数
		String photoUrl = "src//images//students//" + student.getStuID() + ".jpg";
		PublicWindowSet.addPhotoLabel(this, 40, 70, 130, 170, photoUrl);
		// 添加学生信息标签
		PublicWindowSet.addLabel(this, 16, 40, 243, 200, 40, "姓名:   " + student.getStuName());
		PublicWindowSet.addLabel(this, 16, 40, 263, 200, 40, "学号:   " + student.getStuID());
		PublicWindowSet.addLabel(this, 16, 40, 283, 200, 40, "年龄:   " + student.getStuAge());
		PublicWindowSet.addLabel(this, 16, 40, 303, 200, 40, "性别:   " + student.getStuGender());
		PublicWindowSet.addLabel(this, 16, 40, 323, 200, 40, "班级:   " + student.getStuClass());
		PublicWindowSet.addLabel(this, 16, 40, 343, 200, 40, "学院:   " + student.getStuSdept());

		// 3).设置按钮
		// 修改密码按钮
		Icon psdIcon = new ImageIcon("src//images//icons//fixpsd.png");
		this.addButton(psdIcon, 1, 390, this);
		// 已选课程按钮
		Icon selectIcon = new ImageIcon("src//images//icons//seecourse.png");
		this.addButton(selectIcon, 2, 430, this);
		// 查看成绩按钮
		Icon gradeIcon = new ImageIcon("src//images//icons//seegrade.png");
		this.addButton(gradeIcon, 3, 470, this);

		// 4).设置分隔标签
		Icon away = new ImageIcon("src//images//icons//tree1.png");
		JLabel awayLabel = new JLabel(away);
		awayLabel.setBounds(200, 50, 10, 450);
		this.add(awayLabel);
	}

	// 右部列表信息
	public void rigthInformation(Student student) {
		// 设置JTabel的默认类型
		this.tableModel = new DefaultTableModel(new CourseList().userInfo, new CourseList().titles);
		// 添加JLabel组件
		this.table = new JTable(this.tableModel);
		// 禁止JLabel组件随着窗口的大小而改变
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		// JTabel组件表格的显示尺寸
		table.setPreferredScrollableViewportSize(new Dimension(690, 0));
		// 使列表不可以整列的进行移动
		table.getTableHeader().setReorderingAllowed(false);
		// 使列表框不可被编辑
		table.enable(false);
		// 给JTabel组件添加滑动条
		JScrollPane scr = new JScrollPane(this.table);

		// 设置面板
		JPanel toolBar = new JPanel();

		// 添加底部信息提示标签
		Icon bottom = new ImageIcon("src//images//icons//bottom.jpg");
		JLabel bottomLab = new JLabel(bottom);
		toolBar.add(bottomLab);

		// 添加课程号输入文本域
		courseIDText = new JTextField(25);
		toolBar.add(courseIDText);

		// 添加两个按钮:选课按钮和,刷新课程按钮
		toolBar.add(this.addFlushBtn);
		toolBar.add(this.addSelectBtn);

		// 将底部操作组件添加入面板的南部区域
		this.add(toolBar, BorderLayout.SOUTH);
		// 将带滚动条的Tabel组件添加入面板的东部区域
		this.add(scr, BorderLayout.EAST);

		// 添加选课按钮和刷新课程列表按钮的响应事件
		this.addFlushBtn.addActionListener(this);
		this.addSelectBtn.addActionListener(this);

	}

	// 按钮添加方法
	private void addButton(Icon icon, int flag, int high, StudentWindow sw) {
		JButton button = new JButton(icon);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 添加出发事件
				switch (flag) {
				case 1:
					// 修改密码
					new StudentFixPassWordWindows(student, sw);
					break;
				case 2:
					// 查看已选课程
					new SelectCourseWindows(courseFlag, student);
					break;
				case 3:
					// 退选课程
					new exitCourseWindows(student, 1);
					break;
				}
			}
		});
		button.setBounds(35, high, 125, 30);
		sw.add(button);
	}

	// JTabel列表属性设置类
	class CourseList extends AbstractTableModel {
		public String[] titles = { "序号", "课程ID", "课程名", "老师", "学时", "学分", "类型", "选课上限", "已选人数" };
		public Object[][] userInfo = {}; // 定义数据

		public int getRowCount() {
			return 0;
		}

		public int getColumnCount() {
			return 0;
		}

		public Object getValueAt(int rowIndex, int columnIndex) {
			return null;
		}
	}

	// 取得窗口的宽度
	public static int getWindowwide() {
		return WINDOWWIDE;
	}

	// 取得窗口的高度
	public static int getWindowhigh() {
		return WINDOWHIGH;
	}

	// 选课按钮和刷新按钮的时间响应机制处理函数
	public void actionPerformed(ActionEvent e) {
		// 刷新课程信息,从数据库中读出所有的课程信息
		if (e.getSource() == this.addFlushBtn) {
			if (flushFlag == 1) {
				List<CourseArrange> list = new CourseArrangeDAO().searchCourseInformation();
				CourseArrange arrange = null;
				for (int i = 0; i < list.size(); i++) {
					arrange = list.get(i);
					this.tableModel.addRow(new Object[] { i + 1, arrange.getCourseID(), arrange.getCourseName(),
							arrange.getCourseTeacher(), arrange.getCourseTimes(), arrange.getCourseGridits(),
							arrange.getCourseType(), arrange.getCourseNumber(), arrange.getCourseSelectNumber() });
				}
				flushFlag++;
			}
		}
		// 确定添加课程信息
		if (e.getSource() == this.addSelectBtn) {

			// 1. 取得学生的ID
			String studentID = student.getStuID();
			// 2. 取得所写的课程ID
			String courseID = courseIDText.getText();
			// 测试
			System.out.println("StuID: " + studentID + " , " + "courseID: " + courseID);
			// 3. 将数据刷新到数据库的已选课程表
			if (courseID.length() != 0) {
				int count = new SelectCourseDAO().addStudentSelectCourseInformation(studentID, courseID);
			} else {
				PublicWindowSet.promptPopUp("请填写课程ID,在选课!", "选课提示", this);
			}
		}
	}

	// 修改密码成功后学生窗口释放函数
	public void windowDispose() {
		this.dispose();
	}
}

// 修改密码弹出框
class StudentFixPassWordWindows {
	JFrame frame = new JFrame();

	public StudentFixPassWordWindows(Student stu, StudentWindow sw) {

		frame.setLayout(null);

		// 1.添加三个标签
		// 添加密码框修改密码的标签
		PublicWindowSet.addLabel(frame, 16, 68, 30, 80, 30, "  原密码:");
		PublicWindowSet.addLabel(frame, 16, 68, 65, 80, 30, "  新密码:");
		PublicWindowSet.addLabel(frame, 16, 68, 100, 80, 30, "重新输入:");

		// 2.添加三个文本域
		// 添加密码框修改密码的密码输入文本框
		JPasswordField oldText = new JPasswordField();
		JPasswordField newText = new JPasswordField();
		JPasswordField reEText = new JPasswordField();
		PublicWindowSet.addTextField(oldText, 160, 30, 150, 30, frame);
		PublicWindowSet.addTextField(newText, 160, 65, 150, 30, frame);
		PublicWindowSet.addTextField(reEText, 160, 100, 150, 30, frame);

		// 3.添加确定按钮
		// 添加按钮
		Icon icon = new ImageIcon("src//images//icons//confirmFix.png");
		JButton confirmBtn = new JButton(icon);
		// 设置位置
		confirmBtn.setBounds(150, 150, 125, 30);
		// 添加进面板
		frame.add(confirmBtn);
		confirmBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// 添加修改密码的数据库操作
				// 1. 判断原密码输入框的密码是否和元密码相同
				if (oldText.getText().length() == 0) {
					PublicWindowSet.promptPopUp("请输入原密码", "错误提示", frame);
				} else if (newText.getText().length() == 0 || reEText.getText().length() == 0) {
					PublicWindowSet.promptPopUp("请输入新密码或重复输入新密码", "错误提示", frame);
				}

				if (newText.getText().length() != 0 && reEText.getText().length() != 0
						&& !oldText.getText().equals(stu.getLoginPSD())) {
					PublicWindowSet.promptPopUp("原密码输入错误!", "更改密码提示", frame);
				}

				// 2. 判断新密码和原密码是否输入一致
				if (newText.getText().length() != 0 && reEText.getText().length() != 0
						&& !reEText.getText().equals(newText.getText())
						&& oldText.getText().equals(stu.getLoginPSD())) {
					PublicWindowSet.promptPopUp("两次密码输入不一致!", "更改密码提示", frame);
				}

				// 3. 更新密码操作
				if (newText.getText().length() != 0 && reEText.getText().length() != 0
						&& reEText.getText().equals(newText.getText()) && oldText.getText().equals(stu.getLoginPSD())) {
					new StudentDAO().updatePassword(stu.getStuID(), newText.getText());
					// 修改成功释放该窗口
					frame.dispose();
					// 释放学生窗口
					sw.windowDispose();
					// 同时调用出登录窗口
					new LoginWindow();
				}
			}
		});
		PublicWindowSet.windowAttribute(frame, 420, 230, null, "密码修改窗口");
	}
}

// 查看已选课程
class SelectCourseWindows extends JFrame implements ActionListener {
	private String[] columnNames = { "课程名", "老师", "学分", "成绩", "上课安排" };
	private DefaultTableModel selectTableModel = null;
	private static int flag;
	private Student student = null;

	// addTableList是PublicWindowSet中封装的table组件添加的方法
	public SelectCourseWindows(int flag, Student student) {
		PublicWindowSet.windowAttribute(this, 400, 180, null, "查看课程窗口");
		selectTableModel = PublicWindowSet.addTableList(this, columnNames);
		JButton seeBut = new JButton("查看课程");
		JPanel bunPanel = new JPanel();
		bunPanel.add(seeBut);
		seeBut.addActionListener(this);
		this.add(bunPanel, BorderLayout.SOUTH);
		// 确定actionPerformed只能打印一次
		this.flag = flag;
		this.student = student;
	}
	
	// 添加button的时间响应机制
	public void actionPerformed(ActionEvent e) {
		if (flag == 1) {
			List<Object> list = new CourseArrangeDAO().searchSpecificInformation(student.getStuID());
			for (int i = 0; i < list.size(); i = i + 2) {
				CourseArrange ca = (CourseArrange) list.get(i);
				SelectCourse sc = (SelectCourse) list.get(i + 1);
				selectTableModel.addRow(new Object[] { ca.getCourseName(), ca.getCourseTeacher(), ca.getCourseGridits(),
						sc.getStudentGrade(), ca.getCourseArrange() });
			}
			flag++;
		}
	}
}

// 退选课程
class exitCourseWindows extends JFrame implements ActionListener {
	// 表头
	private String[] columnNames = { "课程ID", "课程名", "学分", "课程安排" };
	private DefaultTableModel exitTableModel = null;
	static int flag = 0;
	static int flagExit = 0;
	JTextField text = null;
	private Student student = null;

	public exitCourseWindows(Student student, int flag) {
		PublicWindowSet.windowAttribute(this, 300, 220, null, "退选课程窗口");
		exitTableModel = PublicWindowSet.addTableList(this, columnNames);

		JLabel label = new JLabel("在下框中输入退选的课程ID!");
		text = new JTextField(20);
		JButton exitBut = new JButton("退选");
		JPanel bunPanel = new JPanel();
		bunPanel.setLayout(new GridLayout(3, 1));
		bunPanel.add(label);
		bunPanel.add(text);
		bunPanel.add(exitBut);
		exitBut.addActionListener(this);
		this.add(bunPanel, BorderLayout.SOUTH);

		this.student = student;
		this.flag = flag;
	}

	public void actionPerformed(ActionEvent e) {
		if (flag == 1) {
			List<CourseArrange> list = new CourseArrangeDAO().selectNameAndIDForCourse(student.getStuID());
			for (int i = 0; i < list.size(); i++) {
				exitTableModel.addRow(new Object[] { list.get(i).getCourseID(), list.get(i).getCourseName(),
						list.get(i).getCourseGridits(), list.get(i).getCourseArrange() });
			}
			flag = 0;
			flagExit = 1;
		} else if (flagExit == 1) {
			if (text.getText().length() == 0) {
				PublicWindowSet.promptPopUp("请输入退选课程的ID!", "退选提示", this);
			} else {
				System.out.println(text.getText());
				new SelectCourseDAO().removeCourse(text.getText());
			}
		}
	}
}

  

以上是关于学生选课系统----StudentWindow的主要内容,如果未能解决你的问题,请参考以下文章

JAVA | 学生选课系统

XK关系型学生选课数据库,关系如下:

面向对象的应用:学生选课系统

数据库实验(学生选课系统)

用数据结构(c语言版)编写的学生选课系统

python之路34面向对象作业之学生选课系统