用java实现具有如下功能界面的功能1、定义员工类Teacher类,包括成员变量name,sno,education,构造方法Teacher(String name,String sno,String education),并为三个成员变量设置对应的三对get、set方法,重写toString方法用于获取教师信息;2、在界面中下方输入教师姓名、工号、职称,点击“添加记录”按钮可以把信息添加到上方JTable中,同时根据用户输入的教师姓名、工号、职称信息构造教师对象,把教师对象信息放入HashSet集合中,并同时存入一个文件中,并同时存入界面的JTable组件中,文件如下图所示:3、在界面中选择Jtable中某一行数据,点击“删除记录”可以删除界面Jtable中某一行数据。
时间: 2024-02-18 15:59:25 浏览: 26
这是一个比较复杂的问题,需要用到Java的GUI编程以及集合和文件操作。以下是简单的实现示例,仅供参考:
1. 定义员工类Teacher
```java
public class Teacher {
private String name;
private String sno;
private String education;
public Teacher(String name, String sno, String education) {
this.name = name;
this.sno = sno;
this.education = education;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSno() {
return sno;
}
public void setSno(String sno) {
this.sno = sno;
}
public String getEducation() {
return education;
}
public void setEducation(String education) {
this.education = education;
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + '\'' +
", sno='" + sno + '\'' +
", education='" + education + '\'' +
'}';
}
}
```
2. 创建GUI界面,并添加JTable和按钮
```java
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.HashSet;
import java.util.Set;
public class TeacherGUI extends JFrame {
private JTable table;
private DefaultTableModel model;
private JTextField nameField;
private JTextField snoField;
private JTextField educationField;
public TeacherGUI() {
setTitle("教师管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600, 400);
// 创建JTable
model = new DefaultTableModel();
model.addColumn("姓名");
model.addColumn("工号");
model.addColumn("职称");
table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
// 创建文本框和按钮
nameField = new JTextField(10);
snoField = new JTextField(10);
educationField = new JTextField(10);
JButton addButton = new JButton("添加记录");
addButton.addActionListener(new AddButtonListener());
JButton deleteButton = new JButton("删除记录");
deleteButton.addActionListener(new DeleteButtonListener());
// 创建面板,将组件添加进去
JPanel panel = new JPanel();
panel.add(new JLabel("姓名:"));
panel.add(nameField);
panel.add(new JLabel("工号:"));
panel.add(snoField);
panel.add(new JLabel("职称:"));
panel.add(educationField);
panel.add(addButton);
panel.add(deleteButton);
// 将面板和JTable添加到窗口中
Container contentPane = getContentPane();
contentPane.add(scrollPane, BorderLayout.CENTER);
contentPane.add(panel, BorderLayout.SOUTH);
setVisible(true);
}
// 添加记录按钮的监听器
private class AddButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String sno = snoField.getText();
String education = educationField.getText();
Teacher teacher = new Teacher(name, sno, education);
// 将教师对象添加到HashSet和JTable中
Set<Teacher> teacherSet = readFromFile();
teacherSet.add(teacher);
model.addRow(new Object[]{name, sno, education});
// 将教师信息写入文件中
writeToFile(teacherSet);
}
}
// 删除记录按钮的监听器
private class DeleteButtonListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
int selectedRow = table.getSelectedRow();
if (selectedRow >= 0) {
// 从HashSet和JTable中删除教师对象
Set<Teacher> teacherSet = readFromFile();
String sno = (String)model.getValueAt(selectedRow, 1);
teacherSet.removeIf(teacher -> teacher.getSno().equals(sno));
model.removeRow(selectedRow);
// 将教师信息写入文件中
writeToFile(teacherSet);
}
}
}
// 从文件中读取教师信息
private Set<Teacher> readFromFile() {
Set<Teacher> teacherSet = new HashSet<>();
File file = new File("teacher.txt");
if (file.exists()) {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
String[] array = line.split(",");
if (array.length == 3) {
Teacher teacher = new Teacher(array[0], array[1], array[2]);
teacherSet.add(teacher);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
return teacherSet;
}
// 将教师信息写入文件中
private void writeToFile(Set<Teacher> teacherSet) {
File file = new File("teacher.txt");
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
for (Teacher teacher : teacherSet) {
String line = teacher.getName() + "," + teacher.getSno() + "," + teacher.getEducation();
writer.write(line);
writer.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new TeacherGUI();
}
}
```
在这个示例中,我们通过HashSet集合来保存教师对象,并且将教师信息写入到文件中。当用户点击添加记录按钮时,我们会先从文件中读取教师信息,然后将新的教师对象添加到HashSet和JTable中,最后再将教师信息写入到文件中。当用户点击删除记录按钮时,我们会先从文件中读取教师信息,然后从HashSet和JTable中删除指定的教师对象,最后再将教师信息写入到文件中。