java学生管理系统(简易)(七)---修改学生(教师端)

时间:2023-01-27 18:46:44

调用StudentTools的方法,其余为检错代码

 
package StudentMajor;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Set;
import java.util.TreeSet;

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.JTextField;

public class ReviseStudent{

	private JFrame jf;//窗口
	private JPanel jp1,jp2,jp3,jp4,jp5,jp6,jp7;//面板
	private JLabel jlb1,jlb2,jlb3,jlb4,jlb5,jlb6;//标签
	private JButton jb1,jb2,jb3;//按钮
	private JTextField jtf1,jtf2,jtf3,jtf4,jtf5,jtf6;//文本
	public static Set<StudentUser> set = new TreeSet<StudentUser>();
	
	
	private  File file = new File("e:\\abc\\file\\StudentInfo.txt");

	public ReviseStudent() {
		
	}
	
	public void init() {
		if(!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//<1>设置主页面
		jf = new JFrame("学生管理系统");
		jf.setSize(300, 300);//窗体大小
		jf.setLocationRelativeTo(null);//居中显示
		jf.setLayout(new FlowLayout());//设置布局
		jf.setResizable(false); //设置窗口不能改变大小
		
		
//		jf = new JFrame("学生管理系统");
//		jf.setBounds(300,300, 300, 300);
//		jf.setLocationRelativeTo(null);
//		//jf.setLayout(new FlowLayout());
//		jf.setResizable(false);
		
		
		
		//一初始化就有面板
		jp1 = new JPanel();
		jp2 = new JPanel();
		jp3 = new JPanel();
		jp4 = new JPanel();
		jp5 = new JPanel();
		jp6 = new JPanel();
		//(1)设置标签
		jlb1 = new JLabel("姓   名");
		jlb2 = new JLabel("学   号");
		jlb3 = new JLabel("数   学");
		jlb4 = new JLabel("语   文");
		jlb5 = new JLabel("英   语");
		//(2)设置按钮
		jb1 = new JButton("确定");
		jb2 = new JButton("重置");
		jb3 = new JButton("返回");
		
		//(3)创建文本框
		jtf1 = new JTextField(10);
		jtf2 = new JTextField(10);
		jtf3 = new JTextField(10);
		jtf4 = new JTextField(10);
		jtf5 = new JTextField(10);
		
		//展示学生
		//JScrollPane jsp = new SearchStudents().jspOfAllStudents();
		
		
		//<2>添加部件(添加到面板)
		
		jp1.add(jlb1);
		jp1.add(jtf1);
		
		jp2.add(jlb2);
		jp2.add(jtf2);
		
		jp3.add(jlb3);
		jp3.add(jtf3);
		
		jp4.add(jlb4);
		jp4.add(jtf4);
		
		jp5.add(jlb5);
		jp5.add(jtf5);
		
		jp6.add(jb1);
		jp6.add(jb2);
		jp6.add(jb3);
		
		//(1)将组件都添加到窗体上
		//jf.add(jsp);
		jf.add(jp1);
		jf.add(jp2);
		jf.add(jp3);
		jf.add(jp4);
		jf.add(jp5);
		jf.add(jp6);
		//<3>添加窗体事件
		myEvent();
		
		//<4>设置窗体可见
		jf.setVisible(true);
		//jf.pack();
	}
	
	public static void main(String[] args) {
		new ReviseStudent().init();
	}
	
	
	
	// 基本事件
	public void myEvent() {
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jb1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(jtf1.getText().isEmpty()||jtf2.getText().isEmpty()||jtf3.getText().isEmpty()||
						jtf4.getText().isEmpty()||jtf5.getText().isEmpty()) {
					JOptionPane.showMessageDialog(null,"请输入完整的信息" ,"提示信息", JOptionPane.WARNING_MESSAGE);
					return;
				}else{
					String s1 = jtf1.getText();
					String s1Reg = ".{2,5}";
					if(!s1.matches(s1Reg)) {
						JOptionPane.showMessageDialog(null,"姓名最多5位,最少两位哦" ,"提示信息", JOptionPane.WARNING_MESSAGE);
						return;
					}
					String s2 = jtf2.getText();
					String s2Reg = "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]";
					if(!s2.matches(s2Reg)) {
						JOptionPane.showMessageDialog(null,"学号只能是八位纯数字哦" ,"提示信息", JOptionPane.WARNING_MESSAGE);
						return;
					}
					String s3 = jtf3.getText();
					String s4 = jtf4.getText();
					String s5 = jtf5.getText();
					String jtfReg = "[0-9][0-9]?";
					if(!(s3.matches(jtfReg)&&s4.matches(jtfReg)&&s5.matches(jtfReg))) {
						JOptionPane.showMessageDialog(null,"成绩有不合法输入(0-99分)" ,"提示信息", JOptionPane.WARNING_MESSAGE);
						return;
					}
					
				}
				int a = Integer.parseInt(jtf3.getText());
				int b = Integer.parseInt(jtf4.getText());
				int c = Integer.parseInt(jtf5.getText());
				boolean flag = false;
				flag = StudentTools.deleteStudent(jtf2.getText());
				StudentUser stu = new StudentUser(jtf1.getText(),jtf2.getText(),a,b,c);
				stu.setSum(a+b+c);	
				StudentTools.set.add(stu);
				//更新文件
				if(flag) {
					Set<StudentUser> set = StudentTools.set;
					if(file.exists()) {
						file.delete();
						
					 try {
						file.createNewFile();
						StudentTools.writeToStuFile(file,set);
					} catch (IOException e1) {
		
						e1.printStackTrace();
					}
					 
					 JOptionPane.showMessageDialog(null, "修改成功", "提示信息", JOptionPane.WARNING_MESSAGE); 
				  }else {
					  
					  JOptionPane.showMessageDialog(null, "目前还没有学生", "提示信息", JOptionPane.WARNING_MESSAGE);
				  }
					
					
				}else {
					JOptionPane.showMessageDialog(null, "没有此学号的学生", "提示信息", JOptionPane.WARNING_MESSAGE);
				}
			}
	
		});
		jb2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				jtf1.setText("");
				jtf2.setText("");
				jtf3.setText("");
				jtf4.setText("");
				jtf5.setText("");
				
			}

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

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