[英]Error in writing to file(JAVA) with Formatter using ActionListener
因此,我正在創建一個用作日記的程序,該Journal對象應該創建一個CreateFile
對象,該對象將創建一個文件,將JTextArea
條目中的文本寫入其中,然后關閉該文件。(基本上是保存文件) 。 每當用戶按下保存按鈕時,都應該這樣做。
現在,所有發生的事情都會創建一個空白文件。 除寫入文件外,其他所有操作均正常。 請幫助識別並糾正寫入我的文件中的錯誤,以下是我的代碼。
這是期刊課
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);
}
}
在實現中的主要問題是,方法CreateFile::addRecords
不寫入文件。 僅注冊KeyListener
。 之后, Formatter
程序立即關閉。 現在嘗試在每次按鍵時寫入文件,但這是不可能的,因為Formatter
已關閉。
附加建議:最好在出現異常的情況下打印Stacktrace( err.printStackTrace
)。 這樣您就可以找出問題所在和出處。
第二點( CreateFile
擴展Journal
):有時最好使用策略模式來分離關注點並避免子類。
這是一個解決了您的問題並尊重其他要點的實現:
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(); } } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.