2016-03-27 9 views
0

ジャーナルとして機能するプログラムを作成しています.JournalオブジェクトはCreateFileオブジェクトを作成して、ファイルを作成し、そのテキストを書き込みますJTextAreaエントリを開き、ファイルを閉じます(ファイルを保存することをお勧めします)。これは、ユーザーが保存ボタンを押すたびに実行されるはずです。ActionListenerを使用してFormatterでファイルに書き込む際にエラーが発生しました

今のところ空白のファイルが作成されます。ファイルへの書き込みを除くすべてが機能しています。私のファイルに書いている間違いを特定して訂正するのを助けてください。私のコードは以下の通りです。ここで

はここジャーナルクラス

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.FileWriter; 
import java.io.PrintWriter; 
import java.util.Formatter; 

import javax.swing.*; 
public class Journal extends JFrame{ 
private JTextField date; 
public JTextArea entry; 
private JButton button; 
public static String day, month, year; 
private Formatter formatter; 
     public Journal(String month, String day, String year){ 
      this.day=day; 
      this.month=month; 
      this.year=year; 
     //Use parameter so display a date 
      String theDate = month + "/ "+day+"/ "+year; 
      Font theFont = new Font("Serif",Font.BOLD,20); 
     setLayout(new BorderLayout()); 
     date = new JTextField(theDate+ "  "+"Journal Entry"); 
     date.setFont(theFont); 
     date.setSize(new Dimension(500,50)); 
     date.setEditable(false); 
     //Create a save Button 
     button = new JButton("Save Entry"); 

     add(button,BorderLayout.WEST); 
     //Create a place to write the journal entry 
     entry = new JTextArea("Enter your entry here"); 
     entry.setLineWrap(true); 
     Font JTFFont = new Font("Serif",Font.PLAIN,14); 
     entry.setFont(JTFFont); 
     Handlerclass handler = new Handlerclass(); 
     button.addActionListener(handler); 
     add(date,BorderLayout.NORTH); 
     add(entry,BorderLayout.CENTER); 
     } 
private class Handlerclass implements ActionListener{ 

    @Override 
    public void actionPerformed(ActionEvent e) { 

     try { 

     CreateFile cf = new CreateFile(month,day,year); 
     cf.openFile(); 
     cf.addRecords(); 
     cf.closeFile(); 



     } 
     catch(Exception error){ 
      System.out.println("You have an error"); 
     } 
    } 

    } 
    public void closeFile(){ 
     formatter.close(); 
    } 
} 

でのCreateFileクラスは、ここで

import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
import java.io.*; 
import java.lang.*; 
import java.util.*; 
public class CreateFile extends Journal{ 

    public CreateFile(String month, String day, String year) { 
     super(month, day, year); 
     // TODO Auto-generated constructor stub 
    } 

    private Formatter x; 

    public void openFile(){ 
     try{ 
      String date = String.format("%s_%s_%s.txt", this.month, this.day, this.year); 
      x = new Formatter(date); 
     } 
     catch(Exception e) 
     { 

      System.out.println("you have an error"); 
    } 
    } 
    public void closeFile(){ 
     x.close(); 
    } 
    public void addRecords(){ 

     entry.addKeyListener(
       new KeyListener(){ 

        @Override 
        public void keyTyped(KeyEvent e) { 
         // TODO Auto-generated method stub 
         x.format(entry.getText()); 
        } 

        @Override 
        public void keyPressed(KeyEvent e) {} 

        @Override 
        public void keyReleased(KeyEvent e) {} 
       } 
       ); 

    } 
} 

でメインクラスが実装における主な問題があること、である

import java.awt.Color; 

import javax.swing.*; 

public class MainClass { 
    public static void main(String args[]){ 
     //Create new Journal Entry 
Journal j = new Journal("3","27","2016"); 
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
j.setVisible(true); 
j.setSize(500, 500); 
j.setResizable(false); 
j.getContentPane().setBackground(Color.WHITE); 
    } 
} 
+0

は方法は何であるあなたのファイルへの実際の作家? –

+0

@ PM77-1、KeyPressed(KeyEventイベント)にはx.format(entry.getText());があります。これは、少なくともJTextAreaのエントリにあるものをファイルに書き込むようにフォーマッタxに指示します。 –

+1

「フォーマッタ」とは何ですか? 'ジャーナル'? 'Formatter.format()'は何をしますか? – EJP

答えて

1

ですメソッドCreateFile::addRecordsはファイルに書き込みません。 KeyListenerのみ登録されています。その後、Formatterはすぐに閉じられます。現在は、すべてのキーを押すたびにファイルに書き込もうとしましたが、Formatterが閉じられているため、これはできません。

その他のアドバイス:Stacktrace(err.printStackTrace)を例外のキャッチに印刷する方がはるかに優れています。だからあなたは何が間違っているのか、そしてどこでどこにあるのかを知ることができます。

次のポイント():場合によってはStrategy Patternseparate concernsを使用し、サブクラスを避ける方がよい場合もあります。ここで

あなたの問題が修正されており、他の点が尊重されている実装は:

import java.awt.*; 
 
import java.io.FileNotFoundException; 
 
import javax.swing.*; 
 
public class Journal extends JFrame{ 
 
    public Journal(String month, String day, String year){ 
 
     String theDate = month + "/ "+day+"/ "+year; 
 
     Font theFont = new Font("Serif",Font.BOLD,20); 
 
     setLayout(new BorderLayout()); 
 
     JTextField dateField = new JTextField(theDate+ "  "+"Journal Entry"); 
 
     dateField.setFont(theFont); 
 
     dateField.setSize(new Dimension(500,50)); 
 
     dateField.setEditable(false); 
 
     JButton button = new JButton("Save Entry"); 
 
     JTextArea entry = new JTextArea("Enter your entry here"); 
 
     entry.setLineWrap(true); 
 
     Font JTFFont = new Font("Serif",Font.PLAIN,14); 
 
     entry.setFont(JTFFont); 
 
     String date = String.format("%s_%s_%s.txt", month, day, year); 
 
     try { 
 
      button.addActionListener(new SaveHandler(date, entry)); 
 
     } catch (FileNotFoundException e) { 
 
      e.printStackTrace(); 
 
     } 
 
     add(button,BorderLayout.WEST); 
 
     add(dateField,BorderLayout.NORTH); 
 
     add(entry,BorderLayout.CENTER); 
 
    } 
 
}

import javax.swing.*; 
 
import java.awt.event.ActionEvent; 
 
import java.awt.event.ActionListener; 
 
import java.io.FileNotFoundException; 
 
import java.util.Formatter; 
 

 
class SaveHandler implements ActionListener { 
 

 
    private String date; 
 
    private JTextArea entry; 
 
    public SaveHandler(String date, JTextArea entry) throws FileNotFoundException { 
 
     this.date = date; 
 
     this.entry = entry; 
 
    } 
 

 
    @Override 
 
    public void actionPerformed(ActionEvent e) { 
 
     try { 
 
      new Formatter(date).format(entry.getText()).close(); 
 
     } catch (FileNotFoundException err) { 
 
      err.printStackTrace(); 
 
     } 
 
    } 
 
}

関連する問題