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

本文介绍了一个使用Java Swing实现的教务系统管理界面的设计与实现细节,包括学生信息、教师信息及课程信息的管理功能。
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的布局管理将被设置成CardLayout

		panel1 = 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() {
		// panel1
		panel1.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);// 设置每行的高度为20
		stusql.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() {
		// panel2
		panel2.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);// 设置每行的高度为20

		table2.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() {

		// panel3
		panel3.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);// 设置每行的高度为20

		table3.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);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String 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 block
					e1.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 block
					e1.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 block
					e1.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 block
					e1.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 block
					e1.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 block
					e1.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;"));
				}
			}
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值