学生信息管理系统(swing+文件操作) java作业

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/sdz20172133/article/details/84113060

学生信息管理系统

功能运行界面

(1)读取文件

(2) 添加记录

(3) 查找记录(按学号查找)

(4) 修改记录(按学号修改)

 

 

(5) 删除记录(按学号删除)

(6) 记录按成绩升序排列

(6) 记录按成绩升序排列

(7) 保存文件

 

代码实现:

主测试类

package java作业;


public class Main {
		public static void main(String[] args) {
			new Frame();
		}
}

界面设置类

package java作业;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;


class Frame extends JFrame implements ActionListener {
	
	StudentGroup studentgroup=new StudentGroup();
	final JTextArea textArea = new JTextArea("序号      学号            姓名           成绩\n",10,42);
	final JTextField IDTextField=new JTextField();
	final JTextField nameTextField=new JTextField();
	final JTextField gradeTextField=new JTextField();
	
	Frame() {

		Font f=new Font("黑体",Font.BOLD, 12);
	   /************************窗口*********************/
		setTitle("学生信息管理系统");
		setVisible(true);
		setLayout(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setBounds(100, 100, 520, 350);
		
		/************************显示信息面板*********************/
		final JLabel title = new JLabel("学生信息");
		title.setFont(f);
		title.setBounds(115, 10, 60, 15);
		add(title);
		
		final JScrollPane jscrollPane = new JScrollPane(textArea);
		jscrollPane.setBounds(20, 30, 240, 250);
		jscrollPane.setHorizontalScrollBarPolicy(31);
		//jscrollPane.setWheelScrollingEnabled(true);
		add(jscrollPane);
		
		/************************输入信息面板*********************/
		final JPanel inputPanel = new JPanel(new GridLayout(3,2,10,10));
		final JLabel numberTitle = new JLabel("学号:",4);
		final JLabel nameTitle = new JLabel("姓名:",4);
		final JLabel gradeTitle = new JLabel("成绩:",4);
		
		inputPanel.add(numberTitle);
		inputPanel.add(IDTextField);
		inputPanel.add(nameTitle);
		inputPanel.add(nameTextField);
		inputPanel.add(gradeTitle);
		inputPanel.add(gradeTextField);
		inputPanel.setBounds(180, 30, 300, 100);
		add(inputPanel);
		
		/************************功能按钮面板*********************/
		final JPanel buttonPanel = new JPanel();
		final GridLayout gridLayout = new GridLayout(4,2,4,4);
		buttonPanel.setLayout(gridLayout);
		String[] b = {"添加", "修改","查找", "删除", "升序排序", "降序排序", "读取文件", "保存退出"};
		JButton[] button = new JButton[8];
		for (int i=0; i<8; i++)
		{
			button[i] = new JButton(b[i]);
			buttonPanel.add(button[i]);
			button[i].addActionListener(this);
			button[i].setFont(f);
		}
		buttonPanel.setBounds(300, 140, 180, 130);
		add(buttonPanel);
		
		setResizable(false);
		setVisible(true);
	}
		
	@Override
	public void actionPerformed(ActionEvent e) {

    String op=e.getActionCommand();
    switch(op)
    {
    case"添加":{
    	String s1=IDTextField.getText();
    	String s2=nameTextField.getText();
    	String s3=gradeTextField.getText();
    	
    	if(studentgroup.add(new Student(s1,s2,s3))&&s1.length()>0)
    	{
    		textArea.setText("序号      学号            姓名           成绩\n"+studentgroup.getall());
    		JOptionPane.showMessageDialog(this, "添加成功!", "提示", DISPOSE_ON_CLOSE);
    	}
    	else
    	{
    		JOptionPane.showMessageDialog(this, "学生学号已经存在,添加失败!", "提示", DISPOSE_ON_CLOSE);
    	}
    }break;
    
    case"修改":{
    	String s1=IDTextField.getText();
    	String s2=nameTextField.getText();
    	String s3=gradeTextField.getText();
    	if(studentgroup.change(new Student(s1,s2,s3)))
    	{
    		textArea.setText("序号      学号            姓名           成绩\n"+studentgroup.getall());
    		JOptionPane.showMessageDialog(this, "修改成功!", "提示", DISPOSE_ON_CLOSE);
    	}
    	else
    	{
    		JOptionPane.showMessageDialog(this, "学生学号不存在,修改失败!", "提示", DISPOSE_ON_CLOSE);
    	}
    	
    }break;
    
    case"删除":{
    	String s1=IDTextField.getText();
    	if(studentgroup.delete(s1))
    	{
    		textArea.setText("序号      学号            姓名           成绩\n"+studentgroup.getall());
    		JOptionPane.showMessageDialog(this, "删除成功!", "提示", DISPOSE_ON_CLOSE);
    	}
    	else
    	{
    		JOptionPane.showMessageDialog(this, "学生学号不存在,删除失败!", "提示", DISPOSE_ON_CLOSE);
    	}
    	
    }break;
    case"查找":{
    	String s1=IDTextField.getText();
    	int pos=studentgroup.find(s1);
    	if(pos!=-1)
    	{
    		IDTextField.setText(studentgroup.list.get(pos).getID());
    		nameTextField.setText(studentgroup.list.get(pos).getname());
    		gradeTextField.setText(studentgroup.list.get(pos).getgrade());
    		JOptionPane.showMessageDialog(this, "已显示在查询栏", "提示", DISPOSE_ON_CLOSE);
    	}
    	else
    	{
    		JOptionPane.showMessageDialog(this, "学生学号不存在,查询失败!", "提示", DISPOSE_ON_CLOSE);
    	}
    	
    }break;
    case"降序排序":{
    	if(studentgroup.size()!=0)
    	{
    		studentgroup.Descending_order();
    		textArea.setText("序号      学号            姓名           成绩\n"+studentgroup.getall());
    	}
    	else
    	{
    		JOptionPane.showMessageDialog(this, "无学生信息!", "提示", DISPOSE_ON_CLOSE);
    	}
    	
    }break;
    case"升序排序":{
    	if(studentgroup.size()!=0)
    	{
    		studentgroup.Ascending_order();
    		textArea.setText("序号      学号            姓名           成绩\n"+studentgroup.getall());
    	}
    	else
    	{
    		JOptionPane.showMessageDialog(this, "无学生信息!", "提示", DISPOSE_ON_CLOSE);
    	}
    }break;
    case"读取文件":{
    	if(studentgroup.readfile())
    	{
    		textArea.setText("序号      学号            姓名           成绩\n"+studentgroup.getall());
    		JOptionPane.showMessageDialog(this, "读取文件成功!", "提示", DISPOSE_ON_CLOSE);
    	}
    	else
    	{
    		JOptionPane.showMessageDialog(this, "读取文件失败!", "提示", DISPOSE_ON_CLOSE);
    	}
    	
    }break;
    case"保存退出":{
    	if(studentgroup.writefile())
    	{
    		JOptionPane.showMessageDialog(this, "保存文件成功,退出系统!", "提示", DISPOSE_ON_CLOSE);
    		dispose();
    	}
    	else
    	{
    		JOptionPane.showMessageDialog(this, "保存文件失败!", "提示", DISPOSE_ON_CLOSE);
    	}
    	
    }break;
    	
    }
    
    }
}

学生类

package java作业;

class Student
{
	
	private  String ID,name,grade;
	public Student() {}
	public Student(String ID,String name,String grade)
	{
		super();
		this.ID=ID;
		this.name=name;
		this.grade=grade;
	}
	public String getID()
	{
		return this.ID;
	}
	public String getname()
	{
		return this.name;
	}
	public String getgrade()
	{
		return this.grade;
	}
	public void changename(String name)
	{
		this.name=name;
	}
	public void changegrade(String grade)
	{
		this.grade=grade;
	}
	public String toString()
	{
		return ID+"       "+name+"           "+grade;
	}
	public String fileString()
	{
		return ID+" "+name+" "+grade;
	}
	
}

学生集合类

package java作业;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Stream;

class StudentGroup {
	
	ArrayList<Student> list = new ArrayList<Student>();
	
	public StudentGroup()           //读文件
	{
		this.readfile();
	}
	public int size()       //返回学生数量
	{
		return list.size();
	}
	public int find(String str) //根据学号查找学生信息,找到返回学号,找不到返回-1
	{
		System.out.println(str);
		for (int i = 0; i < list.size(); i++)
			if (list.get(i).getID().equals(str))
				return i;
		return -1;
	}
	public String getall()	 //获取全部学生记信息
	{
		String str="";
	    for(int i=0;i<list.size();i++)
	    {
	    	str+=(i+1)+"       "+list.get(i).toString()+"\n";	   
	    }
	    //System.out.println(x);
		return str;
	}
	public boolean add(Student stu)	//添加学生记信息
	{
		if (find(stu.getID())!=-1)
			return false;
		
		list.add(stu);    
		return true;
	}
	public boolean change(Student stu)	//添加学生记信息
	{  
		int pos=find(stu.getID());
		if (pos==-1)
			return false;
		if(stu.getname().length()==0)
		{
			stu.changename(list.get(pos).getname());
		}
		if(stu.getgrade().length()==0)
		{
			stu.changegrade(list.get(pos).getgrade());
		}

		list.set(pos,stu);    
		return true;
	}
	public boolean delete(String s)	//添加学生记信息
	{  
		int pos=find(s);
		if (pos==-1)
			return false;
		
		list.remove(pos);    
		return true;
	}
	public void Descending_order()       //降序排序
	{
		Collections.sort(list, new Comparator<Student>(){
			public int compare(Student stu1, Student stu2) {			
				return Integer.parseInt(stu2.getgrade())-Integer.parseInt(stu1.getgrade());
			}  	
	    });
	}
	public void Ascending_order()       //降序排序
	{
		Collections.sort(list, new Comparator<Student>(){
			public int compare(Student stu1, Student stu2) {			
				return Integer.parseInt(stu1.getgrade())-Integer.parseInt(stu2.getgrade());
			}  	
	    });
	}
	public boolean readfile() {
			 
		      String t=null;
			  try{
				 FileReader f1 = new FileReader("G:\\student.txt");
			     BufferedReader br=new BufferedReader(f1);
			     list.clear();
				while ((t= br.readLine())!= null)
				{
				 String [] s=t.split(" ");
				 Student st=new Student(s[0],s[1],s[2]);
			     list.add(st);
				}
		        f1.close();
		        br.close();
		        return true;
				} catch (IOException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
					return false;
				}
		
     }
	
	public boolean writefile() {
		FileWriter fw=null;
		BufferedWriter out=null;
		try {
			 fw = new FileWriter("G:\\student.txt");
			 out = new BufferedWriter(fw);
				for(int i=0;i<list.size();i++){
					String s=list.get(i).fileString();
			    	out.write(s);
			    	out.newLine();
			    }
				out.close();
				fw.close();
				return true;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
   }
}

 

展开阅读全文

没有更多推荐了,返回首页