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



好的,所以我的程序似乎是非常基本的,我有一个类,输入学生的文本文件,这个类必须读取它们,在GUI中显示它们,以便用户可以pic哪个学生想要,然后一个删除方法,从文本文件中删除学生。问题是,这比我学过的东西要复杂得多,我真的需要帮助,这是我删除学生类的代码:

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 .

首先声明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中,但我将把它留给你。

查看使用JFC/Swing创建GUI和如何使用列表了解更多详细信息

相关内容

  • 没有找到相关文章

最新更新