【问题标题】:Saving JTextArea to a .txt file with a button使用按钮将 JTextArea 保存到 .txt 文件
【发布时间】:2017-09-27 02:02:15
【问题描述】:

如果我在 JTextArea 中输入文本并单击“保存”按钮,则 JTextArea 文本应写入/保存到 .txt 文件中。我的 try & catch 是在事件处理程序方法中的正确位置还是应该在构造函数中的一部分?

这是我的代码:

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();
        }
    }
}
}

提前致谢

【问题讨论】:

  • Is my try & catch in the correct place... - 为什么会在构造函数中?您是否从构造函数执行代码?另外,使用 JTextArea.write(...) 方法将数据保存到文件中。
  • Java GUI 必须在不同的操作系统、屏幕尺寸、屏幕分辨率等上使用不同的语言环境中的不同 PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或 combinations of them 以及 white space 的布局填充和边框。

标签: java swing user-interface event-handling jtextarea


【解决方案1】:

您的try ... catch 位置正确,但内容应该是:

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

考虑使用 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();
    }

另外,您需要在构建过程中将ActionListener 附加到JButton

button2.addActionListener(this);

thisSimpleNotePadApp 实例,它实现了ActionListener

最后,你会想要:

 if(e.getSource() == button2)

...因为button2 是您的“保存”按钮(不是button1

【讨论】:

  • 感谢您的帮助!但是,由于某种原因,它仍然没有保存到文本文件中。它肯定已经通过了“尝试”部分,因为我已经通过在控制台上打印一些东西来测试它......
  • 你确定它没有写入文本文件吗?我建议对问题中的代码进行一些更改以进行调试。例如。将PrintWriter out = new PrintWriter(new FileWriter("TestFile.txt")); 更改为File f = new File("TestFile.txt"); PrintWriter out = new PrintWriter(new FileWriter(f)); 然后稍后(保存后),执行Desktop.getDesktop().open(f);
猜你喜欢
  • 2012-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多