栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java实战项目【学生信息管理系统】swring + mysql

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java实战项目【学生信息管理系统】swring + mysql

因为个人原因,在可视化这方面落下了课程,我尽力在补,目前功能不完善,后续会完善。

登录界面 代码
package APP;

import javax.swing.*;
import java.awt.*;

public class LoginView extends JFrame {
    //建立弹簧布局
    SpringLayout springLayout = new SpringLayout();
    //设置 流布局的管理器为弹簧布局
    JPanel jPanel = new JPanel(springLayout);
    //定义组件
    JLabel title = new JLabel("学生信息管理系统");
    JLabel user = new JLabel("用户名:");
    JTextField username = new JTextField();
    JLabel password = new JLabel("密  码:");
    JTextField password1 = new JTextField();
    JButton login = new JButton("登录");


    //构造组件
    public LoginView() {
        //名字
        super("弹簧布局");
        Container tan_huang_bu_ju_mian_ban = getContentPane();
        jPanel.setBackground(Color.CYAN);
        jPanel.add(title);
        jPanel.add(user);
        jPanel.add(username);
        username.setPreferredSize(new Dimension(200, 30));//设置titleText大小
        jPanel.add(password);
        jPanel.add(password1);
        password1.setPreferredSize(new Dimension(200, 30));
        jPanel.add(login);
        login.setPreferredSize(new Dimension(80, 30));
//写法1
        //设置title位置
        SpringLayout.Constraints title1 = springLayout.getConstraints(title);//获取title布局信息
        title1.setX(Spring.constant(200));//离左100
        title1.setY(Spring.constant(50));//离上50
//写法2
        
        //设置user
        springLayout.putConstraint(SpringLayout.WEST, user, -170, SpringLayout.EAST, title);
        springLayout.putConstraint(SpringLayout.NORTH, user, 25, SpringLayout.SOUTH, title);
        springLayout.putConstraint(SpringLayout.WEST, username, 0, SpringLayout.EAST, user);
        springLayout.putConstraint(SpringLayout.NORTH, username, 25, SpringLayout.SOUTH, title);

        //设置其他组件位置
        springLayout.putConstraint(SpringLayout.EAST, password, 0, SpringLayout.EAST, user);
        springLayout.putConstraint(SpringLayout.NORTH, password, 25, SpringLayout.SOUTH, user);
        springLayout.putConstraint(SpringLayout.EAST, password1, 0, SpringLayout.EAST, username);
        springLayout.putConstraint(SpringLayout.NORTH, password1, 10, SpringLayout.SOUTH, username);
        springLayout.putConstraint(SpringLayout.WEST, login, 50, SpringLayout.EAST, password);
        springLayout.putConstraint(SpringLayout.NORTH, login, 20, SpringLayout.SOUTH, password);
        tan_huang_bu_ju_mian_ban.add(jPanel); //添加到面板


        //大小
        setSize(500, 400);
        //大小不可改变
        setResizable(false);
        //居中
        //1自动居中
        //jFrame.setLocationRelativeTo(null);
        //2自行计算后居中
        Dimension ping_mu_size = Toolkit.getDefaultToolkit().getScreenSize();//获取屏幕大小
        int offsetX = (ping_mu_size.width - 600) / 2;//设置横向偏移
        int offsetY = (ping_mu_size.height - 400) / 2;//设置纵向偏移
        setLocation(offsetX, offsetY);
        //关闭后退出
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //可见
        setVisible(true);
    }


    //主函数
    public static void main(String[] args) {
        new LoginView();
    }
}
效果

主界面
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 has = new Hashtable();// 哈希表,加密,文件乱码
    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);
    }
}

3.写出个人信息(person)   代码:

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;
    }
}
功能 增加
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) {}
                        }
                    }
                }
            }
        });
修改
        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位数字的学号");
                    }
                }
            }
 
        });
删除
 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, "学号不存在");
                    }
                }
            }
        });
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/966626.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号