JAVAswing老师管理系统源码
时间: 2024-12-11 20:11:06 浏览: 14
Java Swing是一个用于创建图形用户界面(GUI)的Java库。使用Java Swing可以开发出功能丰富、跨平台的桌面应用程序。下面是一个简单的Java Swing老师管理系统的源码示例,该系统可以实现基本的增删改查(CRUD)功能。
```java
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
public class TeacherManagementSystem extends JFrame {
private JTextField nameField, ageField, subjectField;
private JButton addButton, updateButton, deleteButton, resetButton;
private JTable table;
private DefaultTableModel tableModel;
private List<Teacher> teacherList = new ArrayList<>();
private Teacher selectedTeacher;
public TeacherManagementSystem() {
setTitle("老师管理系统");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
// 初始化表单
JPanel formPanel = new JPanel(new GridLayout(4, 2));
formPanel.add(new JLabel("姓名:"));
nameField = new JTextField();
formPanel.add(nameField);
formPanel.add(new JLabel("年龄:"));
ageField = new JTextField();
formPanel.add(ageField);
formPanel.add(new JLabel("科目:"));
subjectField = new JTextField();
formPanel.add(subjectField);
JPanel buttonPanel = new JPanel(new FlowLayout());
addButton = new JButton("添加");
updateButton = new JButton("更新");
deleteButton = new JButton("删除");
resetButton = new JButton("重置");
buttonPanel.add(addButton);
buttonPanel.add(updateButton);
buttonPanel.add(deleteButton);
buttonPanel.add(resetButton);
formPanel.add(buttonPanel);
add(formPanel, BorderLayout.NORTH);
// 初始化表格
tableModel = new DefaultTableModel(new String[]{"姓名", "年龄", "科目"}, 0);
table = new JTable(tableModel);
add(new JScrollPane(table), BorderLayout.CENTER);
// 添加事件监听器
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addTeacher();
}
});
updateButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateTeacher();
}
});
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deleteTeacher();
}
});
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
resetForm();
}
});
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int selectedRow = table.getSelectedRow();
if (selectedRow >= 0) {
selectedTeacher = teacherList.get(selectedRow);
nameField.setText(selectedTeacher.getName());
ageField.setText(String.valueOf(selectedTeacher.getAge()));
subjectField.setText(selectedTeacher.getSubject());
}
}
});
}
private void addTeacher() {
String name = nameField.getText();
int age = Integer.parseInt(ageField.getText());
String subject = subjectField.getText();
Teacher teacher = new Teacher(name, age, subject);
teacherList.add(teacher);
tableModel.addRow(new Object[]{name, age, subject});
resetForm();
}
private void updateTeacher() {
if (selectedTeacher != null) {
selectedTeacher.setName(nameField.getText());
selectedTeacher.setAge(Integer.parseInt(ageField.getText()));
selectedTeacher.setSubject(subjectField.getText());
int selectedRow = table.getSelectedRow();
tableModel.setValueAt(nameField.getText(), selectedRow, 0);
tableModel.setValueAt(Integer.parseInt(ageField.getText()), selectedRow, 1);
tableModel.setValueAt(subjectField.getText(), selectedRow, 2);
resetForm();
}
}
private void deleteTeacher() {
if (selectedTeacher != null) {
teacherList.remove(selectedTeacher);
int selectedRow = table.getSelectedRow();
tableModel.removeRow(selectedRow);
resetForm();
}
}
private void resetForm() {
nameField.setText("");
ageField.setText("");
subjectField.setText("");
selectedTeacher = null;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TeacherManagementSystem().setVisible(true);
}
});
}
}
class Teacher {
private String name;
private int age;
private String subject;
public Teacher(String name, int age, String subject) {
this.name = name;
this.age = age;
this.subject = subject;
}
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 getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
}
```
这个示例代码展示了如何使用Java Swing创建一个简单的老师管理系统,包括添加、删除、更新和重置功能。
阅读全文