小试牛刀-教务信息管理系统(AdmBoard)

来源:互联网 发布:好看网络剧推荐 编辑:程序博客网 时间:2024/04/28 18:23
package StuAdmin;import java.awt.BorderLayout;import java.awt.CardLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.JTextField;import javax.swing.SwingConstants;import javax.swing.table.DefaultTableCellRenderer;public class AdmBoard implements ActionListener, MouseListener {JFrame Admdlg;JButton button, button1, button2, button3, button4, button5, button6,button7, button8, button9, button10, exit, relogin;JPanel panel1;JPanel panel2;JPanel panel3;JPanel pane;JTable table1, table2, table3;CardLayout card;static Connection conn;static Statement st;static ResultSet rs;AdmBoard() throws SQLException {Admdlg = new JFrame("管理员窗口");Admdlg.setLayout(null);Admdlg.setBackground(Color.WHITE);relogin = new JButton("重新登录");relogin.setFocusPainted(false);relogin.addActionListener(this);relogin.setBounds(760, 50, 100, 30);relogin.setBackground(Color.WHITE);relogin.setForeground(new Color(0, 0, 50));exit = new JButton("退出系统");exit.setFocusPainted(false);exit.addActionListener(this);exit.setBounds(870, 50, 100, 30);exit.setBackground(Color.WHITE);exit.setForeground(new Color(0, 0, 50));Admdlg.add(relogin);Admdlg.add(exit);JPanel top = new JPanel();JLabel title = new JLabel("教务管理系统");Font titlefont = new Font("幼圆", Font.PLAIN, 50);title.setFont(titlefont);title.setForeground(Color.WHITE);top.add(title, BorderLayout.EAST);top.setBounds(0, 0, 984, 100);top.setBackground(new Color(0, 0, 50));top.setBorder(BorderFactory.createLineBorder(Color.BLACK));JPanel buttons = new JPanel(new GridLayout(1, 3));buttons.setBounds(0, 95, 984, 40);button = new JButton("学生信息管理");button.setBackground(Color.WHITE);button.setFont(new Font("幼圆", Font.PLAIN, 18));button.setBorder(BorderFactory.createLineBorder(Color.BLACK));button.addActionListener(this);buttons.add(button);button = new JButton("教师信息管理");button.setBackground(Color.WHITE);button.setFont(new Font("幼圆", Font.PLAIN, 18));button.setBorder(BorderFactory.createLineBorder(Color.BLACK));button.addActionListener(this);buttons.add(button);button = new JButton("课程信息管理");button.setBackground(Color.WHITE);button.setFont(new Font("幼圆", Font.PLAIN, 18));button.setBorder(BorderFactory.createLineBorder(Color.BLACK));button.addActionListener(this);buttons.add(button);Admdlg.add(buttons);card = new CardLayout(3, 3);pane = new JPanel(card); // JPanel的布局管理将被设置成CardLayoutpanel1 = new JPanel();panel2 = new JPanel();panel3 = new JPanel();panel1.setBackground(new Color(0, 0, 50));panel2.setBackground(new Color(0, 0, 50));panel3.setBackground(new Color(0, 0, 50));panel1();panel2();panel3();pane.add(panel1, "p1");pane.add(panel2, "p2");pane.add(panel3, "p3");pane.setBounds(0, 135, 985, 425);Admdlg.add(pane);Admdlg.add(top);Admdlg.setBounds(200, 80, 990, 600);Admdlg.setVisible(true);Admdlg.setResizable(false);}public void panel1() {// panel1panel1.setLayout(null);JTextField search = new JTextField();search.setBounds(5, 5, 860, 30);JButton submit = new JButton("快速查找");submit.setBounds(870, 5, 100, 30);submit.setBackground(Color.WHITE);DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中tcr.setHorizontalAlignment(SwingConstants.CENTER);// 这句和上句作用一样SQL stusql = new SQL();stusql.cases = "学生信息管理";String text = "SELECT studentid,studentname,studentsex,studentbirth,studentphone,studentadd,departmentname FROM student,department WHERE student.studentdepart=department.departmentid;";table1 = new JTable(stusql.getTable(text));table1.setDefaultRenderer(Object.class, tcr);table1.setSelectionBackground(new Color(0, 0, 50));table1.getTableHeader().setReorderingAllowed(false);table1.setSelectionForeground(Color.white);table1.setPreferredScrollableViewportSize(new Dimension(980, 350));// 设置表格的大小table1.setRowHeight(30);// 设置每行的高度为20stusql.shutDown();table1.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {button1.setForeground(new Color(0, 0, 50));button1.setBackground(Color.WHITE);button1.setOpaque(true); // 设置背景透明button1.setBorder(BorderFactory.createLineBorder(new Color(0,0, 50)));button1.setEnabled(true);button2.setForeground(new Color(0, 0, 50));button2.setBackground(Color.WHITE);button2.setOpaque(true); // 设置背景透明button2.setBorder(BorderFactory.createLineBorder(new Color(0,0, 50)));button2.setEnabled(true);}});JScrollPane tpane1 = new JScrollPane(table1);tpane1.setBounds(5, 40, 965, 320);button1 = new JButton("编辑学生记录");button1.setBounds(580, 370, 120, 30);button1.addActionListener(this);button1.setForeground(Color.GRAY);button1.setBackground(Color.WHITE);button1.setOpaque(false); // 设置背景透明button1.setBorder(BorderFactory.createLineBorder(Color.GRAY));button1.setEnabled(false);panel1.add(button1);button2 = new JButton("删除学生记录");button2.setBounds(710, 370, 120, 30);button2.addActionListener(this);button2.setForeground(Color.GRAY);button2.setBackground(Color.WHITE);button2.setOpaque(false); // 设置背景透明button2.setBorder(BorderFactory.createLineBorder(Color.GRAY));button2.setEnabled(false);panel1.add(button2);button3 = new JButton("添加学生记录");button3.setBounds(840, 370, 120, 30);button3.addActionListener(this);button3.setForeground(new Color(0, 0, 50));button3.setBackground(Color.WHITE);button3.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 50)));panel1.add(button3);panel1.add(search);panel1.add(submit);panel1.add(tpane1);}public void panel2() {// panel2panel2.setLayout(null);JTextField search = new JTextField();search.setBounds(5, 5, 860, 30);JButton submit = new JButton("快速查找");submit.setBounds(870, 5, 100, 30);submit.setBackground(Color.WHITE);SQL stusql = new SQL();stusql.cases = "教师信息管理";String text = "SELECT teacherid,teachername,teachersex,departmentname,teacherphone FROM teacher,department WHERE teacher.teacherdepart=department.departmentid;";table2 = new JTable(stusql.getTable(text));table2.setSelectionBackground(new Color(0, 0, 50));table2.setSelectionForeground(Color.white);DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中tcr.setHorizontalAlignment(SwingConstants.CENTER);// 这句和上句作用一样table2.setDefaultRenderer(Object.class, tcr);stusql.shutDown();table2.getTableHeader().setReorderingAllowed(false);table2.setPreferredScrollableViewportSize(new Dimension(980, 350));// 设置表格的大小table2.setRowHeight(30);// 设置每行的高度为20table2.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {button4.setForeground(new Color(0, 0, 50));button4.setBackground(Color.WHITE);button4.setOpaque(true); // 设置背景透明button4.setBorder(BorderFactory.createLineBorder(new Color(0,0, 50)));button4.setEnabled(true);button5.setForeground(new Color(0, 0, 50));button5.setBackground(Color.WHITE);button5.setOpaque(true); // 设置背景透明button5.setBorder(BorderFactory.createLineBorder(new Color(0,0, 50)));button5.setEnabled(true);button10.setForeground(new Color(0, 0, 50));button10.setBackground(Color.WHITE);button10.setOpaque(true); // 设置背景透明button10.setBorder(BorderFactory.createLineBorder(new Color(0,0, 50)));button10.setEnabled(true);}});JScrollPane tpane2 = new JScrollPane(table2);tpane2.setBounds(5, 40, 965, 320);button10 = new JButton("添加管理员");button10.setBounds(450, 370, 120, 30);button10.addActionListener(this);button10.setForeground(Color.GRAY);button10.setBackground(Color.WHITE);button10.setOpaque(false);button10.setBorder(BorderFactory.createLineBorder(Color.GRAY));button10.setEnabled(false);panel2.add(button10);button4 = new JButton("编辑教师记录");button4.setBounds(580, 370, 120, 30);button4.addActionListener(this);button4.setForeground(Color.GRAY);button4.setBackground(Color.WHITE);button4.setOpaque(false); // 设置背景透明button4.setBorder(BorderFactory.createLineBorder(Color.GRAY));button4.setEnabled(false);panel2.add(button4);button5 = new JButton("删除教师记录");button5.setBounds(710, 370, 120, 30);button5.addActionListener(this);button5.setForeground(Color.GRAY);button5.setBackground(Color.WHITE);button5.setOpaque(false); // 设置背景透明button5.setBorder(BorderFactory.createLineBorder(Color.GRAY));button5.setEnabled(false);panel2.add(button5);button6 = new JButton("添加教师记录");button6.setBounds(840, 370, 120, 30);button6.addActionListener(this);button6.setForeground(new Color(0, 0, 50));button6.setBackground(Color.WHITE);button6.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 50)));panel2.add(button6);panel2.add(search);panel2.add(submit);panel2.add(tpane2);}public void panel3() {// panel3panel3.setLayout(null);JTextField search = new JTextField();search.setBounds(5, 5, 860, 30);JButton submit = new JButton("快速查找");submit.setBounds(870, 5, 100, 30);submit.setBackground(Color.WHITE);SQL stusql = new SQL();stusql.cases = "课程信息管理";String text = "SELECT courseid,coursename,coursecredit,departmentname FROM course,department WHERE course.coursedepart=department.departmentid;";table3 = new JTable(stusql.getTable(text));table3.setSelectionBackground(new Color(0, 0, 50));table3.getTableHeader().setReorderingAllowed(false);table3.setSelectionForeground(Color.white);DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中tcr.setHorizontalAlignment(SwingConstants.CENTER);// 这句和上句作用一样table3.setDefaultRenderer(Object.class, tcr);stusql.shutDown();table3.setPreferredScrollableViewportSize(new Dimension(980, 350));// 设置表格的大小table3.setRowHeight(30);// 设置每行的高度为20table3.addMouseListener(new MouseAdapter() {public void mouseClicked(MouseEvent e) {button7.setForeground(new Color(0, 0, 50));button7.setBackground(Color.WHITE);button7.setOpaque(true); // 设置背景透明button7.setBorder(BorderFactory.createLineBorder(new Color(0,0, 50)));button7.setEnabled(true);button8.setForeground(new Color(0, 0, 50));button8.setBackground(Color.WHITE);button8.setOpaque(true); // 设置背景透明button8.setBorder(BorderFactory.createLineBorder(new Color(0,0, 50)));button8.setEnabled(true);}});JScrollPane tpane3 = new JScrollPane(table3);tpane3.setBounds(5, 40, 965, 320);button7 = new JButton("编辑课程记录");button7.setBounds(580, 370, 120, 30);button7.addActionListener(this);button7.setForeground(Color.GRAY);button7.setBackground(Color.WHITE);button7.setOpaque(false); // 设置背景透明button7.setBorder(BorderFactory.createLineBorder(Color.GRAY));button7.setEnabled(false);panel3.add(button7);button8 = new JButton("删除课程记录");button8.setBounds(710, 370, 120, 30);button8.addActionListener(this);button8.setForeground(Color.GRAY);button8.setBackground(Color.WHITE);button8.setOpaque(false); // 设置背景透明button8.setBorder(BorderFactory.createLineBorder(Color.GRAY));button8.setEnabled(false);panel3.add(button8);button9 = new JButton("添加课程记录");button9.setBounds(840, 370, 120, 30);button9.addActionListener(this);button9.setForeground(new Color(0, 0, 50));button9.setBackground(Color.WHITE);button9.setBorder(BorderFactory.createLineBorder(new Color(0, 0, 50)));panel3.add(button9);panel3.add(search);panel3.add(submit);panel3.add(tpane3);}@Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubString cmd = e.getActionCommand();if (cmd.equals("学生信息管理")) {card.show(pane, "p1");} else if (cmd.equals("教师信息管理")) {card.show(pane, "p2");} else if (cmd.equals("课程信息管理")) {card.show(pane, "p3");}if (cmd.equals("退出系统")) {Admdlg.dispose();} else if (cmd.equals("重新登录")) {Admdlg.dispose();new StuAdmin();}Operates operate = null;int row;if (cmd.equals("添加学生记录")) {operate = new Operates(Admdlg, 1);if (operate.Confirm == 1) {SQL newsql = new SQL();table1.setModel(newsql.getTable("SELECT studentid,studentname,studentsex,studentbirth,studentphone,studentadd,departmentname FROM student,department WHERE student.studentdepart=department.departmentid;"));}} else if (cmd.equals("编辑学生记录")) {row = table1.getSelectedColumn();if (row != -1) {String studentid = table1.getValueAt(table1.getSelectedRow(), 0).toString();try {operate = new Operates(Admdlg, 1, studentid);} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}if (operate.Confirm == 1) {SQL newsql = new SQL();table1.setModel(newsql.getTable("SELECT studentid,studentname,studentsex,studentbirth,studentphone,studentadd,departmentname FROM student,department WHERE student.studentdepart=department.departmentid;"));}}}else if (cmd.equals("删除学生记录")) {row = table1.getSelectedColumn();if (row != -1) {String studentid = table1.getValueAt(table1.getSelectedRow(), 0).toString();try {operate = new Operates(Admdlg, 2, studentid);} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}if (operate.Confirm == 1) {SQL newsql = new SQL();table1.setModel(newsql.getTable("SELECT studentid,studentname,studentsex,studentbirth,studentphone,studentadd,departmentname FROM student,department WHERE student.studentdepart=department.departmentid;"));}}} else if (cmd.equals("添加管理员")) {row = table2.getSelectedColumn();if (row != -1) {String teacherid = table2.getValueAt(table2.getSelectedRow(), 0).toString();SQL admsql = new SQL();String text = "INSERT INTO admin(adminid) VALUES ('"+ teacherid + "');";if (admsql.othersql(text) > 0) {JOptionPane.showMessageDialog(null, "添加管理员成功!");}}} else if (cmd.equals("添加教师记录")) {operate = new Operates(Admdlg, 2);if (operate.Confirm == 1) {SQL newsql = new SQL();table2.setModel(newsql.getTable("SELECT teacherid,teachername,teachersex,departmentname,teacherphone FROM teacher,department WHERE teacher.teacherdepart=department.departmentid;"));}} else if (cmd.equals("编辑教师记录")) {row = table2.getSelectedColumn();if (row != -1) {String teacherid = table2.getValueAt(table2.getSelectedRow(), 0).toString();try {operate = new Operates(Admdlg, 3, teacherid);} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}if (operate.Confirm == 1) {SQL newsql = new SQL();table2.setModel(newsql.getTable("SELECT teacherid,teachername,teachersex,departmentname,teacherphone FROM teacher,department WHERE teacher.teacherdepart=department.departmentid;"));}}} else if (cmd.equals("删除教师记录")) {row = table2.getSelectedColumn();if (row != -1) {String teacherid = table2.getValueAt(table2.getSelectedRow(), 0).toString();try {operate = new Operates(Admdlg, 4, teacherid);} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}if (operate.Confirm == 1) {SQL newsql = new SQL();table2.setModel(newsql.getTable("SELECT teacherid,teachername,teachersex,departmentname,teacherphone FROM teacher,department WHERE teacher.teacherdepart=department.departmentid;"));}}} else if (cmd.equals("添加课程记录")) {operate = new Operates(Admdlg, 3);if (operate.Confirm == 1) {SQL newsql = new SQL();table3.setModel(newsql.getTable("SELECT courseid,coursename,coursecredit,departmentname FROM course,department WHERE course.coursedepart=department.departmentid;"));}} else if (cmd.equals("编辑课程记录")) {row = table3.getSelectedColumn();if (row != -1) {String courseid = table3.getValueAt(table3.getSelectedRow(), 0).toString();try {operate = new Operates(Admdlg, 5, courseid);} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}if (operate.Confirm == 1) {SQL newsql = new SQL();table3.setModel(newsql.getTable("SELECT courseid,coursename,coursecredit,departmentname FROM course,department WHERE course.coursedepart=department.departmentid;"));}}} else if (cmd.equals("删除课程记录")) {row = table3.getSelectedColumn();if (row != -1) {String courseid = table3.getValueAt(table3.getSelectedRow(), 0).toString();try {operate = new Operates(Admdlg, 6, courseid);} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}if (operate.Confirm == 1) {SQL newsql = new SQL();table3.setModel(newsql.getTable("SELECT courseid,coursename,coursecredit,departmentname FROM course,department WHERE course.coursedepart=department.departmentid;"));}}}}@Overridepublic void mouseClicked(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mousePressed(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mouseReleased(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mouseEntered(MouseEvent e) {// TODO Auto-generated method stub}@Overridepublic void mouseExited(MouseEvent e) {// TODO Auto-generated method stub}}

0 0