⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stuframe.java

📁 学生管理系统
💻 JAVA
字号:
package stu;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.table.DefaultTableModel;

public class StuFrame extends JFrame {

	private JTable table;

	private JTextField tfSearch;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private DefaultTableModel model;

	private String SQL = "select * from students";

	private JLabel labelMessage;

	public StuFrame() {
		super();
		setSize(530, 400);
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize = this.getSize();
		setLocation((screenSize.width - frameSize.width) / 2,
				(screenSize.height - frameSize.height) / 2);
		setVisible(true);
		setTitle("高考成绩管理系统");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		final JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		getContentPane().add(panel, BorderLayout.CENTER);

		final JPanel panelToolBar = new JPanel();
		panelToolBar.setLayout(new GridLayout(1, 0));
		panel.add(panelToolBar, BorderLayout.NORTH);

		final JPanel panelTable = new JPanel();
		panelTable.setLayout(new BorderLayout());
		panel.add(panelTable, BorderLayout.CENTER);

		final JScrollPane scrollPane = new JScrollPane();
		panelTable.add(scrollPane, BorderLayout.CENTER);
		// /jtable
		table = new JTable();
		scrollPane.setViewportView(table);
		table.setAutoCreateRowSorter(true);

		model = (DefaultTableModel) table.getModel();

		JMenuBar menubar = new JMenuBar();

		JMenu file = new JMenu("文件管理");

		JMenu edit = new JMenu("编辑修改");

		JMenu view = new JMenu("视图显示");

		JMenu author = new JMenu("作者信息");

		JMenuItem exit = new JMenuItem("退出系统");
		exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateStudent();
				System.exit(0);
			}
		});

		JMenuItem e1 = new JMenuItem("添加信息");
		e1.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				addStudent();
			}
		});

		JMenuItem e2 = new JMenuItem("删除信息");
		e2.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				deleteStudent();
			}
		});

		JMenuItem e3 = new JMenuItem("修改信息");
		e3.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				updateStudent();
			}
		});

		JMenuItem v3 = new JMenuItem("语文成绩分布图");
		v3.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				StatisticDialog dialog = new StatisticDialog("chinese");
				dialog.setTitle("语文成绩分布饼图和分布数据");
				dialog.setBounds(400, 300, 400, 300);
				dialog.setVisible(true);
			}
		});

		JMenuItem v4 = new JMenuItem("数学成绩分布图");
		v4.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				StatisticDialog dialog = new StatisticDialog("math");
				dialog.setTitle("语文成绩分布饼图和分布数据");
				dialog.setBounds(400, 300, 400, 300);
				dialog.setVisible(true);
			}
		});

		JMenuItem v5 = new JMenuItem("英语成绩分布图");
		v5.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				StatisticDialog dialog = new StatisticDialog("english");
				dialog.setTitle("语文成绩分布饼图和分布数据");
				dialog.setBounds(400, 300, 400, 300);
				dialog.setVisible(true);
			}
		});

		JMenuItem v6 = new JMenuItem("综合成绩分布图");
		v6.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent e) {
				StatisticDialog dialog = new StatisticDialog("zonghe");
				dialog.setTitle("语文成绩分布饼图和分布数据");
				dialog.setBounds(400, 300, 400, 300);
				dialog.setVisible(true);
			}
		});
		JMenuItem v7 = new JMenuItem("作者信息");
		v7.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(null,
						"   软件工程05K2\n   尤文超\n   306443000", "作者信息",
						JOptionPane.PLAIN_MESSAGE);
			}
		});

		JButton jButton1 = new JButton(new ImageIcon("images/add.jpg"));
		jButton1.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				addStudent();
			}
		});

		JButton jButton2 = new JButton(new ImageIcon("images/delete.jpg"));
		jButton2.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				deleteStudent();
			}
		});

		JButton jButton5 = new JButton(new ImageIcon("images/modify.jpg"));
		jButton5.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				updateStudent();
			}
		});

		JButton jButton6 = new JButton(new ImageIcon("images/all.jpg"));
		jButton6.addMouseListener(new MouseAdapter() {
			public void mouseClicked(final MouseEvent e) {
				tfSearch.setText("输入学号查询");
				tableDataDisplay(SQL);
			}
		});

		JToolBar toolBar = new JToolBar();
		toolBar.setFloatable(false);
		panelToolBar.add(toolBar);
		toolBar.add(jButton1);
		jButton1.setToolTipText("新增");
		toolBar.add(jButton2);
		jButton2.setToolTipText("删除");
		toolBar.add(jButton5);
		jButton5.setToolTipText("修改记录");
		toolBar.add(jButton6);
		jButton6.setToolTipText("显示全部");

		tfSearch = new JTextField(12);
		tfSearch.setForeground(new Color(0, 128, 0));
		tfSearch.setFont(new Font("", Font.ITALIC, 16));
		panelToolBar.add(tfSearch);
		tfSearch.setText("输入学号查询");
		tfSearch.addFocusListener(new FocusListener() {

			public void focusGained(FocusEvent e) {
				tfSearch.setText("");
			}

			public void focusLost(FocusEvent e) {
				tfSearch.setText("输入学号查询");
			}
		});

		tfSearch.addKeyListener(new KeyAdapter() {
			public void keyReleased(KeyEvent e) {
				tableDataDisplay("select * from students where num like'"
						+ tfSearch.getText().trim() + "%'");
			}
		});
		Vector<String> tableData = new Vector<String>();

		Vector rowData = new Vector();
		getContentPane().add(menubar, BorderLayout.NORTH);
		menubar.setAutoscrolls(true);
		menubar.add(file);
		menubar.add(edit);
		menubar.add(view);
		menubar.add(author);
		file.add(exit);
		edit.add(e1);
		edit.add(e2);
		edit.add(e3);
		view.add(v3);
		view.add(v4);
		view.add(v5);
		view.add(v6);
		author.add(v7);
		// jtable
		tableData.addElement("学号");
		tableData.addElement("姓名");
		tableData.addElement("语文");
		tableData.addElement("数学");
		tableData.addElement("英语");
		tableData.addElement("综合");
		tableData.addElement("文/理科");
		tableData.addElement("平均分");
		tableData.addElement("总分");
		model.setDataVector(rowData, tableData);
		tableDataDisplay(SQL);

		labelMessage = new JLabel("");
		panel.add(labelMessage, BorderLayout.SOUTH);
		repaint();
	}

	protected void deleteStudent() {
		try {
			int[] tablerow = table.getSelectedRows();
			for (int i = 0; i < tablerow.length; i++) {
				if (tablerow[i] >= 0) {
					String str = String.valueOf(table
							.getValueAt(tablerow[i], 0));
					ServiceFactory.getService().executeUpdate(
							"delete from students where num='" + str + "'");

				} else
					labelMessage.setText("没有选中选项!");
			}

			labelMessage.setText("删除成功!");
			tableDataDisplay(SQL);
		} catch (SQLException ee) {
			System.out.println("SQLException:" + ee.getMessage());
		}
	}

	protected void updateStudent() {
		try {
			int tablerow = table.getRowCount();
			if (0 < tablerow) {
				for (int i = 0; i < tablerow; i++) {
					String str = String.valueOf(table.getValueAt(i, 0));
					String str1 = String.valueOf(table.getValueAt(i, 1));
					String str2 = String.valueOf(table.getValueAt(i, 2));
					String str3 = String.valueOf(table.getValueAt(i, 3));
					String str4 = String.valueOf(table.getValueAt(i, 4));
					String str5 = String.valueOf(table.getValueAt(i, 5));
					String str6 = String.valueOf(table.getValueAt(i, 6));
					String str7 = (str6.equals("理科")) ? "l" : "w";
					float f01 = Float.parseFloat(str2);
					float f02 = Float.parseFloat(str3);
					float f03 = Float.parseFloat(str4);
					float f04 = Float.parseFloat(str5);
					ServiceFactory.getService().executeUpdate(
							"update students set name='" + str1 + "',chinese='"
									+ f01 + "',math='" + f02 + "',english='"
									+ f03 + "',zonghe='" + f04 + "',likefou='"
									+ str7 + "' where num='" + str + "'");

				}
				labelMessage.setText("修改成功!");
				tableDataDisplay(SQL);
			} else {

			}
		} catch (SQLException ee) {
			System.out.println("SQLException:" + ee.getMessage());
		}
	}

	protected void addStudent() {
		final AddStuDialog dialog = new AddStuDialog();
		dialog.setBounds(260, 200, 300, 200);
		dialog.setVisible(true);
		new Thread() {
			public void run() {
				while (dialog.isShowing()) {
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {

					}
				}

				tableDataDisplay(SQL);
			}
		}.start();

	}

	public void tableDataDisplay(String sql) {
		try {
			ResultSet rs = null;
			rs = ServiceFactory.getService().executeQuery(sql);
			int count = table.getRowCount();
			for (int i = 0; i < count; i++)
				model.removeRow(0);
			while (rs.next()) {
				String n_1 = rs.getString("num").trim();
				String n_2 = rs.getString("name").trim();
				String n_3 = rs.getString("chinese").trim();
				String n_4 = rs.getString("math").trim();
				String n_5 = rs.getString("english").trim();
				String n_6 = rs.getString("zonghe").trim();
				String n_7 = ("w".equals(rs.getString("likefou").trim())) ? "文科"
						: "理科";

				float f01 = Float.parseFloat(n_3);
				float f02 = Float.parseFloat(n_4);
				float f03 = Float.parseFloat(n_5);
				float f04 = Float.parseFloat(n_6);
				float f05 = f01 + f02 + f03 + f04;
				String n_8 = String.valueOf(f05 / 4);

				String n_9 = String.valueOf(f05);
				String row[] = { n_1, n_2, n_3, n_4, n_5, n_6, n_7, n_8, n_9 };
				// String row[] = { "n_1", "n_2", "n_3", "n_4", "", "n_6","
				// n_7", "n_8","n_9" };
				model.addRow(row);
			}
		} catch (SQLException e) {
			System.out.println("SQLException:" + e.getMessage());
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -