用java做一个UI界面,以便能够打开学生信息的文件,以表格形式查看文件内容,并能够修改学生信息的内容并保存。写出该程序的代码
时间: 2024-12-20 09:30:29 浏览: 5
在Java中创建一个用户界面(UI)来读取、显示和编辑学生信息通常涉及到Swing库或者JavaFX。以下是一个基本的示例,使用Swing来实现一个简单的窗口,它包含一个JTable用于显示和编辑数据。假设我们有一个包含学号、姓名和成绩的学生信息类。
首先,创建一个Student类:
```java
public class Student {
private int id;
private String name;
private double grade;
// getters and setters
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public double getGrade() { return grade; }
public void setGrade(double grade) { this.grade = grade; }
// toString for JTable display
@Override
public String toString() {
return "ID: " + id + ", Name: " + name + ", Grade: " + grade;
}
}
```
然后,创建一个简单的Swing UI:
```java
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class StudentInfoGUI extends JFrame {
private JTable jTable;
private DefaultTableModel model;
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
try {
StudentInfoGUI frame = new StudentInfoGUI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
});
}
public StudentInfoGUI() {
setTitle("Student Information");
// Create a table model
model = new DefaultTableModel(new Object[]{"ID", "Name", "Grade"}, 0);
// Load data from file
loadFromFile("students.txt");
// Create the table
jTable = new JTable(model);
jTable.getColumnModel().getColumn(0).setPreferredWidth(60);
jTable.getColumnModel().getColumn(1).setPreferredWidth(150);
jTable.getColumnModel().getColumn(2).setPreferredWidth(80);
// Add table to scroll pane
JScrollPane jScrollPane = new JScrollPane(jTable);
add(jScrollPane);
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void loadFromFile(String fileName) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(fileName));
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
model.addRow(new Object[]{parts[0], parts[1], Double.parseDouble(parts[2])});
}
} catch (IOException e) {
System.out.println("Error reading file");
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
// Implement editing functionality here
public void updateRow(int row, String newName, double newGrade) {
model.setValueAt(newName, row, 1);
model.setValueAt(newGrade, row, 2); // Assuming index 2 is for grades
}
// Save changes back to file
public void saveToFile() {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter("students.txt"));
for (int i = 0; i < model.getRowCount(); i++) {
String[] rowData = (String[])model.getValueAt(i, columnModel.getColumnIds());
writer.write(rowData[0] + "," + rowData[1] + "," + rowData[2] + "\n");
}
} catch (IOException e) {
System.out.println("Error saving file");
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
```
这个程序会从`students.txt`文件加载数据到JTable中,允许用户通过点击单元格进行编辑,并在`saveToFile()`函数中将更改保存回去。注意这只是一个基础示例,实际应用可能需要添加更多的错误处理和用户交互细节。
阅读全文