小試牛刀-教務信息管理系統(StuBoard)

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.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;

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;
import javax.swing.table.DefaultTableModel;

public class StuBoard implements MouseListener, ActionListener {
	JFrame Studlg;
	JButton button, exit, relogin, select;
	JPanel panel1, panel2, panel3;
	JPanel pane;
	JTable table1, table2, table3, table4;
	CardLayout card;
	String studentid;

	StuBoard(String sno) throws SQLException {
		studentid = sno;
		Studlg = new JFrame("學生窗口");
		Studlg.setLayout(null);
		Studlg.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));

		JPanel top = new JPanel();
		JLabel title = new JLabel("教務管理系統");
		title.setFont(new Font("黑體", Font.PLAIN, 50));
		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);
		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);

		Studlg.add(relogin);
		Studlg.add(exit);
		Studlg.add(buttons);
		Studlg.add(pane);
		Studlg.add(top);
		Studlg.setBounds(200, 80, 990, 600);
		Studlg.setVisible(true);
		Studlg.setResizable(false);
	}

	public void panel1() throws SQLException {
		SQL idsql = new SQL();
		String query = "SELECT studentid,studentname,studentsex,studentbirth,studentadd,studentphone,departmentname FROM student,department WHERE studentid = '" + studentid
				+ "' AND student.studentdepart=department.departmentid";
		idsql.select(query);
		Object[] columnNames = { " ", " " };
		Object[][] rowData = {
				{ "學號", idsql.resultSet.getString("studentid") },
				{ "姓名", idsql.resultSet.getString("studentname") },
				{ "性別", idsql.resultSet.getString("studentsex") },
				{ "出生日期", idsql.resultSet.getString("studentbirth") },
				{ "籍貫", idsql.resultSet.getString("studentadd") },
				{ "所屬學院", idsql.resultSet.getString("departmentname") },
				{ "聯繫方式", idsql.resultSet.getString("studentphone") } };
		DefaultTableModel model = new DefaultTableModel(rowData, columnNames) {
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		table1 = new JTable(model);
		table1.getTableHeader().setReorderingAllowed(false);
		table1.setSelectionBackground(new Color(0, 0, 50));
		table1.setSelectionForeground(Color.WHITE);
		table1.setPreferredScrollableViewportSize(new Dimension(965, 385));// 設置表格的大小
		table1.setRowHeight(30);// 設置每行的高度爲20
		JScrollPane tpane = new JScrollPane(table1);
		tpane.setBounds(5, 5, 965, 385);
		DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 設置table內容居中
		tcr.setHorizontalAlignment(SwingConstants.CENTER);
		table1.setDefaultRenderer(Object.class, tcr);
		panel1.add(tpane);
	}

	public void 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);

		DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 設置table內容居中
		tcr.setHorizontalAlignment(SwingConstants.CENTER);

		SQL stusql = new SQL();
		String test = "SELECT * FROM stu_stc WHERE 學號='" + studentid + "';";
		table2 = new JTable(stusql.getTable(test));
		table2.setModel(stusql.getTable(test));
		table2.setSelectionBackground(new Color(0, 0, 50));
		table2.getTableHeader().setReorderingAllowed(false);
		table2.getTableHeader().setResizingAllowed(false);
		table2.setSelectionForeground(Color.white);
		table2.setDefaultRenderer(Object.class, tcr);
		stusql.shutDown();
		table2.setPreferredScrollableViewportSize(new Dimension(980, 350));// 設置表格的大小
		table2.setRowHeight(30);// 設置每行的高度爲20
		JScrollPane tpane = new JScrollPane(table2);
		tpane.setBounds(5, 40, 965, 370);
		panel2.add(search);
		panel2.add(submit);
		panel2.add(tpane);

	}

	public void 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();
		String text = "SELECT * FROM stu_tc";
		table3 = new JTable(stusql.getTable(text));
		table3.setSelectionBackground(new Color(0, 0, 50));
		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, 200));// 設置表格的大小
		table3.getTableHeader().setReorderingAllowed(false);
		table3.addMouseListener(this);
		table3.setRowHeight(30);// 設置每行的高度爲20
		JScrollPane tpane = new JScrollPane(table3);
		tpane.setBounds(5, 40, 965, 200);

		select = new JButton("選課");
		select.addActionListener(this);
		select.setBounds(870, 250, 100, 30);
		select.setForeground(Color.GRAY);
		select.setBackground(Color.WHITE);
		select.setOpaque(false); // 設置背景透明
		select.setBorder(BorderFactory.createLineBorder(Color.GRAY));
		select.setEnabled(false);
		panel3.add(select);

		// SQL stusql4 = new SQL();
		text = "SELECT * FROM stu_selected WHERE 學號 = '" + studentid + "'";
		// table2 = stusql2.getTable(test2);
		table4 = new JTable(stusql.getTable(text));
		// table2.setModel(stusql2.getTable(text2));
		DefaultTableCellRenderer tcr2 = new DefaultTableCellRenderer();// 設置table內容居中
		tcr2.setHorizontalAlignment(SwingConstants.CENTER);
		table4.setDefaultRenderer(Object.class, tcr2);
		stusql.shutDown();
		table4.setPreferredScrollableViewportSize(new Dimension(980, 120));// 設置表格的大小
		table4.setRowHeight(25);// 設置每行的高度爲20
		JScrollPane tpane2 = new JScrollPane(table4);
		tpane2.setBounds(5, 290, 965, 120);
		panel3.add(submit);
		panel3.add(search);
		panel3.add(tpane);
		panel3.add(tpane2);

	}

	@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("選課")) {
			int row = table3.getSelectedColumn();
			if (row != -1) {
				String courseid = table3.getValueAt(table3.getSelectedRow(), 0)
						.toString();
				String teachername = table3.getValueAt(table3.getSelectedRow(),
						2).toString();
				try {
					selectcourse(courseid, teachername);
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

			}
		}

		if (cmd.equals("退出系統")) {
			Studlg.dispose();
		} else if (cmd.equals("重新登錄")) {
			Studlg.dispose();
			new StuAdmin();
		}
	}

	public void selectcourse(String cno, String tname) throws SQLException {
		SQL selectcourse = new SQL();
		String tno = selectcourse
				.getcell("SELECT teacherid FROM teacher WHERE teachername = '"
						+ tname + "'");
		String query = "INSERT INTO stc VALUES ('" + studentid + "', '" + cno
				+ "', '" + tno + "',null);";
		if (selectcourse.othersql(query) > 0) {
			SQL sql4 = new SQL();
			JOptionPane.showMessageDialog(null, "選課成功");
			table2.setModel(sql4.getTable("SELECT * FROM stu_stc WHERE 學號='"
					+ studentid + "';"));
			table4.setModel(sql4
					.getTable("SELECT * FROM stu_selected WHERE 學號 = '"
							+ studentid + "'"));

		} else
			JOptionPane.showMessageDialog(null, "選課失敗");
		selectcourse.shutDown();
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		select.setForeground(new Color(0, 0, 50));
		select.setBackground(Color.WHITE);
		select.setOpaque(true); // 設置背景透明
		select.setBorder(BorderFactory.createLineBorder(Color.GRAY));
		select.setEnabled(true);
	}

	@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

	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章