📄 studentmanageframe.java~4~
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.sql.ResultSet;
import java.util.Vector;
import java.util.Enumeration;
import panel.*;
import util.DatabaseTool;
import entity.SortByIdList;
import entity.SubjectScoreInputItem;
import entity.SortByTotalScoreList;
import entity.TotalScoreSortItem;
/**
* <p>Title: 学生信息管理系统</p>
* <p>Description: 对学生信息、学生成绩进行管理</p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author 苏年乐
* @version 1.0
*/
/**
* 学生信息管理系统主界面。本系统具有管理学生记录和管理学生成绩的功能。管理学生记录的功能包括:
* 添加、删除、修改、查询学生记录;管理学生成绩记录的功能包括:添加课程成绩、统计课程平均分、对
* 学生总成绩进行排名
*/
public class StudentManageFrame
extends JFrame {
//当前的操作状态
String status = "";
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenu1 = new JMenu();
JMenu jMenu2 = new JMenu();
JMenuItem jMenuItem1 = new JMenuItem();
JMenuItem jMenuItem2 = new JMenuItem();
JMenuItem jMenuItem4 = new JMenuItem();
JMenuItem jMenuItem3 = new JMenuItem();
JMenuItem jMenuItem5 = new JMenuItem();
JMenuItem jMenuItem6 = new JMenuItem();
JMenuItem jMenuItem7 = new JMenuItem();
JMenuItem jMenuItem8 = new JMenuItem();
JMenuItem jMenuItem9 = new JMenuItem();
JScrollPane jScrollPane = new JScrollPane();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
XYLayout xYLayout2 = new XYLayout();
JLabel tipLabel = new JLabel();
XYLayout xYLayout3 = new XYLayout();
JButton button = new JButton();
public StudentManageFrame() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setJMenuBar(jMenuBar1);
this.setTitle("学生信息管理系统");
this.addWindowListener(new StudentManageFrame_this_windowAdapter(this));
this.getContentPane().setLayout(xYLayout1);
jMenu1.setText("学生记录管理");
jMenu2.setText("学生成绩管理");
jMenuItem1.setText("添加学生记录");
jMenuItem1.addActionListener(new
StudentManageFrame_jMenuItem1_actionAdapter(this));
jMenuItem2.setText("删除学生记录");
jMenuItem2.addActionListener(new
StudentManageFrame_jMenuItem2_actionAdapter(this));
jMenuItem4.setText("修改学生记录");
jMenuItem4.addActionListener(new
StudentManageFrame_jMenuItem4_actionAdapter(this));
jMenuItem3.setText("查询学生记录");
jMenuItem3.addActionListener(new
StudentManageFrame_jMenuItem3_actionAdapter(this));
jMenuItem5.setText("添加语文成绩");
jMenuItem5.addActionListener(new
StudentManageFrame_jMenuItem5_actionAdapter(this));
jMenuItem6.setText("添加数学成绩");
jMenuItem6.addActionListener(new
StudentManageFrame_jMenuItem6_actionAdapter(this));
jMenuItem7.setText("添加英语成绩");
jMenuItem7.addActionListener(new
StudentManageFrame_jMenuItem7_actionAdapter(this));
jMenuItem8.setText("统计课程平均分");
jMenuItem8.addActionListener(new
StudentManageFrame_jMenuItem8_actionAdapter(this));
jMenuItem9.setText("学生总成绩排名");
jMenuItem9.addActionListener(new
StudentManageFrame_jMenuItem9_actionAdapter(this));
jPanel1.setLayout(xYLayout2);
tipLabel.setFont(new java.awt.Font("Dialog", 0, 12));
tipLabel.setHorizontalAlignment(SwingConstants.CENTER);
tipLabel.setText("欢迎使用学生信息管理系统");
jPanel2.setLayout(xYLayout3);
button.setText("确定");
button.setEnabled(false);
button.addActionListener(new StudentManageFrame_button_actionAdapter(this));
xYLayout1.setWidth(402);
xYLayout1.setHeight(312);
jMenuBar1.add(jMenu1);
jMenuBar1.add(jMenu2);
jMenu1.add(jMenuItem1);
jMenu1.add(jMenuItem2);
jMenu1.add(jMenuItem4);
jMenu1.add(jMenuItem3);
jMenu2.add(jMenuItem5);
jMenu2.add(jMenuItem6);
jMenu2.add(jMenuItem7);
jMenu2.add(jMenuItem8);
jMenu2.add(jMenuItem9);
this.getContentPane().add(jScrollPane, new XYConstraints(0, 27, 400, 242));
this.getContentPane().add(jPanel1, new XYConstraints(0, 0, 400, 29));
jPanel1.add(tipLabel, new XYConstraints(64, 0, 260, 27));
this.getContentPane().add(jPanel2, new XYConstraints( -1, 269, 401, 40));
jPanel2.add(button, new XYConstraints(152, 5, 96, 28));
this.setResizable(false);
DatabaseTool.conDB(); //建立数据库连接
}
/**
* 程序入口,设置界面外观,使窗口处于屏幕中央
*/
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
e.printStackTrace();
}
StudentManageFrame frame = new StudentManageFrame();
frame.setSize(400, 360);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation( (d.width - frame.getSize().width) / 2,
(d.height - frame.getSize().height) / 2);
frame.show();
}
/**
* 响应添加学生记录菜单命令
*/
void jMenuItem1_actionPerformed(ActionEvent e) {
this.button.setEnabled(true);
this.tipLabel.setText("添加学生记录");
this.button.setText("提交");
if (this.jScrollPane.getViewport().getComponentCount() > 0)
this.jScrollPane.getViewport().remove(0);
this.jScrollPane.getViewport().add(new StudentInfoPanel());
this.status = "add student item";
}
/**
* 响应删除学生记录菜单命令
*/
void jMenuItem2_actionPerformed(ActionEvent e) {
this.button.setEnabled(true);
this.tipLabel.setText("删除学生记录");
this.button.setText("提交学号");
if (this.jScrollPane.getViewport().getComponentCount() > 0)
this.jScrollPane.getViewport().remove(0);
StudentIDInputPanel inputPanel = new StudentIDInputPanel();
inputPanel.setLabel("请输入要删除的学生学号");
this.jScrollPane.getViewport().add(inputPanel);
this.status = "delete student item:input ID";
}
/**
* 响应修改学生记录菜单命令
*/
void jMenuItem4_actionPerformed(ActionEvent e) {
this.button.setEnabled(true);
this.tipLabel.setText("修改学生记录");
this.button.setText("确定");
if (this.jScrollPane.getViewport().getComponentCount() > 0)
this.jScrollPane.getViewport().remove(0);
StudentIDInputPanel inputPanel = new StudentIDInputPanel();
inputPanel.setLabel("请输入要修改的学生学号");
this.jScrollPane.getViewport().add(inputPanel);
this.status = "modify student item:input ID";
}
/**
* 响应查询学生记录菜单命令
*/
void jMenuItem3_actionPerformed(ActionEvent e) {
this.button.setEnabled(true);
this.tipLabel.setText("查询学生记录");
this.button.setText("确定");
if (this.jScrollPane.getViewport().getComponentCount() > 0)
this.jScrollPane.getViewport().remove(0);
StudentIDInputPanel inputPanel = new StudentIDInputPanel();
inputPanel.setLabel("请输入要查询的学生学号");
this.jScrollPane.getViewport().add(inputPanel);
this.status = "query student item:input ID";
}
/**
* 响应添加语文成绩菜单命令
*/
void jMenuItem5_actionPerformed(ActionEvent e) {
this.tipLabel.setText("添加语文成绩");
this.status = "add score for Chinese:input score";
setAddScoreInterface();
String sqlString = "select id,name,chinesescore from students sort by id";
initialAddScoreInterface(sqlString, "chinesescore");
}
/**
* 响应添加数学成绩菜单命令
*/
void jMenuItem6_actionPerformed(ActionEvent e) {
this.tipLabel.setText("添加数学成绩");
this.status = "add score for Math:input score";
setAddScoreInterface();
String sqlString = "select id,name,mathscore from students";
initialAddScoreInterface(sqlString, "mathscore");
}
/**
* 响应添加英语成绩菜单命令
*/
void jMenuItem7_actionPerformed(ActionEvent e) {
this.tipLabel.setText("添加英语成绩");
this.status = "add score for English:input score";
setAddScoreInterface();
String sqlString = "select id,name,englishscore from students";
initialAddScoreInterface(sqlString, "englishscore");
}
/**
* 响应统计课程平均分菜单命令
*/
void jMenuItem8_actionPerformed(ActionEvent e) {
this.tipLabel.setText("统计课程平均分");
this.status = "calculate the average score:input subject";
this.button.setEnabled(true);
if (this.jScrollPane.getViewport().getComponentCount() > 0)
this.jScrollPane.getViewport().remove(0);
this.jScrollPane.getViewport().add(new SubjectInputPanel());
}
/**
* 响应统计课程平均分菜单命令
*/
void jMenuItem9_actionPerformed(ActionEvent e) {
this.tipLabel.setText("总成绩排名");
this.status = "sort the total scores";
this.button.setEnabled(false);
if (this.jScrollPane.getViewport().getComponentCount() > 0)
this.jScrollPane.getViewport().remove(0);
ScoreSortTable table = new ScoreSortTable();
this.jScrollPane.getViewport().add(new ScoreSortTable());
sortTotalScores();
}
/**
* 设置添加分数界面
*/
private void setAddScoreInterface() {
this.button.setEnabled(true);
this.button.setText("确定");
if (this.jScrollPane.getViewport().getComponentCount() > 0)
this.jScrollPane.getViewport().remove(0);
ScoreInputTable table = new ScoreInputTable();
this.jScrollPane.getViewport().add(table);
}
/**
* 初始化添加分数界面中的表格数据
*/
private void initialAddScoreInterface(String sqlString, String subject) {
ScoreInputTable table = (ScoreInputTable)this.jScrollPane.getViewport().
getComponent(0);
try {
ResultSet rs = DatabaseTool.executeQuery(sqlString);
SortByIdList list = new SortByIdList(20);
while (rs.next()) {
String id = rs.getString("id");
id = id.trim();
String name = rs.getString("name");
String score = rs.getString(subject);
SubjectScoreInputItem item = new SubjectScoreInputItem(id, name, score);
list.add(item);
}
table.setContents(list);
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* 响应按钮单击命令
*/
void button_actionPerformed(ActionEvent e) {
if (this.status.equals("add student item")) {
submitAddItem();
}
else if (this.status.equals("delete student item:input ID")) {
boolean existID = submitID();
if (existID) {
this.status = "delete student item:confirm";
this.button.setText("确定删除");
StudentInfoPanel infoPanel = (StudentInfoPanel)this.jScrollPane.
getViewport().getComponent(0);
infoPanel.setEditEnable(false);
}
}
else if (this.status.equals("delete student item:confirm")) {
submitDeleteItem();
}
else if (this.status.equals("modify student item:input ID")) {
boolean existID = submitID();
if (existID) {
this.status = "modify student item:confirm";
this.button.setText("提交");
}
}
else if (this.status.equals("modify student item:confirm")) {
submitModifyItem();
}
else if (this.status.equals("query student item:input ID")) {
boolean existID = submitID();
if (existID) {
this.button.setText("确定");
if (this.jScrollPane.getViewport().getComponent(0) != null) {
StudentInfoPanel infoPanel = (StudentInfoPanel)this.jScrollPane.
getViewport().getComponent(0);
infoPanel.setEditEnable(false);
this.button.setEnabled(false);
}
}
}
else if (this.status.equals("add score for Chinese:input score")) {
updataScore("chinese");
updataInterface();
}
else if (this.status.equals("add score for Math:input score")) {
updataScore("math");
updataInterface();
}
else if (this.status.equals("add score for English:input score")) {
updataScore("english");
updataInterface();
}
else if (this.status.equals("calculate the average score:input subject")) {
submitSubject();
this.button.setEnabled(false);
}
}
/**
* 提交添加学生记录
*/
private void submitAddItem() {
StudentInfoPanel panel = (StudentInfoPanel)this.jScrollPane.getViewport().
getComponent(0);
if (panel.check() == false) {
JOptionPane.showMessageDialog(null, "请输入完整信息再点击提交按钮!");
return;
}
else {
String id = panel.getID();
String name = panel.getName();
String sex = panel.getSex();
String nation = panel.getNation();
String birthday = panel.getBirthDay();
String strSQL =
"insert into students(id,name,sex,nation,birthday) values('" +
id + "','" + name + "','" + sex + "','" + nation + "','" + birthday +
"')";
try {
DatabaseTool.executeUpdate(strSQL);
}
catch (Exception exx) {
JOptionPane.showMessageDialog(null, "数据库中已经存在您要添加的学生的学号!");
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -