Java期末作业(OA办公管理设计与实现)

发布时间:2023年12月28日

期末作业要求用Java语言设计一个简单的信息系统,对于我这种菜来说,这是一个噩梦。。。。。使劲憋都憋不出啥东西来。。。。好不容易做出来一个能够应付过去的,心里给我激动的,当时还自嗨说自己真牛逼,舍友也给我一顿彩虹屁。。。。大眼瞪小眼。。。。

进入正题:

实验目的:

  1. 掌握Java编程语言的基础知识,尤其是Swing框架的运用。
  2. 学习并理解图形用户界面(GUI)的设计原理和实现方法。
  3. 实践数据模型与表格(JTable)的关联使用。
  4. 综合应用Java技术进行OA办公管理信息系统的设计和开发。

实验环境:

  1. Java编程语言,推荐使用JDK 8及以上版本。
  2. 集成开发环境(IDE):Eclipse。

界面设计与实现:

  • Swing框架应用: 利用Swing组件设计GUI界面,使用JFrame作为主窗口,JTabbedPane实现不同功能模块的切换。
  • JTable展示员工信息: 使用JTable组件展示员工信息,通过DefaultTableModel作为数据模型,实现表头排序、数据动态更新等功能。
  • 功能模块面板设计: 针对每个功能模块设计相应的面板,包括员工管理、日程管理、任务管理和公告管理。如图1.1

? ? ? ? ? ? ? ? ? ? ? ? ??

3. 数据模型与表格关联:

  • DefaultTableModel使用: 使用DefaultTableModel作为JTable的数据模型,通过Vector或二维数组存储数据,实现数据的动态管理和更新。
  • TableModelListener监听器: 利用TableModelListener监听器实现对数据的实时响应,确保界面与数据的同步性。

4. 功能实现:

  • 员工管理功能:
    • 添加员工:通过JOptionPane.showInputDialog实现输入对话框,添加新员工信息。
    • 编辑员工:同样利用输入对话框,编辑选中员工的信息。
    • 删除员工:从员工列表中删除选中员工。如图1.2

? ? ? ? ? ? ? ? ? ? ? ??

  • 日程管理功能:
    • 查看日程:使用JOptionPane.showMessageDialog显示日程信息。
    • 添加日程:通过输入对话框添加新的日程信息。图1.3

? ? ? ??

  • 任务管理功能:
    • 查看任务:使用JOptionPane.showMessageDialog显示任务信息。
    • 添加任务:通过输入对话框添加新的任务信息。如图1.4

? ? ? ? ? ? ? ? ? ? ? ?

  • 公告管理功能:
    • 查看公告:使用JOptionPane.showMessageDialog显示公告信息。
    • 添加公告:通过输入对话框添加新的公告信息。如图1.5

在上述所有例子中,主要使用了Java语言和Swing库来实现前端(用户界面)部分,而后端则是通过Java语言实现的。具体技术如下:

前端(用户界面):

  • 使用Java的Swing库:Swing是Java提供的图形用户界面(GUI)工具包,用于创建桌面应用程序的用户界面。

后端(业务逻辑):

  • 使用Java语言:Java是一种跨平台的高级编程语言,适用于构建各种类型的应用程序。

数据库:

  • 在示例中没有使用数据库,而是使用了内存中的数据结构来模拟存储。

属实不易,记录一下。。。。。

代码如下:

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

class Employee {
    int id;
    String name;
    String gender;
    int age;
    int height;
    String position;
    String department;

    public Employee(int id, String name, String gender, int age, int height, String position, String department) {
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.age = age;
        this.height = height;
        this.position = position;
        this.department = department;
    }
}

class Schedule {
    int id;
    String description;

    public Schedule(int id, String description) {
        this.id = id;
        this.description = description;
    }
}

class Task {
    int id;
    String description;

    public Task(int id, String description) {
        this.id = id;
        this.description = description;
    }
}

class Announcement {
    int id;
    String content;

    public Announcement(int id, String content) {
        this.id = id;
        this.content = content;
    }
}

public class OAGUI extends JFrame {
    private final List<Employee> employees;
    private final List<Schedule> schedules;
    private final List<Task> tasks;
    private final List<Announcement> announcements;

    private DefaultTableModel employeeTableModel;

    public OAGUI() {
        employees = new ArrayList<>();
        schedules = new ArrayList<>();
        tasks = new ArrayList<>();
        announcements = new ArrayList<>();

        // 模拟一些员工数据
        employees.add(new Employee(1, "张三", "男", 25, 175, "工程师", "研发部"));
        employees.add(new Employee(2, "李四", "女", 30, 160, "销售经理", "销售部"));
        employees.add(new Employee(3, "王五", "男", 28, 180, "人力资源", "人力资源部"));

        // 模拟一些日程、任务和公告数据
        schedules.add(new Schedule(1, "开会"));
        schedules.add(new Schedule(2, "培训"));
        tasks.add(new Task(1, "完成项目A"));
        tasks.add(new Task(2, "准备销售报告"));
        announcements.add(new Announcement(1, "公司年会通知"));
        announcements.add(new Announcement(2, "新项目启动"));

        initComponents();
    }

    private void initComponents() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("OA办公管理信息系统");

        // 创建表格模型
        employeeTableModel = new DefaultTableModel();
        employeeTableModel.addColumn("ID");
        employeeTableModel.addColumn("姓名");
        employeeTableModel.addColumn("性别");
        employeeTableModel.addColumn("年龄");
        employeeTableModel.addColumn("身高");
        employeeTableModel.addColumn("职位");
        employeeTableModel.addColumn("部门");

        // 创建表格
        JTable employeeTable = new JTable(employeeTableModel);
        JScrollPane employeeScrollPane = new JScrollPane(employeeTable);

        // 创建按钮和文本框
        JButton addEmployeeButton = new JButton("添加员工");
        JButton editEmployeeButton = new JButton("编辑员工");
        JButton deleteEmployeeButton = new JButton("删除员工");

        JTextArea informationTextArea = new JTextArea();
        informationTextArea.setEditable(false);
        JScrollPane informationScrollPane = new JScrollPane(informationTextArea);

        // 设置布局
        setLayout(new BorderLayout());

        // 创建员工管理面板
        JPanel employeePanel = new JPanel();
        employeePanel.setLayout(new BorderLayout());
        employeePanel.add(employeeScrollPane, BorderLayout.CENTER);

        JPanel employeeButtonPanel = new JPanel();
        employeeButtonPanel.setLayout(new FlowLayout());
        employeeButtonPanel.add(addEmployeeButton);
        employeeButtonPanel.add(editEmployeeButton);
        employeeButtonPanel.add(deleteEmployeeButton);
        employeePanel.add(employeeButtonPanel, BorderLayout.SOUTH);

        // 创建日程管理面板
        JPanel schedulePanel = new JPanel();
        schedulePanel.setLayout(new BorderLayout());
        JButton viewScheduleButton = new JButton("查看日程");
        JButton addScheduleButton = new JButton("添加日程");

        schedulePanel.add(viewScheduleButton, BorderLayout.NORTH);
        schedulePanel.add(addScheduleButton, BorderLayout.SOUTH);

        // 创建任务管理面板
        JPanel taskPanel = new JPanel();
        taskPanel.setLayout(new BorderLayout());
        JButton viewTaskButton = new JButton("查看任务");
        JButton addTaskButton = new JButton("添加任务");

        taskPanel.add(viewTaskButton, BorderLayout.NORTH);
        taskPanel.add(addTaskButton, BorderLayout.SOUTH);

        // 创建公告管理面板
        JPanel announcementPanel = new JPanel();
        announcementPanel.setLayout(new BorderLayout());
        JButton viewAnnouncementButton = new JButton("查看公告");
        JButton addAnnouncementButton = new JButton("发布公告");

        announcementPanel.add(viewAnnouncementButton, BorderLayout.NORTH);
        announcementPanel.add(addAnnouncementButton, BorderLayout.SOUTH);

        // 添加到卡片面板
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.addTab("员工管理", employeePanel);
        tabbedPane.addTab("日程管理", schedulePanel);
        tabbedPane.addTab("任务管理", taskPanel);
        tabbedPane.addTab("公告管理", announcementPanel);

        getContentPane().add(tabbedPane, BorderLayout.WEST);
        getContentPane().add(informationScrollPane, BorderLayout.CENTER);

        // 添加事件监听器
        addEmployeeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addEmployee();
            }
        });

        editEmployeeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                editEmployee(employeeTable.getSelectedRow());
            }
        });

        deleteEmployeeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                deleteEmployee(employeeTable.getSelectedRow());
            }
        });

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

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

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

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

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

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

        // 初始化员工列表
        updateEmployeeTable();

        // 设置窗口大小
        setSize(800, 600);

        // 让窗口居中显示
        setLocationRelativeTo(null);
    }

    private void addEmployee() {
        String name = JOptionPane.showInputDialog(this, "输入员工姓名:");
        String gender = JOptionPane.showInputDialog(this, "输入员工性别:");
        int age = Integer.parseInt(JOptionPane.showInputDialog(this, "输入员工年龄:"));
        int height = Integer.parseInt(JOptionPane.showInputDialog(this, "输入员工身高:"));
        String position = JOptionPane.showInputDialog(this, "输入员工职位:");
        String department = JOptionPane.showInputDialog(this, "输入员工部门:");

        employees.add(new Employee(employees.size() + 1, name, gender, age, height, position, department));
        updateEmployeeTable();
    }

    private void editEmployee(int selectedRow) {
        if (selectedRow >= 0 && selectedRow < employees.size()) {
            Employee selectedEmployee = employees.get(selectedRow);
            String name = JOptionPane.showInputDialog(this, "输入员工姓名:", selectedEmployee.name);
            String gender = JOptionPane.showInputDialog(this, "输入员工性别:", selectedEmployee.gender);
            int age = Integer.parseInt(JOptionPane.showInputDialog(this, "输入员工年龄:", selectedEmployee.age));
            int height = Integer.parseInt(JOptionPane.showInputDialog(this, "输入员工身高:", selectedEmployee.height));
            String position = JOptionPane.showInputDialog(this, "输入员工职位:", selectedEmployee.position);
            String department = JOptionPane.showInputDialog(this, "输入员工部门:", selectedEmployee.department);

            selectedEmployee.name = name;
            selectedEmployee.gender = gender;
            selectedEmployee.age = age;
            selectedEmployee.height = height;
            selectedEmployee.position = position;
            selectedEmployee.department = department;

            updateEmployeeTable();
        } else {
            JOptionPane.showMessageDialog(this, "请选择要编辑的员工");
        }
    }

    private void deleteEmployee(int selectedRow) {
        if (selectedRow >= 0 && selectedRow < employees.size()) {
            employees.remove(selectedRow);
            updateEmployeeTable();
        } else {
            JOptionPane.showMessageDialog(this, "请选择要删除的员工");
        }
    }

    private void viewSchedules() {
        StringBuilder scheduleText = new StringBuilder("日程:\n");
        for (Schedule schedule : schedules) {
            scheduleText.append(schedule.description).append("\n");
        }
        showMessageDialog("查看日程", scheduleText.toString());
    }

    private void addSchedule() {
        String description = JOptionPane.showInputDialog(this, "输入日程描述:");
        schedules.add(new Schedule(schedules.size() + 1, description));
    }

    private void viewTasks() {
        StringBuilder taskText = new StringBuilder("任务:\n");
        for (Task task : tasks) {
            taskText.append(task.description).append("\n");
        }
        showMessageDialog("查看任务", taskText.toString());
    }

    private void addTask() {
        String description = JOptionPane.showInputDialog(this, "输入任务描述:");
        tasks.add(new Task(tasks.size() + 1, description));
    }

    private void viewAnnouncements() {
        StringBuilder announcementText = new StringBuilder("公告:\n");
        for (Announcement announcement : announcements) {
            announcementText.append(announcement.content).append("\n");
        }
        showMessageDialog("查看公告", announcementText.toString());
    }

    private void addAnnouncement() {
        String content = JOptionPane.showInputDialog(this, "输入公告内容:");
        announcements.add(new Announcement(announcements.size() + 1, content));
    }

    private void showMessageDialog(String title, String message) {
        JTextArea textArea = new JTextArea(message, 15, 40);
        textArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(textArea);
        JOptionPane.showMessageDialog(this, scrollPane, title, JOptionPane.INFORMATION_MESSAGE);
    }

    private void updateEmployeeTable() {
        // 清空表格
        employeeTableModel.setRowCount(0);

        // 填充表格
        for (Employee employee : employees) {
            Object[] rowData = {
                    employee.id,
                    employee.name,
                    employee.gender,
                    employee.age,
                    employee.height,
                    employee.position,
                    employee.department
            };
            employeeTableModel.addRow(rowData);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
                e.printStackTrace();
            }
            new OAGUI().setVisible(true);
        });
    }
}

文章来源:https://blog.csdn.net/m0_66319233/article/details/135248062
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。