欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

5

程序员文章站 2024-03-20 09:20:28
...
                                          **学生管理系统二**

 package com.imau.view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

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

import com.imau.base.BaseFraem;
import com.imau.controller.StudentController;
   import com.imau.dao.student.StudentDaoImpl;
import com.imau.entry.PageEntity;
import com.imau.entry.Student;

public class StudentListView extends BaseFraem {

private StudentController controller;
private PageEntity<Student> pageEntity;
// 组件
// 第一部分
// 搜索框
private JTextField tsearch;
// 搜索按钮
private JButton searchBtn;
private JComboBox<String> searchBox;

private String item[] = { "姓名", "学号", "年龄", "性别" };
// 添加学生按钮
private JButton addStu;
private JButton updateStu;
private JButton delStu;
// 第二部分
// 学生列表
private JTable table;
private String columnNames[] = { "序号", "ID", "姓名", "学号", "年龄", "性别", "民族" };
private Object rowData[][];
private JScrollPane scrollPane;
DefaultTableModel tableModel;
// 第三部分
// 首页 上一页 下一页 尾页 跳转 按钮
private JButton homeBtn, upBtn, nextBtn, endBtn, jumpBtn;
// 跳转输入框
private JTextField tjump;
// 显示页面基本信息
private JLabel lpage;
// 装载每一部分的组件
private JPanel panel, panel3;

public StudentListView() {
	controller = new StudentController();
	pageEntity = new PageEntity<>(new StudentDaoImpl());
	initListData();
	this.setJMenuBar(initMenu());
	init();
	setFraem();
}

// 初始表格数据
private void initListData() {

	List<Student> list = pageEntity.getPageData();
	rowData = new Object[list.size()][7];
	for (int i = 0; i < list.size(); i++) {
		 
		rowData[i][0] = i + 1;
		rowData[i][1] = list.get(i).getId();
		rowData[i][2] = list.get(i).getName();
		rowData[i][3] = list.get(i).getNo();
		rowData[i][4] = list.get(i).getAge();
		rowData[i][5] = list.get(i).getSex();
		rowData[i][6] = list.get(i).getNation();
	}

}

// 参数:数据列表
private void UpTabltUI(List<Student> list) {

	// 2.数据清空
	tableModel.setRowCount(0);
	// 3.重新添加数据
	for (int i = 0; i < list.size(); ++i) {

		Object data[] = new Object[tableModel.getColumnCount()];

		data[0] = i + 1;
		data[1] = list.get(i).getId();
		data[2] = list.get(i).getName();
		data[3] = list.get(i).getNo();
		data[4] = list.get(i).getAge();
		data[5] = list.get(i).getSex();
		data[6] = list.get(i).getNation();

		tableModel.addRow(data);
	}

	

	String info = "共" + pageEntity.getTotalCount() + "条 " + pageEntity.getPageNo() + "/"
			+ pageEntity.getTotalPage();
	lpage.setText(info);
	
	// 4.刷新
	// 数据更新
	table.invalidate();

	// 数据更新
	// tableModel.fireTableDataChanged();

}

private void initEnvent() {

	searchBtn.addActionListener(new MyListener2());
	addStu.addActionListener(new MyListener2());
	updateStu.addActionListener(new MyListener2());
	delStu.addActionListener(new MyListener2());

	homeBtn.addActionListener(new MyListener2());
	upBtn.addActionListener(new MyListener2());
	nextBtn.addActionListener(new MyListener2());
	endBtn.addActionListener(new MyListener2());
	jumpBtn.addActionListener(new MyListener2());
}

private void init() {

	tsearch = new JTextField(10);
	searchBtn = new JButton("搜索");
	searchBox = new JComboBox<>(item);

	addStu = new JButton("添加");
	updateStu = new JButton("修改");
	delStu = new JButton("删除");

	tableModel = new DefaultTableModel(rowData, columnNames);
	table = new JTable(tableModel);
	table.setRowHeight(30);
	
	 
	scrollPane = new JScrollPane(table);

	homeBtn = new JButton("首页");
	upBtn = new JButton("上一页");
	nextBtn = new JButton("下一页");
	endBtn = new JButton("尾页");
	jumpBtn = new JButton("跳转");
	tjump = new JTextField(5);

	String info = "共" + pageEntity.getTotalCount() + "条 " + pageEntity.getPageNo() + "/"
			+ pageEntity.getTotalPage();

	lpage = new JLabel(info);

	panel = new JPanel();
	panel3 = new JPanel();

	panel.add(searchBox);
	panel.add(tsearch);
	panel.add(searchBtn);
	panel.add(addStu);
	panel.add(updateStu);
	panel.add(delStu);

	panel3.add(lpage);
	panel3.add(homeBtn);
	panel3.add(upBtn);
	panel3.add(nextBtn);
	panel3.add(endBtn);
	panel3.add(tjump);
	panel3.add(jumpBtn);

	this.add(panel, BorderLayout.NORTH);
	this.add(scrollPane, BorderLayout.CENTER);
	this.add(panel3, BorderLayout.SOUTH);
	this.pack();
	initEnvent();
}

class MyListener2 implements ActionListener {

	@Override
	public void actionPerformed(ActionEvent e) {

		// 添加学生
		if (e.getSource() == addStu) {

			new StudentInfoView(null);
			dispose();
		}

		// 修改学生

		if (e.getSource() == updateStu) {

			// 1.选择行
			int row = table.getSelectedRow();

			if (row != -1) {

				// 2.获取选择行ID的列值
				long id = (Long) table.getValueAt(row, 1);
				// 3.查询对象
				Student student = controller.findStudent(id);
				// 4.传入到修改页面

				new StudentInfoView(student);
				dispose();
			} else {

			}

		}

		// 删除学生

		if (e.getSource() == delStu) {

			// 1.选择行
			int row = table.getSelectedRow();

			if (row != -1) {

				
				 
				
				// 2.获取选择行ID的列值
				long id = (Long) table.getValueAt(row, 1);
				// 3.删除
				if(controller.delStudent(id)) {
					
					JOptionPane.showMessageDialog(null, "删除成功");
					pageEntity.init();
					UpTabltUI(pageEntity.homePage());
				}else {
					JOptionPane.showMessageDialog(null, "删除失败");
				}
				
			} else {

				JOptionPane.showMessageDialog(null, "请选择数据");
			}
		}

		// 页面导航
		if (e.getSource() == homeBtn) {

			List<Student> list=pageEntity.homePage();
			UpTabltUI(list);
		}

		if (e.getSource() == nextBtn) {
			List<Student> list=pageEntity.nextPage();
			UpTabltUI(list);
		}

		
		if (e.getSource() == upBtn) {
			List<Student> list=pageEntity.previouPage();
			UpTabltUI(list);
		}
		
		if (e.getSource() == endBtn) {
			List<Student> list=pageEntity.endPage();
			UpTabltUI(list);
		}
		
		if (e.getSource() == jumpBtn) {
			
			String pageNo=tjump.getText();
			if(pageNo!=null&&!pageNo.equals("")) 
			{
				List<Student> list=pageEntity.getListDataByPageNo(Integer.valueOf(pageNo));
				UpTabltUI(list);
			}else {
				
				
			}
			
			
		}
		
		if(e.getSource()==searchBtn) {
			//选择字段
			String item=(String) searchBox.getSelectedItem();
		    String field;
			Object value =tsearch.getText();
			if(item.equals("姓名")) {
				field="name";
				
			}else if(item.equals("学号")) {
				field="no";
				
			}else if(item.equals("年龄")) {
				field="age";
			}else {
				field="sex";
			}
			
		   List<Student> list=controller.search(field, value);
		
		   UpTabltUI(list);
	       	}
	  }

   }

 }