java学生管理系统(简易)(五)---查询学生(教师端)

查询界面,因为还不会数据库,嘤嘤嘤~就只能先这样写了

package StudentMajor;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Set;

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;

public class SearchStudents {
	
	private JFrame jf;
	private JPanel jp,jp2,jp3;
	private JTextField jtf;
	private JButton jb,jb2,jb3;
	private JLabel jl;
	private JTable jtable;
	private JScrollPane jsp1,jsp2;
	private int countAStudentShow,countAllStudentShow;
	//用来计数查找名字显示或者不显示此处长度应该以学生的数量为准
	private Object[][] tableData = new Object[getFilelength()][];
	// // 定义一维数据作为列标题
	private final Object[] columnTitle = {"姓名", "学号" , "数学","语文","英语","总分"};
	SearchStudents(){
		
	}
	//返回
	public static int getFilelength() {
		return StudentTools.getStudentsFromFile().toArray().length;
		  
	}
	public void setTable() {
		// 定义二维数组作为表格数据
		  for(int i=0;i< getFilelength();i++) {
			  Object[] obj = new Object[]{"","","","","",""}; 
			  tableData[i]=obj;
		  }
	  }
	  
	public JScrollPane jspOfAllStudents() {
		//初始化表格的行列数
		 setTable();
		 //给表格中填入数据
		 Set<StudentUser> set = StudentTools.getStudentsFromFile();
		 if(set==null)
			 return null;
		 Object[] obj = set.toArray();
		// System.out.println(obj.length);
		 for(int i=0;i<obj.length;i++) {
			  StudentUser s = (StudentUser)obj[i];
			//  System.out.println(s.getName());
			  tableData[i][0] = s.getName();
			  tableData[i][1] = s.getNum();
			  tableData[i][2] = s.getScore1();
			  tableData[i][3] = s.getScore2();
			  tableData[i][4] = s.getScore3();
			  tableData[i][5] = s.getSum();
		 }
		 
		 jtable = new JTable(tableData,columnTitle);
		 jtable.enable(false);//让这个表格不可编辑
		 return new JScrollPane(jtable);
		
	}
	
	public JScrollPane jspOfAStudent(StudentUser s) {
		if(s==null)
			return null;
		// 定义二维数组作为表格数据
		 Object[][] tableData =
		 {
		   new Object[]{"","","","","",""},
		 };
		 // 定义一维数据作为列标题
		 Object[] columnTitle = {"姓名", "学号" , "数学","语文","英语","总分"};
		 
		 //给表格中填入数据
		
		  tableData[0][0] = s.getName();
		  tableData[0][1] = s.getNum();
		  tableData[0][2] = s.getScore1();
		  tableData[0][3] = s.getScore2();
		  tableData[0][4] = s.getScore3();
		  tableData[0][5] = s.getSum();
		 
		 
		 jtable = new JTable(tableData,columnTitle);
		 jtable.enable(false);//让这个表格不可编辑
		 return new JScrollPane(jtable);
		
	}
	
	
	
	
	
	
	public void init() {
	
		jf = new JFrame("学生管理系统");
		jf.setSize(300, 250);//窗体大小
		jf.setLocationRelativeTo(null);//居中显示
		jf.setLayout(new FlowLayout());//设置布局
		jf.setResizable(false); //设置窗口不能改变大小
		
		jp = new JPanel();
		jp2 = new JPanel();
		jp3 = new JPanel();
		jl = new JLabel("请输入学号:");
		jb = new JButton("查询");
		jb2 = new JButton("查询全部");
		jb3 = new JButton("返回");
		jtf = new JTextField(10);
		
		jp.add(jl);
		jp.add(jtf);
		jp.add(jb);
		
		jp2.add(jb2);

		jp3.add(jb3);
		
		
		jf.add(jp);
		jf.add(jp2);
		jf.add(jp3);
		//jf.add(jsp());
		//jf.pack();
		
		myEvent();
		jf.setVisible(true);
		
	}
	public void myEvent() {
		
		//(1)窗体关闭
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//(2)监听---确定
		jb.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
		
				if(countAStudentShow==0) {
					StudentUser stu = StudentTools.showAStudentToPanel(jtf.getText());
					if(stu==null)
						JOptionPane.showMessageDialog(null, "没有该学号的学生!", "提示信息", JOptionPane.WARNING_MESSAGE);
					else{
						jsp1 = jspOfAStudent(stu);
						jf.add(jsp1);
						jf.pack();
						countAStudentShow++;
					}
					
				}else {
					jf.remove(jsp1);
					StudentUser stu = StudentTools.showAStudentToPanel(jtf.getText());
					if(stu==null)
						JOptionPane.showMessageDialog(null, "没有该学号的学生!", "提示信息", JOptionPane.WARNING_MESSAGE);
					else {
						jsp1 = jspOfAStudent(stu);
						jf.add(jsp1);
						jf.pack();
					}
					
				}

			}
				
		});
		jb2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				
				if(countAllStudentShow==0) {
					if(jspOfAllStudents()==null)
						JOptionPane.showMessageDialog(null, "还没有学生!", "提示信息", JOptionPane.WARNING_MESSAGE);
					else{
						jsp2 = jspOfAllStudents();
						jf.add(jsp2);
						jf.pack();
						countAllStudentShow++;
					}
					
				}else {
					jf.remove(jsp2);
					jsp2 = jspOfAllStudents();
					jf.add(jsp2);
					jf.pack();
					
					
				}

			}
				
		});
		jb3.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				new TeacherUser().init();
				jf.dispose();
				
				
			}
		});
		
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值