gpt4 book ai didi

java - 读取文本文件(我已经知道)然后在 GUI 中显示它?

转载 作者:行者123 更新时间:2023-12-01 13:14:54 24 4
gpt4 key购买 nike

好吧,我的程序看起来非常基本,我有一个类将学生输入到文本文件中,该类必须读取它们,将它们显示在 GUI 中,以便用户可以选择想要的学生,然后remove 方法从文本文件中删除学生。问题是,这比我曾经研究过的东西要复杂得多,我真的需要帮助,这是我删除学生类的代码:

import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class StudentsRemove extends Frame implements ActionListener
{
String messsage="";
Button buttonView, buttonClose, buttonRemove; // Implements all the buttons
Label labelAnswer; // Implements all the different text boxes
TextField textAnswer;

StudentsRemove(String name)
{
super(name);
setLayout(new GridLayout(7,7));

labelAnswer = new Label("Remove Student: ");
textAnswer = new TextField("");
buttonView = new Button("VIEW STUDENTS");
buttonRemove = new Button("REMOVE");
buttonClose = new Button("CLOSE");

add(labelAnswer);
add(textAnswer);
add(buttonView);
add(buttonRemove);
add(buttonClose);
setVisible(true);

buttonView.addActionListener(this);
buttonRemove.addActionListener(this);
buttonClose.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e)
{
String s="";

String str = e.getActionCommand();
if(str.equals("VIEW STUDENTS"))
{

try
{
BufferedReader BuffRe = new BufferedReader(new FileReader("Students.txt"));
StringBuilder StrBu = new StringBuilder();
String line = BuffRe.readLine();

while (line != null)
{
StrBu.append(line);
StrBu.append(System.lineSeparator());
line = BuffRe.readLine();
//numStudents++;
}
String everything = StrBu.toString();
BuffRe.close();
}

catch(Exception z)
{
System.out.println("The Exception Is : " +z);
}

}
}

}

当我读完整个程序后,我需要帮助在 GUI 中显示它们,然后允许用户选择其中一名学生并将其删除。我知道很多,但我完全迷失了,并且没有“广泛”的编程知识。

提前谢谢大家。

西拉。

最佳答案

首先声明 JListListModel 实例变量...

private JList studentList;
private DefaultListModel model;

这将用于显示您的信息并允许用户与之交互。

创建一些方法来更新模型...

public void addStudent(String student) {
model.addElement(student);
}

public void deleteStudent(String student) {
model.removeElement(student);
}

创建从磁盘读取学生信息并将其写入磁盘的方法...

public List<String> loadStudents() throws IOException {
List<String> students = new ArrayList<>(25);
try (BufferedReader br = new BufferedReader(new FileReader("Students.txt"))) {

String student = null;
while ((student = br.readLine()) != null) {
students.add(student);
}

} finally {
}
return students;
}

public void saveStudents(List<String> students) throws IOException {
try (BufferedWriter bw = new BufferedWriter(new FileWriter("Students.txt"))) {

for (String student : students) {
bw.write(student);
bw.newLine();
}
bw.flush();

} finally {
}
}

然后将文件中的学生信息加载到模型中...

protected void save() {
try {
List<String> students = loadStudents();
model = new DefaultListModel();
for (String student : students) {
model.addElement(student);
}
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "Could not read students.txt: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
studentList.setModel(model);
}

并编写一个方法将模型中的信息保存到磁盘......

protected void save() {
List<String> students = new ArrayList<>(model.getSize());
for (int index = 0; index < model.getSize(); index++) {
students.add((String)model.get(index));
}

try {
saveStudents(students);
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "Could not write students.txt: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
}

现在,显然,您需要创建 JList 并将其添加到您的 UI,但我会将其留给您。

看看Creating a GUI With JFC/SwingHow to Use Lists了解更多详情

关于java - 读取文本文件(我已经知道)然后在 GUI 中显示它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22545083/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com