使用actionlistener使用格式化程序写入文件(java)时出错

uujelgoq  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(360)

所以我要创建一个程序作为日志,日志对象应该创建一个 CreateFile 对象,将 JTextArea 输入,然后关闭文件。(实际上是保存文件)。它应该在用户按下save按钮时这样做。
现在所发生的一切就是创建一个空白文件。除了写入文件,其他一切都正常。请帮助识别并更正写入我的文件时的错误,下面是我的代码。
这是日记课

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);
    }
}
jutyujz0

jutyujz01#

实现中的主要问题是 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();
        }
    }
}

相关问题