2011-04-28 50 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中的內容轉換成文件...

相關問題