|
|
package com.WR.StudentMS.view;
|
|
|
|
|
|
import javax.swing.*;
|
|
|
import javax.swing.table.DefaultTableModel;
|
|
|
import com.WR.StudentMS.dao.mysql.Coursemdaoimplqsy;
|
|
|
import com.WR.StudentMS.model.Coursemqsy;
|
|
|
import java.awt.*;
|
|
|
import java.util.List;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
public class CoursemPanel extends JPanel {
|
|
|
|
|
|
private List<Coursemqsy> coursemList; // 存储从数据库加载的课程信息列表
|
|
|
private Coursemdaoimplqsy coursemDao; // 数据访问对象
|
|
|
private JTable table; // 显示课程信息的表格
|
|
|
private DefaultTableModel model; // 表格的数据模型
|
|
|
private JTextField searchField;
|
|
|
private JButton searchButton;
|
|
|
|
|
|
public CoursemPanel() {
|
|
|
super(new BorderLayout()); // 使用BorderLayout布局管理器
|
|
|
coursemDao = new Coursemdaoimplqsy();
|
|
|
model = new DefaultTableModel();
|
|
|
initializeUI();
|
|
|
loadCourses(); // 加载课程信息
|
|
|
}
|
|
|
|
|
|
private void initializeUI() {
|
|
|
// 定义列名
|
|
|
String[] columnNames = {"课程ID", "课程名", "课程介绍"};
|
|
|
model.setColumnIdentifiers(columnNames);
|
|
|
table = new JTable(model);
|
|
|
|
|
|
// 添加表格到滚动面板
|
|
|
JScrollPane scrollPane = new JScrollPane(table);
|
|
|
add(scrollPane, BorderLayout.CENTER);
|
|
|
|
|
|
// 创建按钮面板
|
|
|
JPanel buttonPanel = new JPanel();
|
|
|
buttonPanel.setLayout(new FlowLayout());
|
|
|
|
|
|
// 添加按钮
|
|
|
JButton addButton = new JButton("添加课程信息");
|
|
|
JButton deleteButton = new JButton("删除选中的课程信息");
|
|
|
|
|
|
// 创建搜索框和搜索按钮
|
|
|
searchField = new JTextField();
|
|
|
searchField.setPreferredSize(new Dimension(200, searchField.getPreferredSize().height));
|
|
|
searchButton = new JButton("课程ID查询");
|
|
|
|
|
|
// 为按钮添加事件监听器
|
|
|
addButton.addActionListener(e -> addCourse());
|
|
|
deleteButton.addActionListener(e -> deleteSelectedCourse());
|
|
|
searchButton.addActionListener(e -> searchCourses());
|
|
|
// 将按钮添加到面板
|
|
|
buttonPanel.add(addButton);
|
|
|
buttonPanel.add(deleteButton);
|
|
|
buttonPanel.add(searchField);
|
|
|
buttonPanel.add(searchButton); // 添加搜索按钮到面板
|
|
|
|
|
|
// 将按钮面板添加到底部
|
|
|
add(buttonPanel, BorderLayout.SOUTH);
|
|
|
}
|
|
|
|
|
|
private void loadCourses() {
|
|
|
try {
|
|
|
// 从数据库加载课程信息并更新表格和列表
|
|
|
coursemList = coursemDao.findAllqsy();
|
|
|
model.setRowCount(0); // 清空表格
|
|
|
for (Coursemqsy coursem : coursemList) {
|
|
|
Object[] rowData = {
|
|
|
coursem.getCourseidqsy(),
|
|
|
coursem.getCoursenameqsy(),
|
|
|
coursem.getJieshaoqsy()
|
|
|
};
|
|
|
model.addRow(rowData);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
JOptionPane.showMessageDialog(this, "加载课程信息失败: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void addCourse() {
|
|
|
// 弹出对话框让用户输入课程信息
|
|
|
String input = (String) JOptionPane.showInputDialog(
|
|
|
this,
|
|
|
"请输入课程信息(格式:课程ID-课程名-课程介绍):",
|
|
|
"添加课程",
|
|
|
JOptionPane.QUESTION_MESSAGE,
|
|
|
null,
|
|
|
null,
|
|
|
null
|
|
|
);
|
|
|
if (input != null && !input.trim().isEmpty()) {
|
|
|
String[] parts = input.split("-", 3); // 修改为分割三部分
|
|
|
if (parts.length == 3) { // 确保分割成三部分
|
|
|
Coursemqsy newCourse = new Coursemqsy();
|
|
|
newCourse.setCourseidqsy(parts[0].trim());
|
|
|
newCourse.setCoursenameqsy(parts[1].trim());
|
|
|
newCourse.setJieshaoqsy(parts[2].trim());
|
|
|
|
|
|
int result = coursemDao.createqsy(newCourse);
|
|
|
if (result > 0) {
|
|
|
newCourse = coursemDao.findByIdqsy(newCourse.getCourseidqsy()); // 确保可以检索到新添加的课程信息
|
|
|
if (newCourse != null) {
|
|
|
Object[] rowData = {
|
|
|
newCourse.getCourseidqsy(),
|
|
|
newCourse.getCoursenameqsy(),
|
|
|
newCourse.getJieshaoqsy()
|
|
|
};
|
|
|
model.addRow(rowData); // 添加到表格
|
|
|
JOptionPane.showMessageDialog(this, "课程信息添加成功!");
|
|
|
} else {
|
|
|
JOptionPane.showMessageDialog(this, "课程信息添加失败!");
|
|
|
}
|
|
|
}
|
|
|
else {
|
|
|
JOptionPane.showMessageDialog(this, "输入格式不正确,需要课程ID-课程名-课程介绍");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void deleteSelectedCourse() {
|
|
|
int selectedRow = table.getSelectedRow();
|
|
|
if (selectedRow >= 0) {
|
|
|
String courseId = (String) model.getValueAt(selectedRow, 0);
|
|
|
Coursemqsy coursemToDelete = new Coursemqsy();
|
|
|
coursemToDelete.setCourseidqsy(courseId);
|
|
|
int result = coursemDao.removeqsy(coursemToDelete);
|
|
|
if (result > 0) {
|
|
|
model.removeRow(selectedRow); // 从表格中移除
|
|
|
JOptionPane.showMessageDialog(this, "课程信息删除成功!");
|
|
|
} else {
|
|
|
JOptionPane.showMessageDialog(this, "课程信息删除失败!");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
private void searchCourses() {
|
|
|
String searchQuery = searchField.getText();
|
|
|
if (!searchQuery.trim().isEmpty()) {
|
|
|
// 过滤课程列表,仅根据课程ID进行搜索
|
|
|
List<Coursemqsy> filteredList = coursemList.stream()
|
|
|
.filter(course -> course.getCourseidqsy().equals(searchQuery)) // 仅根据课程ID过滤
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
// 更新表格模型以显示过滤后的课程信息
|
|
|
model.setRowCount(0); // 清空表格
|
|
|
for (Coursemqsy course : filteredList) {
|
|
|
Object[] rowData = {
|
|
|
course.getCourseidqsy(),
|
|
|
course.getCoursenameqsy(),
|
|
|
course.getJieshaoqsy()
|
|
|
};
|
|
|
model.addRow(rowData);
|
|
|
}
|
|
|
} else {
|
|
|
// 如果搜索框为空,则重新加载所有课程信息
|
|
|
loadCourses();
|
|
|
}
|
|
|
}
|
|
|
} |