如何将JTextArea文本写入文件并使用ArrayList读取Java中的输入?

时间:2019-04-30 16:47:38

标签: java file user-interface arraylist jtextarea

我有一个名为“ Note”的类,其中String字段的主题,标题和文本(无限制)。我有一个类型为“ Note”的名为“ notes”的ArrayList。我想用JOptionPane的用户输入填充字符串并将其保存到notes。在另一个名为“ Java Frame”的类中,我定义了一个JTextArea对象。我想做的是通过JOptionPane从用户那里获取主题和标题以及JTextArea中的文本(由于通过JTextField仅读取一行的限制),然后将用户的输入(使用fileInputStream)写入文本文件并能够阅读每个笔记(用于搜索或删除)。有人可以帮我吗?非常感谢。我是GUI的新手,请分享您的答案。

这是我到目前为止写的:

公共类JavaFrame扩展Note实现ActionListener {

public class JavaFrame extends Note implements ActionListener {

    JTextArea textArea = new JTextArea();

    public void actionPerformed(ActionEvent e) {
        if (e.getSource.equals(newFile)) {
            this.subject = JOptionPane.showInputDialog("Enter The Note's Subject, Please: ");
            notes.add(this);

            this.title = JOptionPane.showInputDialog("Enter The Note's Title, Please: ");
            notes.add(this);

            this.date = getCurrentDateTime();
            notes.add(this);

            @SuppressWarnings("resource")
            Scanner in = new Scanner(new BufferedInputStream(System.in));
            String txt = "";
            String input;

            while (in.hasNextLine()) {
                input = in.nextLine();
                if (input.isEmpty()) {
                    break;
                }
                txt += input + "\n";
            }
            this.text = txt;
            for (Note n : notes) {
                textArea.append(n + "\n");
            }
        }
    }
}

0 个答案:

没有答案