简体   繁体   English

使用按钮将 JTextArea 保存到 .txt 文件

[英]Saving JTextArea to a .txt file with a button

if I enter text in the JTextArea and click the "Save" button, the JTextArea text should write/save into a .txt file.如果我在 JTextArea 中输入文本并单击“保存”按钮,则 JTextArea 文本应写入/保存到 .txt 文件中。 Is my try & catch in the correct place being in the event handler method or should parts of it be in the constructor?我的 try & catch 是在事件处理程序方法中的正确位置还是它的一部分应该在构造函数中?

This is my code:这是我的代码:

package exercises;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class SimpleNotePadApp extends JFrame implements ActionListener {

JButton button1 = new JButton("Open");
JButton button2 = new JButton("Save");

public SimpleNotePadApp(String title) {
    super(title);                             
    setDefaultCloseOperation(EXIT_ON_CLOSE);  
    setSize(300, 350);                        
    setLayout(null);


    JTextArea newItemArea = new JTextArea();
    newItemArea.setLocation(3, 3);
    newItemArea.setSize(297, 282);
    getContentPane().add(newItemArea);

    button1.setLocation(30,290);  
    button1.setSize(120, 25);
    getContentPane().add(button1);

    button2.setLocation(150,290);  
    button2.setSize(120, 25);
    getContentPane().add(button2);

}

public static void main(String[] args) {
    SimpleNotePadApp frame;

    frame = new SimpleNotePadApp("Text File GUI");      
    frame.setVisible(true);                             
}

public void actionPerformed(ActionEvent e) {

    if(e.getSource() == button1)
    {
        try {
            PrintWriter out = new PrintWriter(new FileWriter("TestFile.txt"));
            newItemArea.getText();
            newItemArea.write(out);
            out.println(newItemArea);
            out.flush();
            out.close();

        } catch (IOException e1) {
            System.err.println("Error occurred");
            e1.printStackTrace();
        }
    }
}
}

Thanks in advance提前致谢

Your try ... catch is in the correct spot, but the contents should just be:你的try ... catch是在正确的位置,但内容应该是:

        PrintWriter out = new PrintWriter(new FileWriter("TestFile.txt"));
        newItemArea.write(out);
        out.close();

Consider using try-with-resources, and the .close() becomes unnecessary:考虑使用 try-with-resources,并且.close()变得不必要:

    try ( PrintWriter out = new PrintWriter(new FileWriter("TestFile.txt")) {
        newItemArea.write(out);
    } catch (IOException e1) {
        System.err.println("Error occurred");
        e1.printStackTrace();
    }

Also, you'll need to attach the ActionListener to the JButton during the construction:此外,您需要在构建过程中将ActionListener附加到JButton

button2.addActionListener(this);

( this is the SimpleNotePadApp instance, which implements ActionListener ) thisSimpleNotePadApp实例,它实现了ActionListener

Finally, you'll want:最后,你会想要:

 if(e.getSource() == button2)

... since button2 is your "Save" button (not button1 ) ...因为button2是您的“保存”按钮(不是button1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM