Java-GUI programming actual combat management system Day3 [GUI design of student management system, student management system addition, deletion and modification check, project MVC structure introduction, Java skills map]

  1. Java-GUI programming actual combat management system Day1 [Project development process, software three-tier architecture, project requirements, project structure analysis]
  2. Java-GUI programming actual combat management system Day2 [Swing (component introduction, layout manager, event class and listener class), basic component button and input box usage]

  3. Java-GUI programming actual combat management system Day3 [GUI design of student management system, student management system addition, deletion and modification check, project MVC structure introduction, Java skills map]

table of Contents

GUI Design of Student Management System

Additions, deletions, and changes to the student management system

Student management system source code

1、package cn.itcast.student.app;

MainApp.java

2、package cn.itcast.student.controller;

AdminDialogController.java

MainFrameController.java

3、package cn.itcast.student.dao;

StudentDao.java

4、package cn.itcast.student.data;

DataBase.java

5、package cn.itcast.student.domain;

Student.java

6、package cn.itcast.student.service;

StudentService.java

7、package cn.itcast.student.tools;

GUITools.java

8、package cn.itcast.student.view;

AbstractAdminDialog.java

AbstractMainFrame.java

Project MVC structure introduction

Introduction to the main project window

Project pop-up window introduction

Introduction to project table loading query

Introduction to project table addition

Introduction to project table modification and deletion

Java skills map


GUI Design of Student Management System

  • Several interfaces
  • Which components
  • Coordinate requirements

Additions, deletions, and changes to the student management system

  • Component optimization
  • MVC code interpretation
  • Data storage ideas
  • Run detailed analysis

Student management system source code

1、package cn.itcast.student.app;

MainApp.java

package cn.itcast.student.app;

import cn.itcast.student.controller.MainFrameController;

/**
 * 项目运行
 */
public class MainApp {
    public static void main(String[] args) {
        new MainFrameController().setVisible(true);
    }
}

2、package cn.itcast.student.controller;

AdminDialogController.java

package cn.itcast.student.controller;

import cn.itcast.student.domain.Student;
import cn.itcast.student.service.StudentService;
import cn.itcast.student.view.AbstractAdminDialog;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.*;
import java.util.ArrayList;

/**
 * 管理员界面操作类
 */
@SuppressWarnings("serial")
public class AdminDialogController extends AbstractAdminDialog {
    //定义服务类,提供完整功能服务
    private StudentService studentService = new StudentService();

    //构造方法
    public AdminDialogController() {
        super();
    }

    public AdminDialogController(Frame owner, boolean modal) {
        super(owner, modal);
        //创建对象时展示数据
        queryStudent();
    }

    //查询方法
    @Override
    public void queryStudent() {
        //定义表格头
        String[] thead = {"学生编号", "学生姓名", "学生年龄", "学生住址"};
        //调用adminService的查询服务
        ArrayList<Student> dataList = studentService.queryStudent();
        //调用list2Array方法,将查询到的集合转为数组,方便为JTable赋值
        String[][] tbody = list2Array(dataList);
        //将查询到的结果为table赋值
        TableModel dataModel = new DefaultTableModel(tbody, thead);
        table.setModel(dataModel);
    }

    //集合数据转为二维数组方法
    public String[][] list2Array(ArrayList<Student> list) {
        //根据Student的model与集合数据定义JTable的数据二维数组
        String[][] tbody = new String[list.size()][4];
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            tbody[i][0] = student.getId();
            tbody[i][1] = student.getName();
            tbody[i][2] = student.getAge() + "";
            tbody[i][3] = student.getAddress();
        }
        return tbody;
    }

    //添加方法
    @Override
    public void addStudent() {
        //获取数据
        String addId = addNumberText.getText();
        String addName = addNameText.getText();
        String addAge = addAgeText.getText();
        String addAddress = addAddressText.getText();
        //调用adminService的添加服务
        boolean addSuccess = studentService.addStudent(addId, addName,
                Integer.parseInt(addAge), addAddress);
        //如果添加成功
        if (addSuccess) {
            //添加后刷新表格
            queryStudent();
        } else {
            //没有添加成功弹窗错误提示
            JOptionPane.showMessageDialog(this, "学生学号不能重复,请检查数据!");
        }
    }

    //修改方法
    @Override
    public void updateStudent() {
        //获取数据
        String updateId = updateNumberText.getText();
        String updateName = updateNameText.getText();
        String updateAge = updateAgeText.getText();
        String updateAddress = updateAddressText.getText();
        //调用adminService的修改服务
        boolean updateSuccess = studentService.updateStudent(updateId,
                updateName, Integer.parseInt(updateAge), updateAddress);
        //如果修改成功
        if (updateSuccess) {
            //修改后刷新表格
            queryStudent();
        } else {
            //没有修改成功弹窗错误提示
            JOptionPane.showMessageDialog(this, "没有这个学号的学生,请检查数据!");
        }
    }

    //删除方法
    @Override
    public void delStudent() {
        //获取数据
        String delId = delNumberText.getText();
        //调用adminService的删除服务
        boolean delSuccess = studentService.delStudent(delId);
        //如果删除成功
        if (delSuccess) {
            //删除后刷新表格
            queryStudent();
        } else {
            //没有删除成功弹窗错误提示
            JOptionPane.showMessageDialog(this, "没有这个学号的学生,请检查数据!");
        }
    }
}

MainFrameController.java

package cn.itcast.student.controller;

import cn.itcast.student.view.AbstractMainFrame;

/**
 * 主界面操作类
 */
@SuppressWarnings("serial")
public class MainFrameController extends AbstractMainFrame {
    @Override
    public void showAdminDialog() {
        //在该方法中创建管理员界面并显示
        //this为父窗口(主界面)  true:设置为模态窗口展示
        new AdminDialogController(this, true).setVisible(true);
    }
}

3、package cn.itcast.student.dao;

StudentDao.java

package cn.itcast.student.dao;

import cn.itcast.student.data.DataBase;
import cn.itcast.student.domain.Student;

import java.util.ArrayList;

/**
 * 学生信息  数据访问层
 */
public class StudentDao {
    //获取所有数据
    public ArrayList<Student> queryAllData() {
        return DataBase.data;
    }

    //添加数据
    public void addStudent(Student student) {
        DataBase.data.add(student);
    }

    //删除数据
    public void delStudent(String delNumber) {
        //查询集合中数据
        for (int i = 0; i < DataBase.data.size(); i++) {
            Student thisStudent = DataBase.data.get(i);
            //如果有学生项的编号与传入编号相同,则从集合中删除
            if (thisStudent.getId().equals(delNumber)) {
                DataBase.data.remove(i);
            }
        }
    }
}

4、package cn.itcast.student.data;

DataBase.java

package cn.itcast.student.data;

import cn.itcast.student.domain.Student;

import java.util.ArrayList;

/**
 * 存储数据
 */
public class DataBase {
    public static ArrayList<Student> data = new ArrayList<Student>();

    //初始数据
    static {
        data.add(new Student("s001", "明日花", 31, "s1团"));
    }
}

5、package cn.itcast.student.domain;

Student.java

package cn.itcast.student.domain;

public class Student {
    private String id;
    private String name;
    private int age;
    private String address;

    public Student() {
    }

    public Student(String id, String name, int age, String address) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

6、package cn.itcast.student.service;

StudentService.java

package cn.itcast.student.service;

import cn.itcast.student.dao.StudentDao;
import cn.itcast.student.domain.Student;

import java.util.ArrayList;

public class StudentService {
    private StudentDao studentDao = new StudentDao();

    //查询服务
    public ArrayList<Student> queryStudent() {
        //调用Dao层的获取所有数据方法获取所有数据
        ArrayList<Student> data = studentDao.queryAllData();
        //返回数据
        return data;
    }

    //添加服务
    public boolean addStudent(String id, String name, int age, String address) {
        //调用Dao层的获取所有数据方法获取所有数据
        ArrayList<Student> data = queryStudent();
        //使用输入的编号与所有数据对比
        for (int i = 0; i < data.size(); i++) {
            Student student = data.get(i);
            //如果存在重复编号数据,则添加不成功
            if (id.equals(student.getId())) {
                return false;
            }
        }
        //如果没有重复编号,将数据封装为Student对象
        Student thisStudent = new Student(id, name,
                age, address);
        //调用Dao层的添加数据方法
        studentDao.addStudent(thisStudent);
        //在添加数据后,返回添加成功
        return true;
    }

    //修改服务
    public boolean updateStudent(String id, String name, int age, String address) {
        //调用Dao层的获取所有数据方法获取所有数据
        ArrayList<Student> data = queryStudent();
        //使用输入的编号与所有数据对比
        for (int i = 0; i < data.size(); i++) {
            Student student = data.get(i);
            //如果存在相同编号数据,则可以更新
            if (id.equals(student.getId())) {
                //调用Dao层的删除指定编号数据方法
                studentDao.delStudent(id);
                //如果没有重复编号,将数据封装为Student对象
                Student thisStudent = new Student(id, name,
                        age, address);
                //调用Dao层的添加数据方法
                studentDao.addStudent(thisStudent);
                //在修改数据后,返回添加成功
                return true;
            }
        }
        //如果不存在相同编号数据,则不可以更新
        return false;
    }

    //删除服务
    public boolean delStudent(String delId) {
        //调用Dao层的获取所有数据方法获取所有数据
        ArrayList<Student> data = queryStudent();
        //使用输入的编号与所有数据对比
        for (int i = 0; i < data.size(); i++) {
            Student student = data.get(i);
            //如果存在相同编号数据,则可以删除
            if (delId.equals(student.getId())) {
                //调用Dao层的删除指定编号数据方法
                studentDao.delStudent(delId);
                //在删除数据后,返回添加成功
                return true;
            }
        }
        //如果不存在相同编号数据,则不可以删除
        return false;
    }
}

7、package cn.itcast.student.tools;

GUITools.java

package cn.itcast.student.tools;

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

/*
 * 工具类
 */
public class GUITools {
    //JAVA提供的GUI默认工具类对象
    static Toolkit kit = Toolkit.getDefaultToolkit();

    //将指定组件屏幕居中
    public static void center(Component c) {
        int x = (kit.getScreenSize().width - c.getWidth()) / 2;
        int y = (kit.getScreenSize().height - c.getHeight()) / 2;
        c.setLocation(x, y);
    }

    //为指定窗口设置图标标题
    public static void setTitleImage(JFrame frame, String titleIconPath) {
        frame.setIconImage(kit.createImage(titleIconPath));
    }
}

8、package cn.itcast.student.view;

AbstractAdminDialog.java

package cn.itcast.student.view;

import cn.itcast.student.tools.GUITools;

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

/**
 * 管理窗口类
 */
@SuppressWarnings("serial")
public abstract class AbstractAdminDialog extends JDialog {
    //定义界面使用到的组件作为成员变量
    private JLabel tableLabel = new JLabel("学生列表");//学生列表标题
    private JScrollPane tablePane = new JScrollPane();//滚动视口
    protected JTable table = new JTable(); //学生列表
    private JLabel numberLabel = new JLabel("学生学号");//编号标题
    private JLabel nameLabel = new JLabel("学生名字");//名称标题
    private JLabel ageLabel = new JLabel("学生年龄");//学生年龄
    private JLabel addressLabel = new JLabel("学生地址");//学生地址
    //添加功能组件
    protected JTextField addNumberText = new JTextField(6);//添加编号文本框
    protected JTextField addNameText = new JTextField(6);//添加名称文本框
    protected JTextField addAgeText = new JTextField(6);//添加单价文本框
    protected JTextField addAddressText = new JTextField(6);//添加计价单位文本框
    private JButton addBtn = new JButton("添加学生");//添加按钮
    //修改功能组件
    protected JTextField updateNumberText = new JTextField(6);//修改编号文本框
    protected JTextField updateNameText = new JTextField(6);//修改名称文本框
    protected JTextField updateAgeText = new JTextField(6);//修改单价文本框
    protected JTextField updateAddressText = new JTextField(6);//修改计价单位文本框
    private JButton updateBtn = new JButton("修改学生");//修改按钮
    //删除功能组件
    protected JTextField delNumberText = new JTextField(6);//添加编号文本
    private JButton delBtn = new JButton("删除学生");//删除按钮

    //构造方法
    public AbstractAdminDialog() {
        this(null, true);
    }

    public AbstractAdminDialog(Frame owner, boolean modal) {
        super(owner, modal);
        this.init();// 初始化操作
        this.addComponent();// 添加组件
        this.addListener();// 添加监听器
    }

    // 初始化操作
    private void init() {
        this.setTitle("学生管理系统!");// 标题
        this.setSize(600, 400);// 窗体大小与位置
        GUITools.center(this);//设置窗口在屏幕上的位置
        this.setResizable(false);// 窗体大小固定
    }

    // 添加组件
    private void addComponent() {
        //取消布局
        this.setLayout(null);
        //表格标题
        tableLabel.setBounds(265, 20, 70, 25);
        this.add(tableLabel);
        //表格
        table.getTableHeader().setReorderingAllowed(false);    //列不能移动
        table.getTableHeader().setResizingAllowed(false);    //不可拉动表格
        table.setEnabled(false);                            //不可更改数据
        tablePane.setBounds(50, 50, 500, 200);
        tablePane.setViewportView(table);                    //视口装入表格
        this.add(tablePane);
        //字段标题
        numberLabel.setBounds(50, 250, 70, 25);
        nameLabel.setBounds(150, 250, 70, 25);
        ageLabel.setBounds(250, 250, 70, 25);
        addressLabel.setBounds(350, 250, 70, 25);
        this.add(numberLabel);
        this.add(nameLabel);
        this.add(ageLabel);
        this.add(addressLabel);
        //增加组件
        addNumberText.setBounds(50, 280, 80, 25);
        addNameText.setBounds(150, 280, 80, 25);
        addAgeText.setBounds(250, 280, 80, 25);
        addAddressText.setBounds(350, 280, 80, 25);
        this.add(addNumberText);
        this.add(addNameText);
        this.add(addAgeText);
        this.add(addAddressText);
        addBtn.setBounds(460, 280, 90, 25);
        this.add(addBtn);
        //修改组件
        updateNumberText.setBounds(50, 310, 80, 25);
        updateNameText.setBounds(150, 310, 80, 25);
        updateAgeText.setBounds(250, 310, 80, 25);
        updateAddressText.setBounds(350, 310, 80, 25);
        this.add(updateNumberText);
        this.add(updateNameText);
        this.add(updateAgeText);
        this.add(updateAddressText);
        updateBtn.setBounds(460, 310, 90, 25);
        this.add(updateBtn);
        //删除组件
        delNumberText.setBounds(50, 340, 80, 25);
        this.add(delNumberText);
        delBtn.setBounds(460, 340, 90, 25);
        this.add(delBtn);
    }

    // 添加监听器
    private void addListener() {
        //添加按钮监听
        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //调用添加方法
                addStudent();
            }
        });
        //修改按钮监听
        updateBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //调用修改方法
                updateStudent();
            }
        });
        //删除按钮监听
        delBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //调用删除方法
                delStudent();
            }
        });
    }

    //查询方法
    public abstract void queryStudent();

    //添加方法
    public abstract void addStudent();

    //修改方法
    public abstract void updateStudent();

    //删除方法
    public abstract void delStudent();
}

AbstractMainFrame.java

package cn.itcast.student.view;

import cn.itcast.student.tools.GUITools;

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

/**
 * 主窗口类
 */
@SuppressWarnings("serial")
public abstract class AbstractMainFrame extends JFrame { // 抽象类一般会有子类,子类实现其方法
    //组件
    private JLabel titleLabel = new JLabel(new ImageIcon("..\\store\\logo.png"));//标题图片
    private JButton btn = new JButton("进入系统");//进入系统的按钮

    //构造函数
    public AbstractMainFrame() {
        this.init();// 初始化操作
        this.addComponent();// 添加组件
        this.addListener();// 添加监听器
    }

    //初始化操作
    private void init() {
        this.setTitle("学生管理系统欢迎您!");// 标题
        this.setSize(600, 400);// 窗体大小与位置
        GUITools.center(this);//设置窗口在屏幕上的位置
        GUITools.setTitleImage(this, "..\\store\\title.png");
        this.setResizable(false);// 窗体大小固定
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 关闭窗口默认操作
    }

    //添加组件
    private void addComponent() {
        //窗体使用默认的边界布局,北区放入图片
        this.add(this.titleLabel, BorderLayout.NORTH); // 基础组件直接添加进Frame中
        //创建JPanel对象
        JPanel btnPanel = new JPanel();
        //清除布局,使JPanel中的组件可以自定义位置
        btnPanel.setLayout(null);
        //将JPanel对象添加到窗体中
        this.add(btnPanel);
        //定义边界位置
        btn.setBounds(240, 20, 120, 50);
        //将按钮添加到JPanel对象中
        btnPanel.add(btn);
    }

    //添加监听器
    private void addListener() {
        btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                showAdminDialog();
            }
        });
    }

    //展示管理员界面方法
    public abstract void showAdminDialog();
}

Project MVC structure introduction

  

  • m: data processing layer, business logic layer (dao layer, service layer);
  • v: Interface layer (view layer);
  • c: (controller layer) inherits the role of (interface view) and under (model (dao, service)).

Service layer: business logic processing layer, calling data layer, only responsible for business logic processing.

Controller: Receiving requests (linking from above) to receiving requests initiated by the user interface and requesting to the service.

Requests initiated by the view page first arrive at the controller, and the controller starts as the controller and calls the service; the service needs data, and the service calls dao.

Calling process: view -> controller -> service -> dao (three-tier structure of business logic: controller, service, dao).

Introduction to the main project window

Project pop-up window introduction

  

IDEA plug-in series (11): JFormDesigner plug-in-advanced Swing GUI designer

  

There is only one event type (click event), and three event sources (three click buttons).

Introduction to project table loading query

Introduction to project table addition

  

Introduction to project table modification and deletion

  

Java skills map

Guess you like

Origin blog.csdn.net/weixin_44949135/article/details/113394222