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

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

本文介绍了将JTextArea文本保存到一个txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将文本从JTextArea保存到文本文件。当我保存数据时,我的文本文件没有任何内容。我觉得我写错了输出。有没有更好的方法来编码?感谢您的帮助!



  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;
$ b $ 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;
}
尝试
{
BufferedWriter out = new BufferedWriter(new FileWriter(fileName));

out.write(text);
out.close();

catch(Exception e)
{
System.err.println(Error:+ e.getMessage());









主程序

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

public class SaveMain extends JFrame
{

private SaveClass canvas;

private JPanel buttonPanel;
私人JButton btnOne;

$ b public SaveMain()
{
super(将JTextArea文本保存为一个txt文件);
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);

$ b private void buildButtonPanel()
{
buttonPanel = new JPanel();

btnOne = new JButton(Save);

buttonPanel.add(btnOne);


btnOne.addActionListener(new btnOneListener());


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




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


$ b


解决方案

我认为问题在于你在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();

这篇关于将JTextArea文本保存到一个txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 15:31