2017-09-27 10 views
0

JTextAreaにテキストを入力して[保存]ボタンをクリックすると、JTextAreaテキストは.txtファイルに書き込む/保存する必要があります。私の試して&正しい場所にイベントハンドラメソッド内にキャッチするか、またはその部分は、コンストラクタにする必要がありますか?ボタン付きの.txtファイルにJTextAreaを保存する

これは私のコードです:事前に

package exercises; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.PrintWriter; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JTextArea; 

public class SimpleNotePadApp extends JFrame implements ActionListener { 

JButton button1 = new JButton("Open"); 
JButton button2 = new JButton("Save"); 

public SimpleNotePadApp(String title) { 
    super(title);        
    setDefaultCloseOperation(EXIT_ON_CLOSE); 
    setSize(300, 350);       
    setLayout(null); 


    JTextArea newItemArea = new JTextArea(); 
    newItemArea.setLocation(3, 3); 
    newItemArea.setSize(297, 282); 
    getContentPane().add(newItemArea); 

    button1.setLocation(30,290); 
    button1.setSize(120, 25); 
    getContentPane().add(button1); 

    button2.setLocation(150,290); 
    button2.setSize(120, 25); 
    getContentPane().add(button2); 

} 

public static void main(String[] args) { 
    SimpleNotePadApp frame; 

    frame = new SimpleNotePadApp("Text File GUI");  
    frame.setVisible(true);        
} 

public void actionPerformed(ActionEvent e) { 

    if(e.getSource() == button1) 
    { 
     try { 
      PrintWriter out = new PrintWriter(new FileWriter("TestFile.txt")); 
      newItemArea.getText(); 
      newItemArea.write(out); 
      out.println(newItemArea); 
      out.flush(); 
      out.close(); 

     } catch (IOException e1) { 
      System.err.println("Error occurred"); 
      e1.printStackTrace(); 
     } 
    } 
} 
} 

おかげ

+3

'私の試して正しい場所にキャッチしていますか? ' - なぜそれがコンストラクタになるのですか?コンストラクタからコードを実行しますか?また、JTextArea.write(...)メソッドを使用して、データをファイルに保存します。 – camickr

+0

Java GUIは、異なるロケールの異なるPLAFを使用して、異なるOS、画面サイズ、画面解像度などで動作する必要があります。したがって、ピクセルの完全なレイアウトには役立ちません。代わりに、レイアウトマネージャや[それらの組み合わせ](http://stackoverflow.com/a/5630271/418556)と[空白](http://stackoverflow.com/a/17874718/)のレイアウトパディングとボーダーを使用してください。 418556)。 –

答えて

0

をごtry ... catchは正しい場所にありますが、中身はただのようになります。

 PrintWriter out = new PrintWriter(new FileWriter("TestFile.txt")); 
     newItemArea.write(out); 
     out.close(); 

はトライして使用することを検討してください-resources、.close()は不要になります。

try (PrintWriter out = new PrintWriter(new FileWriter("TestFile.txt")) { 
     newItemArea.write(out); 
    } catch (IOException e1) { 
     System.err.println("Error occurred"); 
     e1.printStackTrace(); 
    } 

また、あなたが工事中JButtonActionListenerを添付する必要があります:

button2.addActionListener(this); 

thisがあるSimpleNotePadApp例えば、ActionListener実装)

最後に、あなたがお勧めします:

if(e.getSource() == button2) 

... button2はあなたの[保存]ボタンです(button1ではない)

+0

あなたの助けてくれてありがとう!しかし、なんらかの理由でまだテキストファイルに保存されていません。私はコンソールに何かを印刷してテストしているので、間違いなく "試してみる"部分になっています... – Caiz

+0

テキストファイルに書き込んでいないのですか?私は、デバッグのために質問のコードにいくつかの変更を加えることをお勧めします。例えば。 'PrintWriter out = new PrintWriter(新しいFileWriter(" TestFile.txt "));' 'ファイルf =新しいファイル(" TestFile.txt "); PrintWriter out =新しいPrintWriter(新しいFileWriter(f)); 'その後、保存後に' Desktop.getDesktop()。open(f); 'を実行します。 –

関連する問題