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

时间:2014-03-20 21:05:53

标签: java user-interface bufferedreader bufferedwriter

好的,所以我的程序看起来非常基础,我有一个课程将学生输入到一个文本文件中,这个类必须读取它们,在GUI中显示它们,这样用户就可以想到哪个学生需要,然后一个删除从文本文件中删除学生的方法。问题是,这比我学过的东西更复杂,而且我真的需要帮助,这是我删除学生班的代码:

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中显示它们,然后允许用户选择其中一个学生并将其删除。我知道这很多但我完全迷失了,并且没有“广泛”的编程知识。

先谢谢你们。

Cyla。

1 个答案:

答案 0 :(得分: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并将其添加到您的用户界面,但我会将其留给您。

请查看Creating a GUI With JFC/SwingHow to Use Lists了解详情