【问题标题】:Reading a text file (which i already know) and then displaying it in a GUI?读取文本文件(我已经知道)然后在 GUI 中显示它?
【发布时间】: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


【解决方案1】:

首先声明一个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.awt.List 而不是java.util.List
  • 这绝对修复了错误。现在我必须创建另一个 GUI?因为我已经有一个使用按钮和东西的。
猜你喜欢
  • 1970-01-01
  • 2014-08-07
  • 2021-07-21
  • 1970-01-01
  • 2018-01-17
  • 2014-03-16
  • 2016-06-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多