学生管理系统(Java)

这是一个关于学生信息管理系统的Java实现,包括登录验证、主页面操作如增加、删除、修改和查看学生数据,以及使用哈希表存储和文件序列化进行数据持久化。系统实现了基本的数据操作功能,并通过对话框提示用户操作结果。
摘要由CSDN通过智能技术生成

项目介绍

主要分为三个部分,登录,主页,信息。

代码

登录

package stndent;

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

class LoginCheck{
    private String name;
    private String password;
    public LoginCheck(String name,String password){
        this.name=name;
        this.password=password;
    }
    public boolean equals(){
        if("root".equals(name)&&"123456".equals(password)){
            return true;
        }else{
            return false;
        }
    }
};
class ActionHandle{
    private JFrame frame=new JFrame("学生信息管理系统");
    private JTextField name=new JTextField();//设置文本框
    private JPasswordField pass=new JPasswordField();
    private JLabel but1=new JLabel("用户名:");
    private JLabel but2=new JLabel("密   码:");
    private JButton but3=new JButton("登录");
    private JButton but4=new JButton("重置");

    public ActionHandle(){
        but3.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                if(e.getSource()==but3){
                    String sname=name.getText();
                    String spass=new String(pass.getPassword());
                    LoginCheck log=new LoginCheck(sname,spass);
                    if(log.equals()){
                        try {
                            new Menu();
                        } catch (Exception e1) {

                            e1.printStackTrace();
                        }
                        frame.setVisible(false);

                    }else{
                        JOptionPane.showMessageDialog(null, "登录失败,错误的用户名或密码!");
                    }
                }
            }
        });
        but4.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        if(e.getSource()==but4){
                            name.setText("");
                            pass.setText("");
                        }
                    }

                });
        frame.setLayout(null);
        but1.setBounds(80, 40 , 80,30);
        name.setBounds(140,42, 120, 25);    //
        but2.setBounds(80, 80 , 80,30);
        pass.setBounds(140,82, 120, 25);
        but3.setBounds(130, 150 , 60,30);
        but4.setBounds(210, 150 , 60,30);
        frame.setSize(400,330);
        frame.setLocation(300, 200);
        frame.add(but1);
        frame.add(name);
        frame.add(pass);
        frame.add(but2);
        frame.add(but3);
        frame.add(but4);
        frame.setVisible(true);
    }
}
class  Enter{
    public static void main(String[] args) {

        new ActionHandle();
    }

}

主页面

package student;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
public class Menu {
    private JButton but1 = new JButton("增加数据"); // 按钮
    private JButton but2 = new JButton("删除数据");
    private JButton but3 = new JButton("修改数据");
    private JButton but4 = new JButton("查看数据");
    private JButton but0 = new JButton("退出系统");
    private JButton but5 = new JButton("显示");
    private JButton clear = new JButton("清空");
    private JTextField number = new JTextField();// 文本框
    private JTextField name = new JTextField();
    private JTextField dor = new JTextField();
    private JTextField address = new JTextField();
    private JTextField sex = new JTextField();
    private JTextField date = new JTextField();
    private JTextField pol = new JTextField();
    private JTextField phone = new JTextField();

    private JTextArea show = new JTextArea(16, 30);
    private JLabel lab1 = new JLabel("姓名:");// 标签
    private JLabel lab2 = new JLabel("学院:");
    private JLabel num = new JLabel("学号:");
    private JLabel lab4 = new JLabel("性别:");
    private JLabel lab5 = new JLabel("专业班级:");
    private JLabel lab6 = new JLabel("出生日期:");
    private JLabel lab7 = new JLabel("政治面貌:");
    private JLabel lab8 = new JLabel("联系方式:");
    // private JLabel lab3 = new JLabel("请输入内容,完成操作。");

    private JFrame frame = new JFrame("信息管理系统"); // 框架
    private JFrame frame1 = new JFrame("显示信息");

    Hashtable<String, Person> has = new Hashtable<String, Person>();// 哈希表,加密,文件乱码
    File file = new File("新建文件.txt");// 注意:新建一个文件
    public Menu() {
        if (!file.exists()) {
            try {
                ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));// 把一个实例的对象以文件的形式保存到磁盘上。        out.writeObject(has);
                out.close();
            } catch (IOException e) {
            }
        }
        but1.addActionListener(new ActionListener() { // 增加,内部类//进行某项操作时触发功能
            public void actionPerformed(ActionEvent e) {//用于接收操作事件的侦听器接口
                if (e.getSource() == but1) {
                    but3.setEnabled(false);//使but3这个按钮变灰不可点击了
                    String number1 = number.getText();
                    if (number1.length() == 12) {
                        try {
                            ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));//读回对象
                            has = (Hashtable) in.readObject();							                       in.close();
                        } catch (Exception e1) {
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "请输入12位数字的学号");//提示框显示
                    }
                    //
                    if (number1.length() == 12) {
                        if (has.containsKey(number1)) {						JOptionPane.showMessageDialog(null, "该生信息已存在,请到修改页面修改!");
                        } else {
                            String name1 = name.getText();
                            String dor1 = dor.getText();
                            String address1 = address.getText();
                            String sex1 = sex.getText();
                            String date1 = date.getText();
                            String pol1 = pol.getText();
                            String phone1 = phone.getText();
                            Person per = null;
                            per = new Person(number1, name1, dor1, address1, sex1, date1, pol1, phone1);
                            has.put(number1, per);// ???
                            try {
                                ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
                                out.writeObject(has);
                                out.close();
                                JOptionPane.showMessageDialog(null, "添加成功!");
                            } catch (Exception e1) {}
                        }
                    }
                }
            }
        });
        clear.addActionListener(new ActionListener() { // 清空
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == clear) {//获得事件所作用的对象

                    number.setText(null);
                    dor.setText(null);
                    name.setText(null);
                    address.setText(null);
                    sex.setText(null);
                    date.setText(null);
                    pol.setText(null);
                    phone.setText(null);
                }
            }
        });
        but2.addActionListener(new ActionListener() { // 删除
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == but2) {
                    but3.setEnabled(false);
                    String number1 = number.getText();
                    if (number1.length() == 12) {
                        try {
                            ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
                            has = (Hashtable) in.readObject();
                            in.close();
                        } catch (Exception e1) {
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "请输入12位数字的学号");
                    }
                    if (has.containsKey(number1)) {
                        has.remove(number1);
                        ObjectOutputStream out = null;
                        JOptionPane.showMessageDialog(null, "删除成功");
                        try {
                            out = new ObjectOutputStream(new FileOutputStream(file));
                            //out.writeObject(has);
                            out.close();
                        } catch (IOException ex) {
                            Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex);//记录当前类可能发生的异常
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "学号不存在");
                    }
                }
            }
        });

        but3.addActionListener(new ActionListener() { // 修改
            public void actionPerformed(ActionEvent e) {
                //String number2 = number.getText();
                if (e.getSource() == but3) {
                    but3.setEnabled(false);

                    String number1 = number.getText();
                    String name1 = name.getText();
                    String dor1 = dor.getText();
                    String address1 = address.getText();
                    String sex1 = sex.getText();
                    String date1 = date.getText();
                    String pol1 = pol.getText();
                    String phone1 = phone.getText();
                    Person per = new Person(number1, name1, dor1, address1, sex1, date1, pol1, phone1);
                    has.put(number1, per);
                    if (number1.length() == 12) {
                        try {
                            ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
                            has = (Hashtable) in.readObject();
                            in.close();
                        } catch (Exception e1) {
                        }
                        JOptionPane.showMessageDialog(null, "修改成功");

                        try {
                            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));
                            out.writeObject(has);
                            out.close();
                        } catch (Exception e1) {
                        }
                    }
                }
            }});

        but4.addActionListener(new ActionListener() { // 查看
            public void actionPerformed(ActionEvent e) {

                if (e.getSource() == but4) {
                    but3.setEnabled(false);
                    String number1 = number.getText();
                    if (number1.length() == 12) {
                        if (has.containsKey(number1)) {
                            try {
                                ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
                                has = (Hashtable) in.readObject();
                                in.close();
                            } catch (Exception e1) {
                            }
                            Person per = (Person) has.get(number1);
                            name.setText(per.getName());
                            dor.setText(per.getDor());
                            address.setText(per.getAddress());
                            sex.setText(per.getSex());
                            date.setText(per.getDate());
                            pol.setText(per.getPol());
                            phone.setText(per.getPhone());

                            but3.setEnabled(true);
                        } else {
                            JOptionPane.showMessageDialog(null, "学号不存在");
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "请输入12位数字的学号");
                    }
                }
            }

        });
        but5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == but5) {
                    frame1.setVisible(true);
                    try {
                        ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
                        has = (Hashtable) in.readObject();
                        in.close();
                    } catch (Exception ee) {
                    }
                    if (has.isEmpty()) {
                        show.append("目前还没有学生的信息记录!\n");
// append(s:String)向文本域的文本追加字符串,简单的说就像system.out.println()
                    } else {

                        for (Enumeration enu = has.elements(); enu.hasMoreElements();) {
//存入内存的内容如果不经过遍历是显示不出来的
                            Person per = (Person) enu.nextElement();
                            String str = "  <学号>:" + per.getNum() + "\n" + "  <姓名>:" + per.getName() + "\n" + "  <学院>:"
                                    + per.getDor() + "\n" + "  <性别>:" + per.getAddress() + "\n" + "  <专业班级>:"
                                    + per.getSex() + "\n" + "<出生日期>:" + per.getDate() + "\n" + "  <政治面貌>:"
                                    + per.getPol() + "\n" + " <电话>:" + per.getPhone() + "\n" + "\n";
                            show.append(str);
                        }
                        String str2 = "------------------------------结束---------------------------------------------------"
                                + "\n";
                        show.append(str2);
                    }
                }
            }

        });
        but0.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == but0) {
                    frame.setVisible(false);
                    new ActionHandle();
                }
            }
        });
        frame.setLayout(null);
        but1.setBounds(30, 35, 90, 25);
        but2.setBounds(30, 75, 90, 25);
        but3.setBounds(30, 115, 90, 25);
        but4.setBounds(30, 155, 90, 25);
        but0.setBounds(240, 430, 100, 25); // setBounds(x,y,width,height);
        num.setBounds(150, 30, 70, 25);
        lab1.setBounds(150, 65, 70, 25);
        lab2.setBounds(150, 100, 70, 25);//
        lab4.setBounds(150, 135, 70, 25);
        lab5.setBounds(150, 170, 70, 25);
        lab6.setBounds(150, 205, 70, 25);
        lab7.setBounds(150, 240, 70, 25);
        lab8.setBounds(150, 275, 70, 25);
        number.setBounds(230, 30, 90, 25);
        name.setBounds(230, 65, 90, 25);
        dor.setBounds(230, 100, 90, 25);
        address.setBounds(230, 135, 90, 25);
        sex.setBounds(230, 170, 90, 25);
        date.setBounds(230, 205, 90, 25);
        pol.setBounds(230, 240, 90, 25);
        phone.setBounds(230, 275, 90, 25);
        // lab3.setBounds(130, 390, 250, 25);
        clear.setBounds(250, 310, 60, 25);
        but5.setBounds(150, 310, 60, 25);
        frame.add(lab1);
        frame.add(lab2);
        //frame.add(lab3);
        frame.add(lab4);
        frame.add(lab5);
        frame.add(lab6);
        frame.add(lab7);
        frame.add(lab8);
        frame.add(num);
        frame.add(number);
        frame.add(name);
        frame.add(dor);
        frame.add(address);
        frame.add(sex);
        frame.add(date);
        frame.add(pol);
        frame.add(phone);
        frame.add(clear);
        frame.add(but1);
        frame.add(but2);
        frame.add(but3);
        frame.add(but4);
        frame.add(but0);
        JScrollPane scroll = new JScrollPane(show);
        frame1.add(scroll,BorderLayout.CENTER);
        frame.add(but5);
        frame.setSize(400, 500); // 页面大小
        frame1.setBounds(200, 200, 400, 300);
        frame.setLocation(300, 200);
        frame.setVisible(true);
        frame1.setVisible(false);
    }
}

信息

package student;

import java.io.Serializable;
class Person implements Serializable {//使用Serializable类,可以使对象被序列化,用于持久化(保存)对象。
    private String num;
    private String name;
    private String dor;
    private String address;
    private String sex;
    private String date;
    private String pol;
    private String phone;
    //public Person(){}
    public Person(String num,String name,String dor,String address,String sex,String date,String pol,String phone ){
        this.num=num;
        this.name=name;
        this.dor=dor;
        this.address=address;
        this.sex=sex;
        this.date=date;
        this.pol=pol;
        this.phone=phone;
    }
    public void setNum(String num){
        this.num=num;
    }
    public String getNum(){
        return num;
    }
    public void setName(String name){
        this.name=name;
    }
    public String getName(){
        return name;
    }
    public void setDor(String dor){
        this.dor=dor;
    }
    public String getDor(){
        return dor;
    }
    public void setAddress(String address){
        this.address=address;
    }
    public String getAddress(){
        return address;
    }
    public void setSex(String sex){
        this.sex=sex;
    }
    public String getSex(){
        return sex;
    }
    public void setDate(String date){
        this.date=date;
    }
    public String getDate(){
        return date;
    }
    public void setPol(String pol){
        this.pol=pol;
    }
    public String getPol(){
        return pol;
    }
    public void setPhone(String phone){
        this.phone=phone;
    }
    public String getPhone(){
        return phone;
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值