大二期末用java代码写一个简单的学生管理系统

本文展示了如何使用Java编写一个简单的学生信息管理系统,包括添加学生信息、按学号查找信息、删除学生信息以及退出功能。代码使用JFrame和ActionListener处理用户交互,通过FileWriter和BufferedReader进行文件操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        展示这个代码最初目的就是在java期末做到这个没能成功写完整,为了弥补遗憾就根据记忆完善一下。经过测试,我写的这个基本功能是能实现的,像:存储学生信息,按学号查找学生信息,按学号查找学生信息并删除(因为学号肯定唯一所以就用学号),以及退出按钮。我直接展示我的代码结构:

Manager类:里面是代码主体
Test类:测试类,调用Manager构造函数即可;

展示大致页面情况:

下面是Manager类的代码

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class Manager extends JFrame implements ActionListener {
    JPanel jPanel[]=new JPanel[5];
    JButton jButton[]=new JButton[4];
    JLabel jLabel[]=new JLabel[4];
    JTextField jTextField[]=new JTextField[4];
    Manager(){
        super("学生管理系统");
        this.setBounds(100,100,500,600);
        this.getContentPane().setLayout(new GridLayout(5,1));
        //创建五个容器对象
        jPanel[0]=new JPanel();
        jPanel[1]=new JPanel();
        jPanel[2]=new JPanel();
        jPanel[3]=new JPanel();
        jPanel[4]=new JPanel();
        //创建四个按钮对象
        jButton[0]=new JButton("保存");
        jButton[1]=new JButton("查找(需要学号)");
        jButton[2]=new JButton("删除");
        jButton[3]=new JButton("退出");
        //创建四个标签对象
        jLabel[0]=new JLabel("姓名:");
        jLabel[1]=new JLabel("学号:");
        jLabel[2]=new JLabel("电话:");
        jLabel[3]=new JLabel("QQ:");
        //创建四个文本框对象
        jTextField[0]=new JTextField(20);
        jTextField[1]=new JTextField(20);
        jTextField[2]=new JTextField(20);
        jTextField[3]=new JTextField(20);
        //将标签和对应的文本框添加到一个容器
        jPanel[0].add(jLabel[0]);
        jPanel[0].add(jTextField[0]);
        jPanel[1].add(jLabel[1]);
        jPanel[1].add(jTextField[1]);
        jPanel[2].add(jLabel[2]);
        jPanel[2].add(jTextField[2]);
        jPanel[3].add(jLabel[3]);
        jPanel[3].add(jTextField[3]);
        jPanel[4].add(jButton[0]);
        jPanel[4].add(jButton[1]);
        jPanel[4].add(jButton[2]);
        jPanel[4].add(jButton[3]);
        //将容器依次添加到顶层容器(顶层容器采用网格布局为简单的五行一列)
        this.getContentPane().add(jPanel[0]);
        this.getContentPane().add(jPanel[1]);
        this.getContentPane().add(jPanel[2]);
        this.getContentPane().add(jPanel[3]);
        this.getContentPane().add(jPanel[4]);
        //对四个按钮添加监听
        jButton[0].addActionListener(this);
        jButton[1].addActionListener(this);
        jButton[2].addActionListener(this);
        jButton[3].addActionListener(this);
        this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==jButton[0]){
            //第一个按钮对应功能是保存,需要用到文件的写入,写入的学生信息分为四行存入,附加解释前缀
            try {
                FileWriter fw = new FileWriter("student.txt",true);
                BufferedWriter bw=new BufferedWriter(fw);
                System.out.println("写入文件中...");
                bw.write("姓名:"+jTextField[0].getText());
                bw.newLine();
                bw.write("学号:"+jTextField[1].getText());
                bw.newLine();
                bw.write("电话:"+jTextField[2].getText());
                bw.newLine();
                bw.write("QQ:"+jTextField[3].getText());
                bw.newLine();
                bw.close();
                fw.close();
                System.out.println("文件写入成功!");
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
        if(e.getSource()==jButton[1]){
            //第二个按钮对应按照学号查找学生信息操作,需要用到文件的读取
            try {
                FileReader fr = new FileReader("student.txt");
                BufferedReader br=new BufferedReader(fr);
                String s[]=new String[10000];
                int i=0;
                boolean flag=false;
                while ((s[i]=br.readLine())!=null) {
                    if(s[i].equals("学号:"+jTextField[1].getText())&&i>0){
                        System.out.println("你所查找的学生信息如下:\n"+s[i-1]+
                                "\n"+s[i]+"\n"+br.readLine()+"\n"
                                +br.readLine()+"\n");
                        i+=2;
                        JFrame show=new JFrame("查找信息");
                        flag=true;
                        break;
                    }
                    i++;
                }
                if(!flag){
                    System.out.println("未找到该学生信息!");
                }
            } catch (FileNotFoundException ex) {
                System.out.println("该文件不存在!请先进行学生信息保存");
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
        if(e.getSource()==jButton[2]){
            //第三个按钮对应按照学号删除学生信息,需要文件的读取和写入操作
            try {
                FileReader fr = new FileReader("student.txt");
                BufferedReader br=new BufferedReader(fr);
                FileWriter fw = new FileWriter("student.txt",true);
                BufferedWriter bw=new BufferedWriter(fw);
                int a=-1;
                String s[]=new String[10000];
                int i=0;
                while ((s[i]=br.readLine())!=null) {
                    if(s[i].equals("学号:"+jTextField[1].getText())&&i>0){
                        a=i-1;
                    }
                    i++;
                }
                //这个地方声明文件输入流为了清空文件信息
                FileWriter fwEvil = new FileWriter("student.txt");
                for(int j=0;j<i;j++){
                    if(j!=a){
                        bw.write(s[j]);
                        bw.newLine();
                    }else {
                        j+=4;
                    }
                }
                if(a!=-1)
                {
                    System.out.println("删除成功!");
                }
                else {
                    System.out.println("该学生信息本就不存在!");
                }
                fwEvil.close();
                br.close();
                bw.close();
                fr.close();
                fw.close();
            } catch (FileNotFoundException ex) {
                System.out.println("文件不存在!请先保存学生信息!");
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }
        if(e.getSource()==jButton[3]){
            //退出
            System.out.println("退出...");
            System.exit(0);
        }
    }
}

下面是Test类代码:

 

public class Test {
    public static void main(String[] args) {
        new Manager();
    }
}

最后也没啥好说的,就再展示一下我的运行成果(编译器用的IDEA) 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值