2014-09-24 59 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模式。