期末作业要求用Java语言设计一个简单的信息系统,对于我这种菜来说,这是一个噩梦。。。。。使劲憋都憋不出啥东西来。。。。好不容易做出来一个能够应付过去的,心里给我激动的,当时还自嗨说自己真牛逼,舍友也给我一顿彩虹屁。。。。大眼瞪小眼。。。。
进入正题:
实验目的:
实验环境:
界面设计与实现:
? ? ? ? ? ? ? ? ? ? ? ? ??
3. 数据模型与表格关联:
4. 功能实现:
? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ??
? ? ? ? ? ? ? ? ? ? ? ?
在上述所有例子中,主要使用了Java语言和Swing库来实现前端(用户界面)部分,而后端则是通过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);
});
}
}