2011-04-28 51 views
-1

我需要保存用户输入到JTextArea作为txt文件,当他们按下保存按钮但我不断收到错误信息,当我尝试编译它,希望有人可以帮帮我。这里是我的代码:保存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从内部类访问;需要被宣布为final outFile.write(inputroute.getText());

我已经看过谷歌如何做到这一点,但我没有运气。

+4

退出跨页! http://www.java-forums.org/awt-swing/43189-saving-jtextarea-txt-file.html – camickr 2011-04-28 15:56:56

回答

0

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

0

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

地方:

final JTextArea myInputroute = inputroute 

并使用myInputroute内部类的内部。

1

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

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

你只需要两个语句写JTextArea中的内容转换成文件...

相关问题