2014-09-24 99 views
-1

我正在製作一個程序,需要保存用戶輸入。所以我想知道如何將JTextArea保存爲文本文件,以及何時關閉並重新打開程序文本仍然在JTextArea中。JTextArea保存到txt

也對不起我的語法錯誤。

package main; 


import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextArea; 
import javax.swing.SwingUtilities; 
import javax.swing.border.EtchedBorder; 

public class main extends JFrame { 

JLabel statusbar; 

public main() { 

initUI(); 
} 

public final void initUI() { 

JPanel panel = new JPanel(); 
statusbar = new JLabel(""); 

statusbar.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED)); 

panel.setLayout(null); 

JTextArea area1; 

area1 = new JTextArea(90, 25); 
area1.setBounds(20, 20, 200, 25); 
area1.setBackground(Color.white); 
area1.setForeground(Color.BLACK); 
area1.setText(""); 
panel.add(area1); 


add(panel); 
add(statusbar, BorderLayout.SOUTH); 

setTitle("Viskis"); 
setSize(300, 200); 
setLocationRelativeTo(null); 
setDefaultCloseOperation(EXIT_ON_CLOSE); 

} 
class ButtonListener implements ActionListener { 

public void actionPerformed(ActionEvent e) { 

JButton o = (JButton) e.getSource(); 
String label = o.getText(); 
statusbar.setText(""); 

} } 

public static void main(String[] args) { 

SwingUtilities.invokeLater(new Runnable() { 

public void run() { 

main ms = new main(); 
ms.setVisible(true); 

new main(); 

} 
}); 
} 
} 
+0

在問你的問題之前,你是否研究過關於文件的問題? – Dici 2014-09-24 16:44:13

回答

0

以下是幫助您讀取和寫入文件的一些幫助程序方法。請在JavaDoc中查找getText()setText()以獲取和設置JTextArea的文本。

我建議reading the SwingWorker tutorials,並使用一個SwingWorker的這些方法,以免你鎖定你的GUI,同時節省/讀取文件

/** 
    * Writes a String text to a File specified by filename 
    * 
    * @param filename The filename/path of the File we would like to write the text to 
    * @param text  The text to write to the File 
    * 
    */ 
public static void writeToFile(String filename, String text) { 

    BufferedWriter writer = null; // This could go in a try-with-resources if you wanna get fancy 

    try { 
     writer = new BufferedWriter(new FileWriter(new File(filename))); // Open a File for writing 
     writer.write(text); // write the text to the file 
    } catch (IOException e) { 

    /* We could not open the File for writing, or could not write to the File */ 

    } finally { 
     try { 
      if (writer != null) { 
       writer.close(); // we are done writing to the File, close the connection 
      } 
     } catch (IOException e) { 

     /* We could not close the connection to the File */ 

     } 
    } 
} 

/** 
    * Reads all lines from a File specified by filename 
    * 
    * @param filename The filename/path of the File we would like to read text from 
    * 
    * @return   An list of Strings containing each line of the File 
    */ 
public static List<String> readFromFile(String filename) { 

    BufferedReader reader = null; // This could go in a try-with-resources if you wanna get fancy 
    List<String> lines = new ArrayList<String>(); 

    try { 

     reader = new BufferedReader(new FileReader(new File(filename))); // Open a File for writing 
     while ((line = reader.readLine()) != null) { 
      lines.add(line); 
     } 

    } catch (IOException e) { 

    /* We could not open the File for reading, or could not read from the File */ 

    } finally { 
     try { 
      if (reader != null) { 
       reader.close(); // we are done reading from the File, close the connection 
      } 
     } catch (IOException e) { 

     /* We could not close the connection to the File */ 

     } 
    } 

    return lines; 
} 
0

此代碼工作對我來說與「南」作爲當前的日期和「名'在jTextField中的輸入。

try { 

     con=Connect.ConnectDB();   
     pst.execute(); 
     Date date=new Date(); 
     SimpleDateFormat sd=new SimpleDateFormat("dd-mm-yyyy-h-m-s"); 
     String nam= sd.format(date); 
     String name=CaseNoField.getText(); 
     CaseNoField.setText(""); 

     FileWriter writer=new FileWriter("C:\\path"+name+"("+nam+")"+".txt");  
     BufferedWriter bw=new BufferedWriter(writer); 
     JCaseArea.write(bw); 
     bw.close(); 
     JCaseArea.setText(""); 
     JCaseArea.requestFocus(); 

      JOptionPane.showMessageDialog(null, "Case Saved"); 
    } 
    catch(Exception e){ 

     JOptionPane.showMessageDialog(null, e); 


    } 
0

不推薦在擺動線上寫入和讀取文件。使用mvc模式。創建一個綁定了字段的數據模型。創建一個適配器來更新模型,並寫入文件 那麼你的組件將始終與你的模型

使用模型最新的寫數據(使用適配器)和讀取文件將與適配器

對於更新你的模型例如,Focus監聽器會調用適配器來完成任務。或者,如果修改Object,則可以使用Obseevee模式。