IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> 学生信息管理系统(Java Swing) -> 正文阅读

[Java知识库]学生信息管理系统(Java Swing)

用户类

package Message;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable {
    private String name;
    private String password;
    private String affirmPassword;
    private Date birthday;
    public User(String name, String password, String affirmPassword, Date birthday){
        this.name=name;
        this.password=password;
        this.affirmPassword=affirmPassword;
        this.birthday=birthday;
    }
    public User(){}

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAffirmPassword() {
        return affirmPassword;
    }

    public void setAffirmPassword(String affirmPassword) {
        this.affirmPassword = affirmPassword;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public String toString(){
        return "User{"+
                " 用户名=\""+name+"\""+
                " 密码=\""+password+"\""+
                " 生日="+birthday+" }";
    }
}

学生类

package Message;

import java.io.Serializable;

public class Student implements Serializable {
    String name;           // 姓名
    String studentNumber;  // 学号
    double language;       // 语文
    double math;          // 数学
    double English;       // 英语

    public Student() {
    }

    public Student(String name, String studentNumber, double language, double math, double english) {
        this.name = name;
        this.studentNumber = studentNumber;
        this.language = language;
        this.math = math;
        English = english;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStudentNumber() {
        return studentNumber;
    }

    public void setStudentNumber(String studentNumber) {
        this.studentNumber = studentNumber;
    }

    public double getLanguage() {
        return language;
    }

    public void setLanguage(double language) {
        this.language = language;
    }

    public double getMath() {
        return math;
    }

    public void setMath(double math) {
        this.math = math;
    }

    public double getEnglish() {
        return English;
    }

    public void setEnglish(double english) {
        English = english;
    }
    public double sum(){
        return language+math+English;
    }
    public String toString(){
        return "Student{"+
                " 姓名=\""+name+"\""+"\n"+
                " 学号=\""+studentNumber+"\""+"\n"+
                " 语文=\""+language+"\""+"\n"+
                " 数学=\""+math+"\""+"\n"+
                " 英语=\""+English+"\n"+
                " }";
    }
}

整合用户类和学生类内容

package domain;

import Message.Student;
import Message.User;

import java.text.ParseException;
import java.util.Date;

public class Conform {
    public static User conUser(String name, String password, String affirmPassword, String birthday){
        User user=new User();
        user.setName(name);
        user.setPassword(password);
        user.setAffirmPassword(affirmPassword);
        try {
            user.setBirthday(DateUtils.stringToDate(birthday,"yyyy-MM-dd"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return user;
    }
    public static Student conStudent(String name,String studentNumber,String language,String math,String English){
        Student student=new Student();
        student.setName(name);
        student.setStudentNumber(studentNumber);
        student.setLanguage(Integer.valueOf(language));
        student.setMath(Integer.valueOf(math));
        student.setEnglish(Integer.valueOf(English));
        return student;
    }
}

Date与String的转换

package domain;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {
    public static String dateToString(Date date, String format){
        SimpleDateFormat sdf=new SimpleDateFormat(format);
        String s=sdf.format(date);
        return s;
    }
    public static Date stringToDate(String str,String format) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat(format);
        Date date=sdf.parse(str);
        return date;
    }
}

?主体

package domain;

import Message.Student;
import Message.User;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;

public class Subject {
    ArrayList<User> alUser=new ArrayList<>();  // 用户集合
    ArrayList<Student> alStudent=new ArrayList<>(); // 学生集合
    boolean b=false;
    public Subject()
    {
        if(fileNULL("User.txt")){
            getUser();
        }
        if(fileNULL("Student.txt")){
            getStudent();
        }
        logIn();
    }
    // 登录界面
    public void logIn(){
        JFrame jFrame=new JFrame("用户登录");
        jFrame.setSize(400,300);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setResizable(false);
        jFrame.setLocationRelativeTo(null);

        JPanel jPanel=new JPanel(null);
        JLabel jLabel1=new JLabel("用户名");
        jLabel1.setBounds(50,50,50,30);
        JTextField jTextField=new JTextField(10);
        jTextField.setBounds(100,50,150,30);
        JLabel jLabel2=new JLabel("密码");
        jLabel2.setBounds(50,100,50,30);
        JPasswordField jPasswordField=new JPasswordField(10);
        jPasswordField.setBounds(100,100,150,30);
        JButton jButton1=new JButton("登录");
        jButton1.setBounds(80,200,60,30);
        JButton jButton2=new JButton("注册");
        jButton2.setBounds(200,200,60,30);
        JButton jButton3=new JButton("保存");
        jButton3.setBounds(300,200,60,30);
        jPanel.add(jLabel1);
        jPanel.add(jTextField);
        jPanel.add(jLabel2);
        jPanel.add(jPasswordField);
        jPanel.add(jButton1);
        jPanel.add(jButton2);
        jPanel.add(jButton3);
        jButton1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(alUser.size()!=0){
                    for (User u:alUser) {
                        if(u.getName().equals(jTextField.getText()) && u.getPassword().equals(jPasswordField.getText())){
                            JOptionPane.showMessageDialog(jFrame ,"登录成功","success",JOptionPane.WARNING_MESSAGE);
                            b=true;
                            jFrame.setVisible(false);
                            mainWindow();
                        }
                    }
                    if(!b){
                        JOptionPane.showMessageDialog(jFrame,"登录失败","error",JOptionPane.ERROR_MESSAGE);
                    }

                }
                else {
                    JOptionPane.showMessageDialog(jFrame,"无用户请注册","error",JOptionPane.ERROR_MESSAGE);
                }


            }
        });

        jButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                register();
            }
        });

        jButton3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setUser();
            }
        });
        jFrame.setContentPane(jPanel);
        jFrame.setVisible(true);

    }

    public void register(){
        JFrame jf=new JFrame("用户注册");
        jf.setSize(300,400);
        jf.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
        jf.setResizable(false);

        JPanel jp=new JPanel(null);
        JLabel jLabel1=new JLabel("用户名");
        jLabel1.setBounds(50,50,50,30);
        JTextField jTextField=new JTextField(10);
        jTextField.setBounds(100,50,150,30);
        JLabel jLabel2=new JLabel("密码");
        jLabel2.setBounds(50,100,50,30);
        JPasswordField jPasswordField1=new JPasswordField(10);
        jPasswordField1.setBounds(100,100,150,30);
        JLabel jLabel3=new JLabel("确认密码");
        jLabel3.setBounds(50,150,50,30);
        JPasswordField jPasswordField2=new JPasswordField(10);
        jPasswordField2.setBounds(100,150,150,30);
        JLabel jLabel4=new JLabel("生日");
        jLabel4.setBounds(50,200,50,30);
        JTextField jTextField1=new JTextField(10);
        jTextField1.setBounds(100,200,150,30);
        JButton jButton=new JButton("注册");
        jButton.setBounds(110,300,80,30);
        jp.add(jLabel1);
        jp.add(jTextField);
        jp.add(jLabel2);
        jp.add(jLabel3);
        jp.add(jPasswordField2);
        jp.add(jPasswordField1);
        jp.add(jLabel4);
        jp.add(jTextField1);
        jp.add(jButton);

        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(jPasswordField1.getText().equals(jPasswordField2.getText())){
                    alUser.add(Conform.conUser(jTextField.getText(),jPasswordField1.getText(),
                            jPasswordField2.getText(),jTextField1.getText()));
                    JOptionPane.showMessageDialog(jf,"注册成功","success",JOptionPane.WARNING_MESSAGE);
                    jf.setVisible(false);
                }
                else {
                    JOptionPane.showMessageDialog(jf,"两次密码不同","error",JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        jf.setContentPane(jp);
        jf.setVisible(true);
    }

    public void setUser(){      // 将用户集合写进文件
        ObjectOutputStream oos= null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("User.txt"));
            oos.writeObject(alUser);
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void getUser(){             //读取文件中的用户集合
        ObjectInputStream ois= null;
        ArrayList<User> al=new ArrayList<>();
        try {
            ois = new ObjectInputStream(new FileInputStream("User.txt"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            al=(ArrayList<User>) ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            ois.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (User user:al) {
            alUser.add(user);
        }
    }

    public boolean fileNULL(String s){
        File file=new File(s);
        if(file.exists()&&file.length()==0){
            return false;
        }
        return true;
    }

    // 主窗口
    public void mainWindow(){
        JFrame jf=new JFrame();
        jf.setSize(400,300);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jf.setResizable(false);
        jf.setLocationRelativeTo(null);

        JPanel jp=new JPanel(null);
        JLabel jLabel1=new JLabel("学生信息管理系统");
        jLabel1.setBounds(150,30,100,30);
        JButton jb1=new JButton("添加");
        jb1.setBounds(100,80,80,30);
        JButton jb2=new JButton("删除");
        jb2.setBounds(220,80,80,30);
        JButton jb3=new JButton("修改");
        jb3.setBounds(100,130,80,30);
        JButton jb4=new JButton("查找");
        jb4.setBounds(220,130,80,30);
        JButton jb5=new JButton("表格");
        jb5.setBounds(100,180,80,30);
        JButton jb6=new JButton("保存");
        jb6.setBounds(220,180,80,30);
        jp.add(jLabel1);
        jp.add(jb1);
        jp.add(jb2);
        jp.add(jb3);
        jp.add(jb4);
        jp.add(jb5);
        jp.add(jb6);

        jb1.addActionListener(new ActionListener() {   // 添加
            @Override
            public void actionPerformed(ActionEvent e) {
                addStudent();
            }
        });

        jb2.addActionListener(new ActionListener() {   // 删除
            @Override
            public void actionPerformed(ActionEvent e) {
                deleteStudent();
            }
        });

        jb3.addActionListener(new ActionListener() {    // 修改
            @Override
            public void actionPerformed(ActionEvent e) {
                alterStudent();
            }
        });

        jb4.addActionListener(new ActionListener() {   // 查找
            @Override
            public void actionPerformed(ActionEvent e) {
                findStudent();
            }
        });

        jb5.addActionListener(new ActionListener() {    // 表格
            @Override
            public void actionPerformed(ActionEvent e) {
                form();
            }
        });

        jb6.addActionListener(new ActionListener() {    // 保存
            @Override
            public void actionPerformed(ActionEvent e) {
                setStudent();
            }
        });
        jf.setContentPane(jp);
        jf.setVisible(true);
    }

    public void add(Student s){   // 添加学生
        alStudent.add(s);
    }

    public boolean delete(String studentNumber){  // 删除学生
        if(find(studentNumber)==null){
            return false;
        }
        else {
            alStudent.remove(find(studentNumber));
            return true;
        }
    }

    public boolean alter(Student student){  // 修改
        if(delete(student.getStudentNumber())){
            add(student);
            return true;
        }
        else {
            return false;
        }
    }
    public Student find(String studentNumber){   // 查找
        for (Student s:alStudent) {
            if(s.getStudentNumber().equals(studentNumber)){
                return s;
            }
        }
        return null;
    }

    public void addStudent(){  // 添加学生窗口
        JFrame jf=new JFrame("添加学生");
        jf.setSize(300,400);
        jf.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
        jf.setResizable(false);

        JPanel jp=new JPanel(null);
        JLabel jLabel1=new JLabel("姓名");
        jLabel1.setBounds(50,50,50,30);
        JTextField jTextField1=new JTextField(10);
        jTextField1.setBounds(100,50,150,30);
        JLabel jLabel2=new JLabel("学号");
        jLabel2.setBounds(50,100,50,30);
        JTextField jTextField2=new JTextField(10);
        jTextField2.setBounds(100,100,150,30);
        JLabel jLabel3=new JLabel("语文");
        jLabel3.setBounds(50,150,50,30);
        JTextField jTextField3=new JTextField(10);
        jTextField3.setBounds(100,150,150,30);
        JLabel jLabel4=new JLabel("数学");
        jLabel4.setBounds(50,200,50,30);
        JTextField jTextField4=new JTextField(10);
        jTextField4.setBounds(100,200,150,30);
        JLabel jLabel5=new JLabel("英语");
        jLabel5.setBounds(50,250,50,30);
        JTextField jTextField5=new JTextField(10);
        jTextField5.setBounds(100,250,150,30);
        JButton jButton=new JButton("添加");
        jButton.setBounds(80,300,80,30);

        jp.add(jLabel1);
        jp.add(jLabel2);
        jp.add(jLabel3);
        jp.add(jLabel4);
        jp.add(jLabel5);
        jp.add(jTextField1);
        jp.add(jTextField2);
        jp.add(jTextField3);
        jp.add(jTextField4);
        jp.add(jTextField5);
        jp.add(jButton);

        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                add(Conform.conStudent(jTextField1.getText(),jTextField2.getText(),jTextField3.getText(),
                        jTextField4.getText(),jTextField5.getText()));
                JOptionPane.showMessageDialog(jf,"添加成功","success",JOptionPane.WARNING_MESSAGE);
            }
        });

        jf.setContentPane(jp);
        jf.setVisible(true);
    }

    public void alterStudent(){  // 修改学生信息窗口
        JFrame jf=new JFrame("添加学生");
        jf.setSize(300,400);
        jf.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
        jf.setResizable(false);

        JPanel jp=new JPanel(null);
        JLabel jLabel1=new JLabel("姓名");
        jLabel1.setBounds(50,50,50,30);
        JTextField jTextField1=new JTextField(10);
        jTextField1.setBounds(100,50,150,30);
        JLabel jLabel2=new JLabel("学号");
        jLabel2.setBounds(50,100,50,30);
        JTextField jTextField2=new JTextField(10);
        jTextField2.setBounds(100,100,150,30);
        JLabel jLabel3=new JLabel("语文");
        jLabel3.setBounds(50,150,50,30);
        JTextField jTextField3=new JTextField(10);
        jTextField3.setBounds(100,150,150,30);
        JLabel jLabel4=new JLabel("数学");
        jLabel4.setBounds(50,200,50,30);
        JTextField jTextField4=new JTextField(10);
        jTextField4.setBounds(100,200,150,30);
        JLabel jLabel5=new JLabel("英语");
        jLabel5.setBounds(50,250,50,30);
        JTextField jTextField5=new JTextField(10);
        jTextField5.setBounds(100,250,150,30);
        JButton jButton=new JButton("修改");
        jButton.setBounds(80,300,80,30);

        jp.add(jLabel1);
        jp.add(jLabel2);
        jp.add(jLabel3);
        jp.add(jLabel4);
        jp.add(jLabel5);
        jp.add(jTextField1);
        jp.add(jTextField2);
        jp.add(jTextField3);
        jp.add(jTextField4);
        jp.add(jTextField5);
        jp.add(jButton);

        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                alter(Conform.conStudent(jTextField1.getText(),jTextField2.getText(),jTextField3.getText(),
                        jTextField4.getText(),jTextField5.getText()));
                JOptionPane.showMessageDialog(jf,"修改成功","success",JOptionPane.WARNING_MESSAGE);
            }
        });

        jf.setContentPane(jp);
        jf.setVisible(true);
    }

    public void deleteStudent(){  // 删除学生窗口
        JFrame jf=new JFrame("删除学生");
        jf.setSize(300,250);
        jf.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
        jf.setResizable(false);

        JPanel jp=new JPanel(null);
        JLabel jLabel=new JLabel("学号");
        jLabel.setBounds(50,100,50,30);
        JTextField jTextField=new JTextField(10);
        jTextField.setBounds(110,100,150,30);
        JButton jb=new JButton("删除");
        jb.setBounds(120,150,60,30);

        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                delete(jTextField.getText());
                JOptionPane.showMessageDialog(jf,"删除成功","success",JOptionPane.WARNING_MESSAGE);
            }
        });
        jp.add(jLabel);
        jp.add(jTextField);
        jp.add(jb);

        jf.setContentPane(jp);
        jf.setVisible(true);
    }

    public void findStudent(){  // 查找学生窗口
        JFrame jf=new JFrame("查找学生");
        jf.setSize(400,400);
        jf.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
        jf.setResizable(false);

        JPanel jp=new JPanel(null);
        JLabel jLabel=new JLabel("学号");
        jLabel.setBounds(20,20,50,30);
        JTextField jTextField=new JTextField(10);
        jTextField.setBounds(80,20,150,30);
        JButton jb=new JButton("查找");
        jb.setBounds(20,70,60,30);
        final JTextArea ja=new JTextArea(5,10);
        ja.setBounds(100,70,200,100);


        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(find(jTextField.getText())!=null){
                    ja.append(find(jTextField.getText()).toString());

                }
                else {
                    JOptionPane.showMessageDialog(jf,"无该学生","success",JOptionPane.WARNING_MESSAGE);
                }
            }
        });
        jp.add(jLabel);
        jp.add(jTextField);
        jp.add(ja);
        jp.add(jb);

        jf.setContentPane(jp);
        jf.setVisible(true);
    }

    public void setStudent(){      // 将学生集合写进文件
        ObjectOutputStream oos= null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("Student.txt"));
            oos.writeObject(alStudent);
            oos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void getStudent(){             //读取文件中的学生集合
        ObjectInputStream ois= null;
        ArrayList<Student> al=new ArrayList<>();
        try {
            ois = new ObjectInputStream(new FileInputStream("Student.txt"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            al=(ArrayList<Student>) ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            ois.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (Student s:al) {
            alStudent.add(s);
        }
    }

    public void form(){
        JFrame jf=new JFrame("表格");
        jf.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);

        JPanel jp=new JPanel(new BorderLayout());
        Object[] columnNames={"学号","姓名","语文","数学","英语","总分"};
        Object[][] rowData=new Object[alStudent.size()][6];
        for(int i=0;i<alStudent.size();i++){
            rowData[i][0]=alStudent.get(i).getStudentNumber();
            rowData[i][1]=alStudent.get(i).getName();
            rowData[i][2]=alStudent.get(i).getLanguage();
            rowData[i][3]=alStudent.get(i).getMath();
            rowData[i][4]=alStudent.get(i).getEnglish();
            rowData[i][5]=alStudent.get(i).sum();
        }

        TableModel tableModel=new DefaultTableModel(rowData,columnNames);
        JTable table=new JTable(tableModel);
        RowSorter<TableModel> rowSorter=new TableRowSorter<TableModel>(tableModel);
        table.setRowSorter(rowSorter);
        jp.add(table.getTableHeader(),BorderLayout.NORTH);
        jp.add(table,BorderLayout.CENTER);

        jf.setContentPane(jp);
        jf.pack();
        jf.setLocationRelativeTo(null);
        jf.setVisible(true);
    }

}

测试

package Test;

import Message.Student;
import domain.Subject;

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

运行结果

?

源代码链接:链接:https://pan.baidu.com/s/13WiRM491r0rVeqNIrWyK5Q?
提取码:abcd

?

?

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2021-07-10 14:24:43  更:2021-07-10 14:26:55 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年12日历 -2024/12/18 18:07:17-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码