【发布时间】:2014-03-20 21:05:52
【问题描述】:
好的,所以我的程序看起来很基础,我有一个班级将学生输入到一个文本文件中,这个班级必须阅读它们,在 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 中显示它们,然后允许用户选择其中一名学生并将其删除。我知道这很多,但我完全迷失了,没有“广泛”的编程知识。
提前谢谢各位。
赛拉。
【问题讨论】:
-
基本上,将文件读入某种列表。操作 Kist,准备好后,将 List 保存回文件。这减少了一些开销和复杂性。我会使用 JList 来显示各个学生,这将允许您选择要操作的学生....
-
问题是伙伴,我不知道该怎么做。我如何将其保存回来?我知道解释起来很麻烦,但我非常需要帮助哈哈。
-
保存(显然)与加载是相同的过程,查看Basic I/O了解更多详情
标签: java user-interface bufferedreader bufferedwriter