0


基于java学生选课系统

开发工具eclipse,jdk1.8

技术:java swing

数据库:mysql5.7

学生选课系统功能:管理员、教师、学生三个角色

一、管理员功能:

1.登录、修改密码、退出系统

2.学生管理:添加、修改、删除、查询

3.班级管理:添加、修改、删除、查询

4.教师管理:添加、修改、删除、查询

5.课程管理:添加、修改、删除、查询

6.选课管理:修改选课、推选课程

7.帮助、关于

二、教师功能:

1.登录、修改密码、退出系统

2.学生管理:添加、修改、删除、查询

3.班级管理:添加、修改、删除、查询

4.教师管理:修改个人信息

5.课程管理:添加、修改、删除、查询

6.选课管理:修改选课、推选课程

7.帮助、关于

三、学生功能:

1.登录、修改密码、退出系统

2.学生管理:修改个人信息

3.选课管理:修改选课、推选课程

4.帮助、关于

管理员截图:

教师截图:

学生截图:

package com.artisan.view;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JLabel;

import java.awt.Font;
import java.util.List;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.UIManager;

import com.artisan.dao.ClassDao;
import com.artisan.model.StudentClass;
import com.artisan.util.StringUtil;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JTextArea;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ManageClassFrm extends JInternalFrame {
    private JTextField searchClassNameTextField;
    private JTable classListTable;
    private JTextField editClassNameTextField;
    private JTextArea editClassInfoTextArea;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ManageClassFrm frame = new ManageClassFrm();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ManageClassFrm() {
        setClosable(true);
        setIconifiable(true);
        setTitle("\u73ED\u7EA7\u4FE1\u606F\u7BA1\u7406");
        setBounds(100, 100, 773, 562);
        
        JScrollPane scrollPane = new JScrollPane();
        
        JLabel label = new JLabel("\u73ED\u7EA7\u540D\u79F0\uFF1A");
        label.setIcon(new ImageIcon(ManageClassFrm.class.getResource("/images/\u73ED\u7EA7\u540D\u79F0.png")));
        label.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        
        searchClassNameTextField = new JTextField();
        searchClassNameTextField.setColumns(10);
        
        JButton searchButton = new JButton("\u67E5\u8BE2");
        searchButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                StudentClass sc = new StudentClass();
                sc.setName(searchClassNameTextField.getText().toString());
                setTable(sc);
            }
        });
        searchButton.setIcon(new ImageIcon(ManageClassFrm.class.getResource("/images/\u641C\u7D22.png")));
        searchButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        
        JLabel label_1 = new JLabel("\u73ED\u7EA7\u540D\u79F0\uFF1A");
        label_1.setIcon(new ImageIcon(ManageClassFrm.class.getResource("/images/\u73ED\u7EA7\u540D\u79F0.png")));
        label_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        
        editClassNameTextField = new JTextField();
        editClassNameTextField.setColumns(10);
        
        JLabel label_2 = new JLabel("\u73ED\u7EA7\u4FE1\u606F\uFF1A");
        label_2.setIcon(new ImageIcon(ManageClassFrm.class.getResource("/images/\u73ED\u7EA7\u4ECB\u7ECD.png")));
        label_2.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        
        editClassInfoTextArea = new JTextArea();
        
        JButton submitEditButton = new JButton("\u786E\u8BA4\u4FEE\u6539");
        submitEditButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                submitEditAct(ae);
            }
        });
        submitEditButton.setIcon(new ImageIcon(ManageClassFrm.class.getResource("/images/\u786E\u8BA4.png")));
        submitEditButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        
        JButton submitDeleteButton = new JButton("\u5220\u9664");
        submitDeleteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                deleteClassAct(ae);
            }
        });
        submitDeleteButton.setIcon(new ImageIcon(ManageClassFrm.class.getResource("/images/\u5220\u9664.png")));
        submitDeleteButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                            .addGap(160)
                            .addComponent(label)
                            .addGap(18)
                            .addComponent(searchClassNameTextField, GroupLayout.PREFERRED_SIZE, 207, GroupLayout.PREFERRED_SIZE)
                            .addGap(50)
                            .addComponent(searchButton))
                        .addGroup(groupLayout.createSequentialGroup()
                            .addGap(110)
                            .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 536, GroupLayout.PREFERRED_SIZE))
                        .addGroup(groupLayout.createSequentialGroup()
                            .addGap(53)
                            .addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
                                .addGroup(groupLayout.createSequentialGroup()
                                    .addPreferredGap(ComponentPlacement.RELATED)
                                    .addComponent(label_2)
                                    .addGap(18)
                                    .addComponent(editClassInfoTextArea))
                                .addGroup(groupLayout.createSequentialGroup()
                                    .addComponent(label_1)
                                    .addGap(18)
                                    .addComponent(editClassNameTextField, GroupLayout.PREFERRED_SIZE, 146, GroupLayout.PREFERRED_SIZE)))
                            .addGap(102)
                            .addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
                                .addComponent(submitDeleteButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(submitEditButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addContainerGap(111, Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
            groupLayout.createParallelGroup(Alignment.LEADING)
                .addGroup(groupLayout.createSequentialGroup()
                    .addGap(45)
                    .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                        .addComponent(label, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
                        .addComponent(searchClassNameTextField, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
                        .addComponent(searchButton, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE))
                    .addGap(30)
                    .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 282, GroupLayout.PREFERRED_SIZE)
                    .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                            .addGap(18)
                            .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                .addComponent(label_1)
                                .addComponent(editClassNameTextField, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE))
                            .addGap(18)
                            .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                .addGroup(groupLayout.createSequentialGroup()
                                    .addComponent(editClassInfoTextArea, GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE)
                                    .addContainerGap())
                                .addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
                                    .addComponent(label_2)
                                    .addGap(32))))
                        .addGroup(groupLayout.createSequentialGroup()
                            .addGap(30)
                            .addComponent(submitEditButton)
                            .addGap(34)
                            .addComponent(submitDeleteButton)
                            .addContainerGap())))
        );
        
        classListTable = new JTable();
        classListTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent me) {
                selectedTableRow(me);
            }
        });
        classListTable.setFont(new Font("微软雅黑", Font.PLAIN, 12));
        classListTable.setModel(new DefaultTableModel(
            new Object[][] {
            },
            new String[] {
                    "\u73ED\u7EA7\u7F16\u53F7","\u73ED\u7EA7\u540D\u79F0" , "\u73ED\u7EA7\u4FE1\u606F\u4ECB\u7ECD"
            }
        ) {
            boolean[] columnEditables = new boolean[] {
                false, false, false
            };
            public boolean isCellEditable(int row, int column) {
                return columnEditables[column];
            }
        });
        classListTable.getColumnModel().getColumn(2).setPreferredWidth(316);
        scrollPane.setViewportView(classListTable);
        getContentPane().setLayout(groupLayout);
        setTable(new StudentClass());
    }
    protected void deleteClassAct(ActionEvent ae) {
        // TODO Auto-generated method stub
        if(JOptionPane.showConfirmDialog(this, "您确定删除么?") != JOptionPane.OK_OPTION){
            return;
        }
        int index = classListTable.getSelectedRow();
        if(index == -1){
            JOptionPane.showMessageDialog(this, "请选中要删除的数据!");
            return;
        }
        DefaultTableModel dft = (DefaultTableModel) classListTable.getModel();
        int id = Integer.parseInt(dft.getValueAt(classListTable.getSelectedRow(), 0).toString());
        ClassDao classDao = new ClassDao();
        if(classDao.delete(id)){
            JOptionPane.showMessageDialog(this, "删除成功!");
        }else{
            JOptionPane.showMessageDialog(this, "删除失败!");
        }
        classDao.closeDao();
        setTable(new StudentClass());
    }

    protected void submitEditAct(ActionEvent ae) {
        // TODO Auto-generated method stub
        ClassDao classDao = new ClassDao();
        int index = classListTable.getSelectedRow();
        if(index == -1){
            JOptionPane.showMessageDialog(this, "请选中要修改的数据!");
            return;
        }
        DefaultTableModel dft = (DefaultTableModel) classListTable.getModel();
        String className = dft.getValueAt(classListTable.getSelectedRow(), 1).toString();
        String classInfo = dft.getValueAt(classListTable.getSelectedRow(), 2).toString();
        String editClassName = editClassNameTextField.getText().toString();
        String editClassInfo = editClassInfoTextArea.getText().toString();
        if(StringUtil.isEmpty(editClassName)){
            JOptionPane.showMessageDialog(this, "请填写要修改的名称!");
            return;
        }
        if(className.equals(editClassName) && classInfo.equals(editClassInfo)){
            JOptionPane.showMessageDialog(this, "您还没有做任何修改!");
            return;
        }
        int id = Integer.parseInt(dft.getValueAt(classListTable.getSelectedRow(), 0).toString());
        StudentClass sc = new StudentClass();
        sc.setId(id);
        sc.setName(editClassName);
        sc.setInfo(editClassInfo);
        if(classDao.update(sc)){
            JOptionPane.showMessageDialog(this, "更新成功!");
        }else{
            JOptionPane.showMessageDialog(this, "更新失败!");
        }
        classDao.closeDao();
        setTable(new StudentClass());
    }

    protected void selectedTableRow(MouseEvent me) {
        // TODO Auto-generated method stub
        DefaultTableModel dft = (DefaultTableModel) classListTable.getModel();
        editClassNameTextField.setText(dft.getValueAt(classListTable.getSelectedRow(), 1).toString());
        editClassInfoTextArea.setText(dft.getValueAt(classListTable.getSelectedRow(), 2).toString());
    }

    private void setTable(StudentClass studentClass){
        DefaultTableModel dft = (DefaultTableModel) classListTable.getModel();
        dft.setRowCount(0);
        ClassDao classDao = new ClassDao();
        List<StudentClass> classList = classDao.getClassList(studentClass);
        for (StudentClass sc : classList) {
            Vector v = new Vector();
            v.add(sc.getId());
            v.add(sc.getName());
            v.add(sc.getInfo());
            dft.addRow(v);
        }
        classDao.closeDao();
    }
}

package com.artisan.view;

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

import com.artisan.dao.CourseDao;
import com.artisan.dao.SelectedCourseDao;
import com.artisan.dao.StudentDao;
import com.artisan.model.Course;
import com.artisan.model.SelectedCourse;
import com.artisan.model.Student;

public class ManageSelectedCourseFrm extends JInternalFrame {
private JTable selectedCourseListTable;
private JComboBox searchCourseComboBox;
private JComboBox searchStudentComboBox;
private JComboBox editSelectedStudentComboBox;
private JComboBox editSelectedCourseComboBox;
private List<Student> studentList = new ArrayList<Student>();
private List<Course> courseList = new ArrayList<Course>();
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ManageSelectedCourseFrm frame = new ManageSelectedCourseFrm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

/**
  * Create the frame.
  */
 public ManageSelectedCourseFrm() {
     setClosable(true);
     setIconifiable(true);
     setTitle("\u9009\u8BFE\u7BA1\u7406");
     setBounds(100, 100, 669, 562);
     
     JLabel label = new JLabel("\u5B66\u751F\uFF1A");
     label.setIcon(new ImageIcon(ManageSelectedCourseFrm.class.getResource("/images/\u5B66\u751F\u7BA1\u7406.png")));
     label.setFont(new Font("微软雅黑", Font.PLAIN, 14));
     
     searchStudentComboBox = new JComboBox();
     searchStudentComboBox.addItemListener(new ItemListener() {
         public void itemStateChanged(ItemEvent ie) {
             studentChangeAct(ie);
         }
     });
     
     JLabel label_1 = new JLabel("\u8BFE\u7A0B\uFF1A");
     label_1.setIcon(new ImageIcon(ManageSelectedCourseFrm.class.getResource("/images/\u65B0\u4EBA\u8BFE\u7A0B.png")));
     label_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
     
     searchCourseComboBox = new JComboBox();
     
     JButton confirmSelectedButton = new JButton("\u786E\u8BA4\u9009\u8BFE");
     confirmSelectedButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
             confirmSelectedCourse(ae);
         }
     });
     confirmSelectedButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
     confirmSelectedButton.setIcon(new ImageIcon(ManageSelectedCourseFrm.class.getResource("/images/\u786E\u8BA4.png")));
     
     JScrollPane scrollPane = new JScrollPane();
     
     JPanel panel = new JPanel();
     panel.setBorder(new TitledBorder(null, "\u4FEE\u6539\u9009\u8BFE", TitledBorder.LEADING, TitledBorder.TOP, null, null));
     GroupLayout groupLayout = new GroupLayout(getContentPane());
     groupLayout.setHorizontalGroup(
         groupLayout.createParallelGroup(Alignment.TRAILING)
             .addGroup(groupLayout.createSequentialGroup()
                 .addGap(46)
                 .addComponent(label)
                 .addGap(2)
                 .addComponent(searchStudentComboBox, GroupLayout.PREFERRED_SIZE, 124, GroupLayout.PREFERRED_SIZE)
                 .addGap(20)
                 .addComponent(label_1)
                 .addGap(12)
                 .addComponent(searchCourseComboBox, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE)
                 .addGap(36)
                 .addComponent(confirmSelectedButton)
                 .addContainerGap(49, Short.MAX_VALUE))
             .addGroup(groupLayout.createSequentialGroup()
                 .addContainerGap(81, Short.MAX_VALUE)
                 .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 493, GroupLayout.PREFERRED_SIZE)
                 .addGap(77))
             .addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
                 .addGap(75)
                 .addComponent(panel, GroupLayout.PREFERRED_SIZE, 500, GroupLayout.PREFERRED_SIZE)
                 .addContainerGap(78, Short.MAX_VALUE))
     );
     groupLayout.setVerticalGroup(
         groupLayout.createParallelGroup(Alignment.LEADING)
             .addGroup(groupLayout.createSequentialGroup()
                 .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                     .addGroup(groupLayout.createSequentialGroup()
                         .addGap(36)
                         .addComponent(label))
                     .addGroup(groupLayout.createSequentialGroup()
                         .addGap(34)
                         .addComponent(searchStudentComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                     .addGroup(groupLayout.createSequentialGroup()
                         .addGap(34)
                         .addComponent(label_1))
                     .addGroup(groupLayout.createSequentialGroup()
                         .addGap(33)
                         .addComponent(searchCourseComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                     .addGroup(groupLayout.createSequentialGroup()
                         .addGap(32)
                         .addComponent(confirmSelectedButton)))
                 .addGap(47)
                 .addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 216, GroupLayout.PREFERRED_SIZE)
                 .addGap(31)
                 .addComponent(panel, GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE)
                 .addContainerGap(35, Short.MAX_VALUE))
     );
     
     selectedCourseListTable = new JTable();
     selectedCourseListTable.addMouseListener(new MouseAdapter() {
         @Override
         public void mouseClicked(MouseEvent me) {
             selectedCourse(me);
         }
     });
     selectedCourseListTable.setModel(new DefaultTableModel(
         new Object[][] {
         },
         new String[] {
             "\u9009\u8BFE\u7F16\u53F7", "\u5B66\u751F\u59D3\u540D", "\u8BFE\u7A0B\u540D\u79F0"
         }
     ) {
         boolean[] columnEditables = new boolean[] {
             false, false, false
         };
         public boolean isCellEditable(int row, int column) {
             return columnEditables[column];
         }
     });
     scrollPane.setViewportView(selectedCourseListTable);
     
     JLabel label_2 = new JLabel("\u5B66\u751F\uFF1A");
     label_2.setIcon(new ImageIcon(ManageSelectedCourseFrm.class.getResource("/images/\u5B66\u751F\u7BA1\u7406.png")));
     label_2.setFont(new Font("微软雅黑", Font.PLAIN, 14));
     
     editSelectedStudentComboBox = new JComboBox();
     
     JLabel label_3 = new JLabel("\u8BFE\u7A0B\uFF1A");
     label_3.setIcon(new ImageIcon(ManageSelectedCourseFrm.class.getResource("/images/\u65B0\u4EBA\u8BFE\u7A0B.png")));
     label_3.setFont(new Font("微软雅黑", Font.PLAIN, 14));
     
     editSelectedCourseComboBox = new JComboBox();
     
     JButton confirmEditButton = new JButton("\u786E\u8BA4\u4FEE\u6539");
     confirmEditButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
             editSelectCourseAct(ae);
         }
     });
     confirmEditButton.setIcon(new ImageIcon(ManageSelectedCourseFrm.class.getResource("/images/\u786E\u8BA4.png")));
     confirmEditButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
     
     JButton deleteCourseButton = new JButton("\u9000\u9009\u8BFE\u7A0B");
     deleteCourseButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent ae) {
             deleteSelectedCourse(ae);
         }
     });
     deleteCourseButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
     deleteCourseButton.setIcon(new ImageIcon(ManageSelectedCourseFrm.class.getResource("/images/\u5220\u9664.png")));
     GroupLayout gl_panel = new GroupLayout(panel);
     gl_panel.setHorizontalGroup(
         gl_panel.createParallelGroup(Alignment.LEADING)
             .addGroup(gl_panel.createSequentialGroup()
                 .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                     .addGroup(gl_panel.createSequentialGroup()
                         .addContainerGap()
                         .addComponent(label_2)
                         .addGap(18)
                         .addComponent(editSelectedStudentComboBox, GroupLayout.PREFERRED_SIZE, 127, GroupLayout.PREFERRED_SIZE))
                     .addGroup(gl_panel.createSequentialGroup()
                         .addGap(75)
                         .addComponent(confirmEditButton)))
                 .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                     .addGroup(gl_panel.createSequentialGroup()
                         .addGap(39)
                         .addComponent(label_3)
                         .addGap(18)
                         .addComponent(editSelectedCourseComboBox, 0, 140, Short.MAX_VALUE))
                     .addGroup(gl_panel.createSequentialGroup()
                         .addGap(65)
                         .addComponent(deleteCourseButton)))
                 .addContainerGap())
     );
     gl_panel.setVerticalGroup(
         gl_panel.createParallelGroup(Alignment.LEADING)
             .addGroup(gl_panel.createSequentialGroup()
                 .addContainerGap()
                 .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                     .addComponent(label_2)
                     .addComponent(editSelectedStudentComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                     .addComponent(label_3)
                     .addComponent(editSelectedCourseComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                 .addPreferredGap(ComponentPlacement.RELATED, 52, Short.MAX_VALUE)
                 .addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
                     .addComponent(confirmEditButton)
                     .addComponent(deleteCourseButton))
                 .addContainerGap())
     );
     panel.setLayout(gl_panel);
     getContentPane().setLayout(groupLayout);
     setStudentCombox();
     setCourseCombox();
     initTable();
     setAuthority();
 }
 protected void deleteSelectedCourse(ActionEvent ae) {
     // TODO Auto-generated method stub
     int row = selectedCourseListTable.getSelectedRow();
     if(row == -1){
         JOptionPane.showMessageDialog(this, "请选择要修改的数据!");
         return;
     }
     int selected_id = Integer.parseInt(selectedCourseListTable.getValueAt(row, 0).toString());
     String courseName = selectedCourseListTable.getValueAt(row, 2).toString();
     SelectedCourse sc = new SelectedCourse();
     sc.setId(selected_id);
     SelectedCourseDao scDao = new SelectedCourseDao();
     CourseDao courseDao = new CourseDao();
     if(scDao.delete(selected_id)){
         if(courseDao.updateSelectedNum(getCourseIdByName(courseName), -1)){
             JOptionPane.showMessageDialog(this, "退课成功!");
         }else{
             JOptionPane.showMessageDialog(this, "退课成功,更新课程信息失败!");
         }
     }else{
         JOptionPane.showMessageDialog(this, "退课失败!");
     }
     scDao.closeDao();
     courseDao.closeDao();
     initTable();
 }

protected void editSelectCourseAct(ActionEvent ae) {
     // TODO Auto-generated method stub
     int row = selectedCourseListTable.getSelectedRow();
     if(row == -1){
         JOptionPane.showMessageDialog(this, "请选择要修改的数据!");
         return;
     }
     int selected_id = Integer.parseInt(selectedCourseListTable.getValueAt(row, 0).toString());
     String studentName = selectedCourseListTable.getValueAt(row, 1).toString();
     String courseName = selectedCourseListTable.getValueAt(row, 2).toString();
     Student student = (Student) editSelectedStudentComboBox.getSelectedItem();
     Course course = (Course) editSelectedCourseComboBox.getSelectedItem();
     if(studentName.equals(student.getName())&&courseName.equals(course.getName())){
         JOptionPane.showMessageDialog(this, "还未修改数据!");
         return;
     }
     CourseDao courseDao = new CourseDao();
     if(!courseDao.selectedEnable(course.getId())){
         JOptionPane.showMessageDialog(this, "该课程已经选满,不能再选!");
         return;
     }
     
     SelectedCourse sc = new SelectedCourse();
     sc.setId(selected_id);
     sc.setStudent_id(student.getId());
     sc.setCourse_id(course.getId());
     SelectedCourseDao scDao = new SelectedCourseDao();
     if(scDao.isSelected(sc)){
         JOptionPane.showMessageDialog(this, "已经选过这门课程了,不能再选!");
         return;
     }
     if(scDao.updateSelectedCourse(sc)){
         if(courseDao.updateSelectedNum(sc.getCourse_id(),1)){
             if(courseDao.updateSelectedNum(getCourseIdByName(courseName), -1)){
                 JOptionPane.showMessageDialog(this, "修改成功!!");
             }
         }else{
             JOptionPane.showMessageDialog(this, "修改成功,课程信息更新失败!");
         }
     }
     courseDao.closeDao();
     scDao.closeDao();
     initTable();
 }

protected void selectedCourse(MouseEvent me) {
     // TODO Auto-generated method stub
     int row = selectedCourseListTable.getSelectedRow();
     String studentName = selectedCourseListTable.getValueAt(row, 1).toString();
     String courseName = selectedCourseListTable.getValueAt(row, 2).toString();
     for (int i = 0; i < editSelectedStudentComboBox.getItemCount();i++) {
         Student student = (Student) editSelectedStudentComboBox.getItemAt(i);
         if(studentName.equals(student.getName())){
             editSelectedStudentComboBox.setSelectedIndex(i);
             break;
         }
     }
     for (int i = 0; i < editSelectedCourseComboBox.getItemCount();i++) {
         Course course = (Course) editSelectedCourseComboBox.getItemAt(i);
         if(courseName.equals(course.getName())){
             editSelectedCourseComboBox.setSelectedIndex(i);
             break;
         }
     }
 }

protected void studentChangeAct(ItemEvent ie) {
     // TODO Auto-generated method stub
     if(ie.getStateChange() == ItemEvent.SELECTED){
         initTable();
     }
     //JOptionPane.showMessageDialog(this, ie.getStateChange());
 }

protected void confirmSelectedCourse(ActionEvent ae) {
     // TODO Auto-generated method stub
     Student sstudent = (Student) searchStudentComboBox.getSelectedItem();
     Course scourse = (Course) searchCourseComboBox.getSelectedItem();
     SelectedCourse sc = new SelectedCourse();
     sc.setStudent_id(sstudent.getId());
     sc.setCourse_id(scourse.getId());
     CourseDao courseDao = new CourseDao();
     if(!courseDao.selectedEnable(scourse.getId())){
         JOptionPane.showMessageDialog(this, "该课程已经选满,不能再选!");
         return;
     }
     SelectedCourseDao scDao = new SelectedCourseDao();
     if(scDao.isSelected(sc)){
         JOptionPane.showMessageDialog(this, "已经选过这门课程了,不能再选!");
         return;
     }
     if(scDao.addSelectedCourse(sc)){
         if(courseDao.updateSelectedNum(sc.getCourse_id(),1)){
             JOptionPane.showMessageDialog(this, "选课成功!!");
         }else{
             JOptionPane.showMessageDialog(this, "选课成功,课程信息更新失败!");
         }
     }else{
         JOptionPane.showMessageDialog(this, "选课失败!");
     }
     courseDao.closeDao();
     scDao.closeDao();
     initTable();
 }

private void setStudentCombox(){
     StudentDao studentDao = new StudentDao();
     studentList = studentDao.getStudentList(new Student());
     studentDao.closeDao();
     for (Student student : studentList) {
         searchStudentComboBox.addItem(student);
         editSelectedStudentComboBox.addItem(student);
     }
     if("学生".equals(MainFrm.userType.getName())){
         Student user = (Student) MainFrm.userObject;
         for(int i = 0; i < searchStudentComboBox.getItemCount();i++){
             Student student = (Student) searchStudentComboBox.getItemAt(i);
             if(student.getId() == user.getId()){
                 searchStudentComboBox.setSelectedIndex(i);
                 editSelectedStudentComboBox.setSelectedIndex(i);
                 break;
             }
         }
     }
 }
 private void setCourseCombox(){
     CourseDao courseDao = new CourseDao();
     courseList = courseDao.getCourseList(new Course());
     courseDao.closeDao();
     for (Course course : courseList) {
         searchCourseComboBox.addItem(course);
         editSelectedCourseComboBox.addItem(course);
     }
 }
 private void getSelectedCourse(SelectedCourse selectedCourse){
     SelectedCourseDao selectedCourseDao = new SelectedCourseDao();
     List<SelectedCourse> selectedCourseList = selectedCourseDao.getSelectedCourseList(selectedCourse);
     DefaultTableModel dft = (DefaultTableModel) selectedCourseListTable.getModel();
     dft.setRowCount(0);
     for (SelectedCourse sc : selectedCourseList) {
         Vector v = new Vector();
         v.add(sc.getId());
         v.add(getStudentNameById(sc.getStudent_id()));
         v.add(getCourseNameById(sc.getCourse_id()));
         dft.addRow(v);
     }
     selectedCourseDao.closeDao();
 }
 private void initTable(){
     Student student = (Student) searchStudentComboBox.getSelectedItem();
     SelectedCourse sc = new SelectedCourse();
     sc.setStudent_id(student.getId());
     getSelectedCourse(sc);
 }
 private String getStudentNameById(int id){
     for (int i = 0; i < studentList.size(); i++) {
         if(studentList.get(i).getId() == id)return studentList.get(i).getName();
     }
     return "";
 }
 private String getCourseNameById(int id){
     for (int i = 0; i < courseList.size(); i++) {
         if(id == courseList.get(i).getId())return courseList.get(i).getName();
     }
     return "";
 }
 private int getCourseIdByName(String name){
     for (int i = 0; i < courseList.size(); i++) {
         if(name.equals(courseList.get(i).getName()))return courseList.get(i).getId();
     }
     return 0;
 }
 private void setAuthority(){
     if("学生".equals(MainFrm.userType.getName())){
         searchStudentComboBox.setEnabled(false);
         editSelectedStudentComboBox.setEnabled(false);
     }
 }

}


本文转载自: https://blog.csdn.net/weixin_42899150/article/details/127950511
版权归原作者 IT教程资源 所有, 如有侵权,请联系我们删除。

“基于java学生选课系统”的评论:

还没有评论