如何使用printwriter将Jtextarea中的文本传输到文本文件?

时间:2015-11-27 15:07:52

标签: java textarea jtextarea printwriter

如何使用Printwriter将文本文本文件从文本区域传输到文本文件?

我在哪里放置代码:Printwriter f=new Printwriter("")?如果方法具有函数:System.out.println,如何将方法打印到Jtextarea?

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;
    public class ReliefFrame extends JFrame
    {
    ......

    }

    private void createNorthPanel()
    {
        .....
    }
    private void createEastPanel()
    {
        .....
    }
    private void createWestPanel()
    {
        .....
    }
    private void createSouthPanel()
    {
        ......
    }

    private void createaTextField()
    {
        .....
    }
    private void createTextArea()
    {
        ......
    }   
    private void createCButton()
    {
        button[2] = new JButton( "Release Packs" );
        class ButtonListener implements ActionListener
        { 
            public void actionPerformed( ActionEvent ae )
            {
                String a = bTextField.getText();
                int b=Integer.parseInt(a);
                s.releasePacks(b);
                bTextField.setText( "" );           
            }
        }
        button[2].addActionListener( new ButtonListener() );
        rowwww.add( button[2] ); 
    }
    private void createEButton()
    {
        button[4] = new JButton( "Inventory Report" );
        class ButtonListener implements ActionListener
        { 
            public void actionPerformed( ActionEvent ae )
            {

                aTextArea.append("===INVENTORY===\n");
                s.printInventory();
                aTextArea.append("===============\n");

            }
        }
        button[4].addActionListener( new ButtonListener() );
        rowwww.add( button[4] ); 
    }
    public static void main(String[] arguments) {
    ReliefFrame c = new ReliefFrame();
    }

  }

2 个答案:

答案 0 :(得分:0)

有很多方法可以完成这类事情,但是你需要通过弄清楚你希望用户做什么来解决它来决定你想要放置代码的位置。可以选择JButton或菜单选择。这是你需要决定的事情。让我们说一个JButton,因为你已经创建了一些。

创建另一个按钮并为按钮文本输入单词“保存”,然后确保它具有 actionPerformed 事件的监听器,就像您使用其他按钮一样,然后将下面的代码放入其中。它应该看起来像:

public void actionPerformed( ActionEvent evt )
    try {
        // Be sure to supply the path and file name you want.
        PrintWriter printwriter = new PrintWriter("c:\\temp\\MyTextFile.txt");

        String[] lines = yourJTextArea.getText().split("\\n");
        for(int i = 0 ; i < lines.length; i++) {
            printwriter.println(lines[i]);
        }
        printwriter.close();
    } 
    catch (IOException e) {
        //Do something with caught exception.
    }
}

现在,下面的问题我不太明白。你需要用更明确的细节来解释,但我认为你想说的是:

你的问题:

  

如果方法有一个函数:System.out.println,我该如何放置   该方法将打印到Jtextarea?

我认为这意味着:

  

“嘿,我得到了一个我想从网上使用的方法,但是我   不能为我的生活弄清楚如何从中得到结果   方法进入我的JTextArea控件。现在该方法吐出了   方法结果输出到输出控制台(或窗格)。“

如果确实如此,那么您可以将 System.out.println(); 方法替换为:

  

yourJTextArea.append(该方法结果字符串);

我希望这对你有所帮助。

答案 1 :(得分:0)

使用data1 <- read.table("readStats.txt", header=T) data1.long <- melt(data1) ggplot(data1.long[1:36,], aes(data1.long$Sample[1:36],y=data1.long$value[1:36], fill=data1.long$variable[1:36])) + geom_bar(stat="identity", width=0.5, position="dodge") API提供的write(...)方法:

JTextArea