接上一篇~~
教师信息的删改查:
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.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;
import javax.swing.ButtonGroup;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import com.artisan.dao.TeacherDao;
import com.artisan.model.Teacher;
import com.artisan.util.StringUtil;
public class ManageTeacherFrm extends JInternalFrame {
private JTable teacherListTable;
private JTextField searchTeacherNameTextField;
private JTextField editTeacherNameTextField;
private JTextField editTeacherTitleTextField;
private JTextField editTeacherAgeTextField;
private JPasswordField editTeacherPasswordField;
private JRadioButton editTeacherSexManRadioButton;
private JRadioButton editTeacherSexFemalRadioButton;
private JButton deleteTeacherButton;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ManageTeacherFrm frame = new ManageTeacherFrm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ManageTeacherFrm() {
setClosable(true);
setIconifiable(true);
setTitle("\u6559\u5E08\u4FE1\u606F\u7BA1\u7406");
setBounds(100, 100, 805, 691);
JScrollPane scrollPane = new JScrollPane();
JLabel label = new JLabel("\u6559\u5E08\u59D3\u540D\uFF1A");
label.setIcon(new ImageIcon(ManageTeacherFrm.class.getResource("/images/\u8001\u5E08.png")));
label.setFont(new Font("微软雅黑", Font.PLAIN, 14));
searchTeacherNameTextField = new JTextField();
searchTeacherNameTextField.setColumns(10);
JButton searchTeacherButton = new JButton("\u67E5\u8BE2");
searchTeacherButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
searchTeacher(e);
}
});
searchTeacherButton.setIcon(new ImageIcon(ManageTeacherFrm.class.getResource("/images/\u641C\u7D22.png")));
searchTeacherButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u6559\u5E08\u4FE1\u606F\u4FEE\u6539", TitledBorder.LEADING, TitledBorder.TOP, null, null));
GroupLayout groupLayout = new GroupLayout(getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING, false)
.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
.addGap(178)
.addComponent(label)
.addGap(33)
.addComponent(searchTeacherNameTextField, GroupLayout.PREFERRED_SIZE, 179, GroupLayout.PREFERRED_SIZE)
.addGap(66)
.addComponent(searchTeacherButton))
.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
.addGap(92)
.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(panel, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 610, Short.MAX_VALUE)
.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 610, Short.MAX_VALUE))))
.addContainerGap(92, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(33)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(searchTeacherNameTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label)
.addComponent(searchTeacherButton))
.addGap(18)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 323, GroupLayout.PREFERRED_SIZE)
.addGap(29)
.addComponent(panel, GroupLayout.PREFERRED_SIZE, 175, GroupLayout.PREFERRED_SIZE)
.addContainerGap(56, Short.MAX_VALUE))
);
JLabel label_1 = new JLabel("\u6559\u5E08\u59D3\u540D\uFF1A");
label_1.setIcon(new ImageIcon(ManageTeacherFrm.class.getResource("/images/\u8001\u5E08.png")));
label_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
editTeacherNameTextField = new JTextField();
editTeacherNameTextField.setColumns(10);
JLabel label_2 = new JLabel("\u6559\u5E08\u6027\u522B\uFF1A");
label_2.setIcon(new ImageIcon(ManageTeacherFrm.class.getResource("/images/\u6027\u522B.png")));
label_2.setFont(new Font("微软雅黑", Font.PLAIN, 14));
ButtonGroup buttonGroup = new ButtonGroup();
editTeacherSexManRadioButton = new JRadioButton("\u7537");
editTeacherSexManRadioButton.setSelected(true);
editTeacherSexFemalRadioButton = new JRadioButton("\u5973");
buttonGroup.add(editTeacherSexManRadioButton);
buttonGroup.add(editTeacherSexFemalRadioButton);
JLabel label_3 = new JLabel("\u6559\u5E08\u804C\u79F0\uFF1A");
label_3.setIcon(new ImageIcon(ManageTeacherFrm.class.getResource("/images/\u804C\u79F0\u8BC4\u5B9A.png")));
label_3.setFont(new Font("微软雅黑", Font.PLAIN, 14));
editTeacherTitleTextField = new JTextField();
editTeacherTitleTextField.setColumns(10);
JLabel label_4 = new JLabel("\u6559\u5E08\u5E74\u9F84\uFF1A");
label_4.setIcon(new ImageIcon(ManageTeacherFrm.class.getResource("/images/\u5E74\u9F84.png")));
label_4.setFont(new Font("微软雅黑", Font.PLAIN, 14));
editTeacherAgeTextField = new JTextField();
editTeacherAgeTextField.setColumns(10);
JLabel label_5 = new JLabel("\u767B\u5F55\u5BC6\u7801\uFF1A");
label_5.setIcon(new ImageIcon(ManageTeacherFrm.class.getResource("/images/\u5BC6\u7801.png")));
label_5.setFont(new Font("微软雅黑", Font.PLAIN, 14));
editTeacherPasswordField = new JPasswordField();
JButton editTeacherSubmitButton = new JButton("\u786E\u8BA4\u4FEE\u6539");
editTeacherSubmitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
editTeacherAct(ae);
}
});
editTeacherSubmitButton.setIcon(new ImageIcon(ManageTeacherFrm.class.getResource("/images/\u786E\u8BA4.png")));
editTeacherSubmitButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
deleteTeacherButton = new JButton("\u5220\u9664\u4FE1\u606F");
deleteTeacherButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
deleteTeacher(ae);
}
});
deleteTeacherButton.setIcon(new ImageIcon(ManageTeacherFrm.class.getResource("/images/\u5220\u9664.png")));
deleteTeacherButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
GroupLayout gl_panel = new GroupLayout(panel);
gl_panel.setHorizontalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGap(24)
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addComponent(label_1)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(editTeacherNameTextField, GroupLayout.PREFERRED_SIZE, 122, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_panel.createSequentialGroup()
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addComponent(label_3)
.addComponent(label_5))
.addPreferredGap(ComponentPlacement.RELATED)
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addComponent(editTeacherPasswordField, GroupLayout.DEFAULT_SIZE, 122, Short.MAX_VALUE)
.addComponent(editTeacherTitleTextField))))
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGap(27)
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addComponent(label_2)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(editTeacherSexManRadioButton)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(editTeacherSexFemalRadioButton))
.addGroup(gl_panel.createSequentialGroup()
.addComponent(label_4)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(editTeacherAgeTextField, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE))))
.addGroup(gl_panel.createSequentialGroup()
.addGap(39)
.addComponent(editTeacherSubmitButton)
.addGap(50)
.addComponent(deleteTeacherButton)))
.addGap(67))
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addContainerGap()
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(label_1)
.addComponent(editTeacherNameTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label_2)
.addComponent(editTeacherSexManRadioButton)
.addComponent(editTeacherSexFemalRadioButton))
.addPreferredGap(ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(label_3)
.addComponent(editTeacherTitleTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label_4)
.addComponent(editTeacherAgeTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(29)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(label_5)
.addComponent(editTeacherPasswordField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(editTeacherSubmitButton)
.addComponent(deleteTeacherButton))
.addGap(19))
);
panel.setLayout(gl_panel);
teacherListTable = new JTable();
teacherListTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent arg0) {
selectedTableRow(arg0);
}
});
teacherListTable.setModel(new DefaultTableModel(
new Object[][] {
},
new String[] {
"\u6559\u5E08ID", "\u6559\u5E08\u59D3\u540D", "\u6559\u5E08\u6027\u522B", "\u6559\u5E08\u804C\u79F0", "\u6559\u5E08\u5E74\u9F84", "\u767B\u5F55\u5BC6\u7801"
}
) {
boolean[] columnEditables = new boolean[] {
false, false, false, false, false, false
};
public boolean isCellEditable(int row, int column) {
return columnEditables[column];
}
});
scrollPane.setViewportView(teacherListTable);
getContentPane().setLayout(groupLayout);
setTable(new Teacher());
setAuthority();
}
protected void editTeacherAct(ActionEvent ae) {
// TODO Auto-generated method stub
int row = teacherListTable.getSelectedRow();
if(row == -1){
JOptionPane.showMessageDialog(this, "请选择要修改的数据!");
return;
}
String teacherName = editTeacherNameTextField.getText().toString();
String teacherSex = editTeacherSexManRadioButton.isSelected() ? editTeacherSexManRadioButton.getText().toString() : editTeacherSexFemalRadioButton.getText().toString();
String teacherTitle = editTeacherTitleTextField.getText().toString();
int teacherAge = 0;
try {
teacherAge = Integer.parseInt(editTeacherAgeTextField.getText().toString());
} catch (Exception e) {
// TODO: handle exception
JOptionPane.showMessageDialog(this, "年龄只允许输入数字!");
return;
}
String teacherPassword = editTeacherPasswordField.getText().toString();
if(StringUtil.isEmpty(teacherName)){
JOptionPane.showMessageDialog(this, "教师姓名必须填写!");
return;
}
if(StringUtil.isEmpty(teacherTitle)){
JOptionPane.showMessageDialog(this, "教师职称必须填写!");
return;
}
if(teacherAge == 0 || teacherAge < 0){
JOptionPane.showMessageDialog(this, "教师年龄必须大于0!");
return;
}
if(StringUtil.isEmpty(teacherPassword)){
JOptionPane.showMessageDialog(this, "教师登录密码必须填写!");
return;
}
Teacher teacher = new Teacher();
teacher.setId(Integer.parseInt(teacherListTable.getValueAt(row, 0).toString()));
teacher.setName(teacherName);
teacher.setSex(teacherSex);
teacher.setTitle(teacherTitle);
teacher.setAge(teacherAge);
teacher.setPassword(teacherPassword);
TeacherDao teacherDao = new TeacherDao();
if(teacherDao.update(teacher)){
JOptionPane.showMessageDialog(this, "修改成功!");
}else{
JOptionPane.showMessageDialog(this, "修改失败!");
}
teacherDao.closeDao();
setTable(new Teacher());
}
protected void searchTeacher(ActionEvent e) {
// TODO Auto-generated method stub
String teacherNameString = searchTeacherNameTextField.getText().toString();
Teacher teacher = new Teacher();
teacher.setName(teacherNameString);
setTable(teacher);
}
protected void deleteTeacher(ActionEvent ae) {
// TODO Auto-generated method stub
int row = teacherListTable.getSelectedRow();
if(row == -1){
JOptionPane.showMessageDialog(this, "请选择要删除的数据!");
return;
}
if(JOptionPane.showConfirmDialog(this, "确定要删除么?") != JOptionPane.OK_OPTION)return;
int id = Integer.parseInt(teacherListTable.getValueAt(row, 0).toString());
TeacherDao teacherDao = new TeacherDao();
if(teacherDao.delete(id)){
JOptionPane.showMessageDialog(this, "删除成功!");
}else{
JOptionPane.showMessageDialog(this, "删除失败!");
}
teacherDao.closeDao();
setTable(new Teacher());
}
private void setTable(Teacher teacher){
if("教师".equals(MainFrm.userType.getName())){
Teacher tLogined = (Teacher) MainFrm.userObject;
teacher.setName(tLogined.getName());
searchTeacherNameTextField.setText(teacher.getName());
}
DefaultTableModel dft = (DefaultTableModel) teacherListTable.getModel();
dft.setRowCount(0);
TeacherDao teacherDao = new TeacherDao();
List teacherList = teacherDao.getTeacherList(teacher);
for (Teacher t : teacherList) {
Vector v = new Vector();
v.add(t.getId());
v.add(t.getName());
v.add(t.getSex());
v.add(t.getTitle());
v.add(t.getAge());
v.add(t.getPassword());
dft.addRow(v);
}
teacherDao.closeDao();
}
protected void selectedTableRow(MouseEvent me) {
// TODO Auto-generated method stub
DefaultTableModel dft = (DefaultTableModel) teacherListTable.getModel();
editTeacherNameTextField.setText(dft.getValueAt(teacherListTable.getSelectedRow(), 1).toString());
editTeacherTitleTextField.setText(dft.getValueAt(teacherListTable.getSelectedRow(), 3).toString());
editTeacherAgeTextField.setText(dft.getValueAt(teacherListTable.getSelectedRow(), 4).toString());
editTeacherPasswordField.setText(dft.getValueAt(teacherListTable.getSelectedRow(), 5).toString());
String sex = dft.getValueAt(teacherListTable.getSelectedRow(), 2).toString();
if(sex.equals(editTeacherSexManRadioButton.getText()))editTeacherSexManRadioButton.setSelected(true);
if(sex.equals(editTeacherSexFemalRadioButton.getText()))editTeacherSexFemalRadioButton.setSelected(true);
}
private void setAuthority(){
if("教师".equals(MainFrm.userType.getName())){
deleteTeacherButton.setEnabled(false);
searchTeacherNameTextField.setEditable(false);
}
}
}
课程的删改查:
package com.artisan.view;
import java.awt.EventQueue;
import java.awt.Font;
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.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import com.artisan.dao.CourseDao;
import com.artisan.dao.TeacherDao;
import com.artisan.model.Course;
import com.artisan.model.Teacher;
import com.artisan.util.StringUtil;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class ManageCourseFrm extends JInternalFrame {
private JTextField searchCourseNameTextField;
private JTable courseListTable;
private JTextField editCourseTextField;
private JTextField editCourseStudentNumTextField;
private JComboBox editCourseTeachComboBox;
private JTextArea editCourseInfoTextArea;
private List teacherList = new ArrayList();
private JComboBox searchTeacherComboBox;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ManageCourseFrm frame = new ManageCourseFrm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ManageCourseFrm() {
setTitle("\u8BFE\u7A0B\u4FE1\u606F\u7BA1\u7406");
setBounds(100, 100, 819, 704);
setClosable(true);
setIconifiable(true);
JLabel label = new JLabel("\u8BFE\u7A0B\u540D\u79F0\uFF1A");
label.setIcon(new ImageIcon(ManageCourseFrm.class.getResource("/images/\u8BFE\u7A0B.png")));
label.setFont(new Font("微软雅黑", Font.PLAIN, 14));
searchCourseNameTextField = new JTextField();
searchCourseNameTextField.setColumns(10);
JLabel label_1 = new JLabel("\u6388\u8BFE\u8001\u5E08\uFF1A");
label_1.setIcon(new ImageIcon(ManageCourseFrm.class.getResource("/images/\u8001\u5E08.png")));
label_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
searchTeacherComboBox = new JComboBox();
JButton searchButton = new JButton("\u67E5\u8BE2");
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
searchCourse(ae);
}
});
searchButton.setIcon(new ImageIcon(ManageCourseFrm.class.getResource("/images/\u641C\u7D22.png")));
searchButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
JScrollPane scrollPane = new JScrollPane();
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(null, "\u7F16\u8F91\u8BFE\u7A0B\u4FE1\u606F", TitledBorder.LEADING, TitledBorder.TOP, null, null));
GroupLayout groupLayout = new GroupLayout(getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(81)
.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
.addComponent(panel, Alignment.LEADING, 0, 0, Short.MAX_VALUE)
.addGroup(Alignment.LEADING, groupLayout.createParallelGroup(Alignment.TRAILING, false)
.addComponent(scrollPane, Alignment.LEADING)
.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
.addComponent(label)
.addGap(18)
.addComponent(searchCourseNameTextField, GroupLayout.PREFERRED_SIZE, 131, GroupLayout.PREFERRED_SIZE)
.addGap(32)
.addComponent(label_1)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(searchTeacherComboBox, GroupLayout.PREFERRED_SIZE, 122, GroupLayout.PREFERRED_SIZE)
.addGap(50)
.addComponent(searchButton))))
.addContainerGap(116, Short.MAX_VALUE))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(38)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(label)
.addComponent(searchCourseNameTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label_1)
.addComponent(searchTeacherComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(searchButton))
.addGap(41)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 266, GroupLayout.PREFERRED_SIZE)
.addGap(35)
.addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addContainerGap(31, Short.MAX_VALUE))
);
JLabel label_2 = new JLabel("\u8BFE\u7A0B\u540D\u79F0\uFF1A");
label_2.setIcon(new ImageIcon(ManageCourseFrm.class.getResource("/images/\u8BFE\u7A0B.png")));
label_2.setFont(new Font("微软雅黑", Font.PLAIN, 14));
editCourseTextField = new JTextField();
editCourseTextField.setColumns(10);
JLabel label_3 = new JLabel("\u6388\u8BFE\u8001\u5E08\uFF1A");
label_3.setIcon(new ImageIcon(ManageCourseFrm.class.getResource("/images/\u8001\u5E08.png")));
label_3.setFont(new Font("微软雅黑", Font.PLAIN, 14));
editCourseTeachComboBox = new JComboBox();
JLabel label_4 = new JLabel("\u5B66\u751F\u4EBA\u6570\uFF1A");
label_4.setIcon(new ImageIcon(ManageCourseFrm.class.getResource("/images/\u4EBA\u6570.png")));
label_4.setFont(new Font("微软雅黑", Font.PLAIN, 14));
editCourseStudentNumTextField = new JTextField();
editCourseStudentNumTextField.setColumns(10);
JLabel label_5 = new JLabel("\u8BFE\u7A0B\u4ECB\u7ECD\uFF1A");
label_5.setIcon(new ImageIcon(ManageCourseFrm.class.getResource("/images/\u4ECB\u7ECD.png")));
label_5.setFont(new Font("微软雅黑", Font.PLAIN, 14));
editCourseInfoTextArea = new JTextArea();
JButton submitEditButton = new JButton("\u786E\u8BA4\u4FEE\u6539");
submitEditButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
editCourseSubmit(ae);
}
});
submitEditButton.setIcon(new ImageIcon(ManageCourseFrm.class.getResource("/images/\u786E\u8BA4.png")));
submitEditButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
JButton deleteCourseButton = new JButton("\u5220\u9664\u8BFE\u7A0B");
deleteCourseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
deleteCourse(ae);
}
});
deleteCourseButton.setIcon(new ImageIcon(ManageCourseFrm.class.getResource("/images/\u5220\u9664.png")));
deleteCourseButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
GroupLayout gl_panel = new GroupLayout(panel);
gl_panel.setHorizontalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGap(37)
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false)
.addGroup(gl_panel.createSequentialGroup()
.addComponent(label_4)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(editCourseStudentNumTextField))
.addGroup(gl_panel.createSequentialGroup()
.addComponent(label_2)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(editCourseTextField, GroupLayout.PREFERRED_SIZE, 112, GroupLayout.PREFERRED_SIZE)))
.addGap(18)
.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addComponent(label_3)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(editCourseTeachComboBox, GroupLayout.PREFERRED_SIZE, 153, GroupLayout.PREFERRED_SIZE))
.addGroup(gl_panel.createSequentialGroup()
.addComponent(label_5)
.addGap(18)
.addComponent(editCourseInfoTextArea)))
.addContainerGap(118, Short.MAX_VALUE))
.addGroup(gl_panel.createSequentialGroup()
.addGap(121)
.addComponent(submitEditButton)
.addGap(113)
.addComponent(deleteCourseButton)
.addContainerGap(160, Short.MAX_VALUE))
);
gl_panel.setVerticalGroup(
gl_panel.createParallelGroup(Alignment.LEADING)
.addGroup(gl_panel.createSequentialGroup()
.addGap(27)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(label_2)
.addComponent(editCourseTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label_3)
.addComponent(editCourseTeachComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(43)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(label_4)
.addComponent(editCourseStudentNumTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label_5)
.addComponent(editCourseInfoTextArea, GroupLayout.PREFERRED_SIZE, 65, GroupLayout.PREFERRED_SIZE))
.addGap(18)
.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
.addComponent(submitEditButton)
.addComponent(deleteCourseButton))
.addContainerGap(15, Short.MAX_VALUE))
);
panel.setLayout(gl_panel);
courseListTable = new JTable();
courseListTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent me) {
selectedCourse(me);
}
});
courseListTable.setModel(new DefaultTableModel(
new Object[][] {
},
new String[] {
"\u8BFE\u7A0B\u7F16\u53F7", "\u8BFE\u7A0B\u540D\u79F0", "\u6388\u8BFE\u8001\u5E08", "\u8BFE\u7A0B\u6700\u5927\u4EBA\u6570", "\u5DF2\u9009\u4EBA\u6570", "\u8BFE\u7A0B\u4ECB\u7ECD"
}
) {
boolean[] columnEditables = new boolean[] {
false, true, false, false, false, false
};
public boolean isCellEditable(int row, int column) {
return columnEditables[column];
}
});
courseListTable.getColumnModel().getColumn(3).setPreferredWidth(90);
courseListTable.getColumnModel().getColumn(5).setPreferredWidth(225);
scrollPane.setViewportView(courseListTable);
getContentPane().setLayout(groupLayout);
setTeacherCombox();
setCourseListTable(new Course());
}
protected void editCourseSubmit(ActionEvent ae) {
// TODO Auto-generated method stub
int row = courseListTable.getSelectedRow();
if(row == -1){
JOptionPane.showMessageDialog(this, "请选中要修改的数据!");
return;
}
int course_id = Integer.parseInt(courseListTable.getValueAt(row, 0).toString());
Teacher teacher = (Teacher) editCourseTeachComboBox.getSelectedItem();
String courseName = editCourseTextField.getText().toString();
if(StringUtil.isEmpty(courseName)){
JOptionPane.showMessageDialog(this, "课程名称不能为空!");
return;
}
int max_student_num = 0;
try {
max_student_num = Integer.parseInt(editCourseStudentNumTextField.getText().toString());
} catch (Exception e) {
// TODO: handle exception
JOptionPane.showMessageDialog(this, "学生人数请输入大于0的整数!");
return;
}
if(max_student_num <= 0){
JOptionPane.showMessageDialog(this, "学生人数请输入大于0的整数!");
return;
}
String courseInfo = editCourseInfoTextArea.getText().toString();
Course course = new Course();
course.setId(course_id);
course.setName(courseName);
course.setTeacher_id(teacher.getId());
course.setMax_student_num(max_student_num);
course.setInfo(courseInfo);
CourseDao courseDao = new CourseDao();
if(courseDao.update(course)){
JOptionPane.showMessageDialog(this, "修改成功!");
}else{
JOptionPane.showMessageDialog(this, "修改失败!");
}
courseDao.closeDao();
setCourseListTable(new Course());
}
protected void selectedCourse(MouseEvent me) {
// TODO Auto-generated method stub
int row = courseListTable.getSelectedRow();
String couseName = courseListTable.getValueAt(row, 1).toString();
int teacher_id = getTeacherIdByName(courseListTable.getValueAt(row, 2).toString());
int max_student_num = Integer.parseInt(courseListTable.getValueAt(row, 3).toString());
String couseInfo = courseListTable.getValueAt(row, 5).toString();
editCourseTextField.setText(couseName);
editCourseStudentNumTextField.setText(max_student_num+"");
editCourseInfoTextArea.setText(couseInfo);
for(int i=0;i courseList = courseDao.getCourseList(course);
DefaultTableModel dft = (DefaultTableModel) courseListTable.getModel();
dft.setRowCount(0);
for (Course c : courseList) {
Vector v = new Vector();
v.add(c.getId());
v.add(c.getName());
v.add(getTeacherNameById(c.getTeacher_id()));
v.add(c.getMax_student_num());
v.add(c.getSelected_num());
v.add(c.getInfo());
dft.addRow(v);
}
courseDao.closeDao();
}
private void setTeacherCombox(){
TeacherDao teacherDao = new TeacherDao();
teacherList = teacherDao.getTeacherList(new Teacher());
teacherDao.closeDao();
for (Teacher teacher : teacherList) {
editCourseTeachComboBox.addItem(teacher);
searchTeacherComboBox.addItem(teacher);
}
}
private String getTeacherNameById(int teacher_id){
String retString = "";
for (Teacher teacher : teacherList) {
if(teacher.getId() == teacher_id){
retString = teacher.getName();
break;
}
}
return retString;
}
private int getTeacherIdByName(String teacher_name){
int retId = -1;
for (Teacher teacher : teacherList) {
if(teacher_name.equals(teacher.getName())){
retId = teacher.getId();
break;
}
}
return retId;
}
}
登录界面:
package com.artisan.view;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;
import com.artisan.dao.AdminDao;
import com.artisan.dao.StudentDao;
import com.artisan.dao.TeacherDao;
import com.artisan.model.Admin;
import com.artisan.model.Student;
import com.artisan.model.Teacher;
import com.artisan.model.UserType;
import com.artisan.util.StringUtil;
public class LoginFrm extends JFrame {
private JPanel contentPane;
private JTextField userNameTextField;
private JPasswordField passwordTextField;
private JComboBox userTypeComboBox;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginFrm frame = new LoginFrm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LoginFrm() {
setTitle("\u767B\u9646\u754C\u9762");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 540, 379);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
setLocationRelativeTo(null);
JLabel label = new JLabel("\u5B66\u751F\u4FE1\u606F\u7CFB\u7EDF\u767B\u9646\u754C\u9762");
label.setIcon(new ImageIcon(LoginFrm.class.getResource("/images/logo.png")));
label.setFont(new Font("微软雅黑", Font.BOLD, 18));
JLabel label_1 = new JLabel("\u7528\u6237\u540D\uFF1A");
label_1.setIcon(new ImageIcon(LoginFrm.class.getResource("/images/\u7528\u6237\u540D.png")));
label_1.setFont(new Font("微软雅黑", Font.PLAIN, 14));
userNameTextField = new JTextField();
userNameTextField.setColumns(10);
JLabel label_2 = new JLabel("\u5BC6 \u7801\uFF1A");
label_2.setFont(new Font("微软雅黑", Font.PLAIN, 14));
label_2.setIcon(new ImageIcon(LoginFrm.class.getResource("/images/\u5BC6\u7801.png")));
passwordTextField = new JPasswordField();
passwordTextField.setColumns(10);
JLabel label_3 = new JLabel("\u7528\u6237\u7C7B\u578B\uFF1A");
label_3.setIcon(new ImageIcon(LoginFrm.class.getResource("/images/userType.png")));
label_3.setFont(new Font("微软雅黑", Font.PLAIN, 14));
userTypeComboBox = new JComboBox();
userTypeComboBox.setModel(new DefaultComboBoxModel(new UserType[] {UserType.ADMIN, UserType.TEACHER, UserType.STUDENT}));
userTypeComboBox.setFont(new Font("微软雅黑", Font.PLAIN, 14));
JButton loginButton = new JButton("\u767B\u5F55");
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
loginAct(ae);
}
});
loginButton.setIcon(new ImageIcon(LoginFrm.class.getResource("/images/\u767B\u5F55.png")));
loginButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
JButton resetButton = new JButton("\u91CD\u7F6E");
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
restValue(ae);
}
});
resetButton.setIcon(new ImageIcon(LoginFrm.class.getResource("/images/\u91CD\u7F6E.png")));
resetButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap(118, Short.MAX_VALUE)
.addComponent(label))
.addGroup(Alignment.LEADING, gl_contentPane.createSequentialGroup()
.addGap(134)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(label_1)
.addComponent(label_2))
.addGap(18)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(passwordTextField, GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE)
.addComponent(userNameTextField, GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)))
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(label_3)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(userTypeComboBox, 0, 162, Short.MAX_VALUE)))))
.addGap(116))
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(156)
.addComponent(loginButton)
.addGap(61)
.addComponent(resetButton)
.addContainerGap(127, Short.MAX_VALUE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addGap(26)
.addComponent(label)
.addGap(33)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(label_1)
.addComponent(userNameTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(28)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(label_2)
.addComponent(passwordTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGap(36)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(label_3)
.addComponent(userTypeComboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(resetButton)
.addComponent(loginButton))
.addGap(22))
);
contentPane.setLayout(gl_contentPane);
}
protected void loginAct(ActionEvent ae) {
// TODO Auto-generated method stub
String userName = userNameTextField.getText().toString();
String password = passwordTextField.getText().toString();
UserType selectedItem = (UserType)userTypeComboBox.getSelectedItem();
if(StringUtil.isEmpty(userName)){
JOptionPane.showMessageDialog(this, "用户名不能为空!");
return;
}
if(StringUtil.isEmpty(password)){
JOptionPane.showMessageDialog(this, "密码不能为空!");
return;
}
Admin admin = null;
if("系统管理员".equals(selectedItem.getName())){
AdminDao adminDao = new AdminDao();
Admin adminTmp = new Admin();
adminTmp.setName(userName);
adminTmp.setPassword(password);
admin = adminDao.login(adminTmp);
adminDao.closeDao();
if(admin == null){
JOptionPane.showMessageDialog(this, "用户名或密码错误!");
return;
}
JOptionPane.showMessageDialog(this, "欢迎【"+selectedItem.getName()+"】:"+admin.getName()+"登录本系统!");
this.dispose();
new MainFrm(selectedItem, admin).setVisible(true);
}else if("教师".equals(selectedItem.getName())){
//教师登录
Teacher teacher = null;
TeacherDao teacherDao = new TeacherDao();
Teacher teacherTmp = new Teacher();
teacherTmp.setName(userName);
teacherTmp.setPassword(password);
teacher = teacherDao.login(teacherTmp);
teacherDao.closeDao();
if(teacher == null){
JOptionPane.showMessageDialog(this, "用户名或密码错误!");
return;
}
JOptionPane.showMessageDialog(this, "欢迎【"+selectedItem.getName()+"】:"+teacher.getName()+"登录本系统!");
this.dispose();
new MainFrm(selectedItem, teacher).setVisible(true);
}else{
//学生登录
Student student = null;
StudentDao studentDao = new StudentDao();
Student studentTmp = new Student();
studentTmp.setName(userName);
studentTmp.setPassword(password);
student = studentDao.login(studentTmp);
studentDao.closeDao();
if(student == null){
JOptionPane.showMessageDialog(this, "用户名或密码错误!");
return;
}
JOptionPane.showMessageDialog(this, "欢迎【"+selectedItem.getName()+"】:"+student.getName()+"登录本系统!");
this.dispose();
new MainFrm(selectedItem, student).setVisible(true);
}
}
protected void restValue(ActionEvent ae) {
// TODO Auto-generated method stub
userNameTextField.setText("");
passwordTextField.setText("");
userTypeComboBox.setSelectedIndex(0);
}
}
选课的增删改查:
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 studentList = new ArrayList();
private List courseList = new ArrayList();
/**
* 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 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);
}
}
}
见下一篇哦~