将JTextArea保存为txt文件 - 最终变量

时间:2011-04-28 15:38:24

标签: java variables save final jtextarea

当我们按下保存按钮时,我需要将用户输入JTextArea的内容保存为txt文件,但是当我尝试编译它时,我不断收到错误消息,希望有人可以提供帮助。这是我的代码:

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

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.*;

class InputRoute extends JFrame
{   
InputRoute()
{
    Container c = getContentPane();
    c.setLayout ( null );

    Color b = new Color(100,200,255); // set colour of JFrame
    c.setBackground( b );

    JLabel title = new JLabel("Input Route"); // new label
    title.setBounds(250, 20, 500, 30); // set position and size
    title.setForeground(Color.BLUE); // set colour of text to blue
    title.setFont(new Font("Time", Font.BOLD, 18)); // change font and size of text
    c.add(title);//adds object

    JLabel todo = new JLabel("Please enter your route below:"); // new label
    todo.setBounds(200, 40, 500, 30); // set position and size
    todo.setForeground(Color.BLUE); // set colour of text to blue
    todo.setFont(new Font("Time", Font.BOLD, 12)); // change font and size of text
    c.add(todo);//adds object

    JLabel eastmid = new JLabel("East Midlands Bus");//creates label
    eastmid.setBounds(245,400,500,30);//label location and size
    eastmid.setForeground(Color.BLUE);//colour of text
    eastmid.setFont(new Font("Time", Font.BOLD, 12));//font of text
    c.add(eastmid);//adds object

    JTextArea inputroute=new JTextArea("");//creates text field to enter route
    inputroute.setBounds(50, 75, 500, 250);//sets location and size
    inputroute.setEditable(true);//makes the field editable
    inputroute.setFont(new Font("Time", Font.PLAIN,12));//sets font of text
    inputroute.setBackground(null);//makes background transparents
    inputroute.setForeground(Color.BLUE);//sets colour of text
    inputroute.setBorder(BorderFactory.createEtchedBorder(3, Color.BLUE, Color.BLUE)); //sets border so text field can be seen

    JScrollPane scrollPane = new JScrollPane(inputroute);
    scrollPane.setBounds(50,75,500,250);

    JButton save = new JButton("SAVE ROUTE");//creates buttons
    save.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent e)
        {
            String fileName = JOptionPane.showInputDialog("Enter file name");//String finalFileName = fileName.getText();
            FileWriter outFile = new FileWriter(fileName +".txt",true);
            outFile.write(inputroute.getText());
            outFile.close();
        }
    });

    JButton exit = new JButton("EXIT");
    exit.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent e)
        {
            dispose();
        }
    });

    exit.setForeground(Color.BLUE);//edits button
    exit.setFont(new Font("Time", Font.BOLD, 12));
    save.setForeground(Color.BLUE);//edits button
    save.setFont(new Font("Time", Font.BOLD, 12));

    c.add(exit);//adds objects
    c.add(save);
    c.add(scrollPane);

    exit.setBounds(250, 375, 90, 30);//sets location of button
    save.setBounds(230,340, 150,30);

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    this.setBounds((int) screenSize.getWidth()/2 - 370, (int) screenSize.getHeight()/2 - 300, 600, 450);  // set position and size
    this.setResizable(false);
    this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    this.setTitle("Admin");
    this.setVisible(true);
    this.setResizable(false);
}
 }

这是我得到的错误:

从内部类中访问本地变量inputroute;需要宣布最终 outFile.write(inputroute.getText());

我在google上看过如何做到这一点,但我没有运气。

4 个答案:

答案 0 :(得分:1)

还使用textArea.write(...)方法保存数据。您当前的代码是错误的,无法在Windows上正常工作。 textArea.getText()方法将返回一个文本字符串,该字符串使用“\ n”表示新行。在Window上,新行字符串为“\ r \ n”。文本区域write(...)方法将为您处理。

答案 1 :(得分:0)

您正在尝试从内部类访问构造函数中声明为local的非final变量。 尝试使inputRoute成为类的字段而不是构造函数中的变量。

答案 2 :(得分:0)

行之前:save.addActionListener(new ActionListener()

位:

final JTextArea myInputroute = inputroute

并在内部类中使用myInputroute

答案 3 :(得分:0)

            FileWriter pw = new FileWriter ("filename.txt");
            txtarea.write(pw); //Object of JTextArea

只需要两个语句就可以将JTextArea的内容写入文件...