且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将JTextArea文本保存到一个txt文件

更新时间:2023-12-02 19:20:34

我认为问题在于你在Main类中创建了一个SaveClass的实例,但是在Save方法中,你在SaveClass中创建了另一个实例,并且你从那个实例中读取了文本。所以你可能想这样做的Save()方法:

$ $ p $ 删除SaveClass sa = new SaveClass();

然后:

  String text = this.getText1(); 


I'm having trouble saving text from a JTextArea to a text file. When I save the data my text file has nothing in it. I feel like I'm writing to the output wrong. Is there a better way to code this? Thanks for the help!

The class for the program

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;

public class SaveClass extends JPanel
{
    JPanel cards;
    private JPanel card1;
    private JTextArea textarea1;
    private JFileChooser fc;

    public SaveClass()
    {
        Font mono = new Font("Monospaced", Font.PLAIN, 12);

        textarea1 = new JTextArea();
        textarea1.setFont(mono);



        card1 = new JPanel();
        card1.add(textarea1);



        cards = new JPanel(new CardLayout());
        cards.add(card1, "1");

        add(cards, BorderLayout.CENTER);



        setBorder(BorderFactory.createTitledBorder("Text here"));
        setFont(mono);
    }

    public String getText1()
    {
        return this.textarea1.getText();
    }

    public void Save()
    {
        SaveClass sa = new SaveClass();
        String text = sa.getText1();

        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory( new File( "./") );
        int actionDialog = chooser.showSaveDialog(this);
        if (actionDialog == JFileChooser.APPROVE_OPTION)
        {
            File fileName = new File(chooser.getSelectedFile( ) + "" );
            if(fileName == null)
                return;
            if(fileName.exists())
            {
                actionDialog = JOptionPane.showConfirmDialog(this,
                                   "Replace existing file?");
                if (actionDialog == JOptionPane.NO_OPTION)
                    return;
            }
            try
            {
                BufferedWriter out = new BufferedWriter(new FileWriter(fileName));

                    out.write(text);
                    out.close();
            }
            catch(Exception e)
            {
                 System.err.println("Error: " + e.getMessage());
            }
        }

    }
}

The Main program

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class SaveMain extends JFrame
{

    private SaveClass canvas;

    private JPanel buttonPanel;
    private JButton btnOne;


    public SaveMain()
    {
        super("Save JTextArea text to a txt file");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setLayout(new BorderLayout());

        canvas = new SaveClass();

        buildButtonPanel();

        add(buttonPanel, BorderLayout.SOUTH);
        add(canvas, BorderLayout.CENTER);

        pack();
        setSize(800, 800);
        setVisible(true);

    }
    private void buildButtonPanel()
    {
        buttonPanel = new JPanel();

        btnOne = new JButton("Save");

        buttonPanel.add(btnOne);


        btnOne.addActionListener(new btnOneListener());


    }
    private class btnOneListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if (e.getSource() == btnOne)
            {
                canvas.Save();
            }
        }
    }


    public static void main(String[] args)
    {
        new SaveMain();
    }

}

I think the problem is that you create an instance of SaveClass in the Main class, but in the Save method, that is in the SaveClass you create another instance, and you read the text from that instance. So you might want to do this to the Save() method:

delete the SaveClass sa = new SaveClass(); 

and then:

String text = this.getText1();