需要帮助来完成我的Java日历。

谁能看到为什么今天日期所写的文字没有保存?

当关闭窗口时,如何确保书面文本保存在OnDeleteEvent方法中?

我的代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Date;
import java.text.SimpleDateFormat;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;

import com.toedter.calendar.JCalendar;

public class Diary extends JPanel
{
    private JCalendar calendar;
    private JTextArea text;
    private SimpleDateFormat sdf;
    private JScrollPane scroll;

    private Value days = null;
    private String date = null;

    public Diary()
    {
        calendar = new JCalendar();
        sdf = new SimpleDateFormat("dd-MM-yyyy");
        text = new JTextArea(5,15);
        scroll = new JScrollPane(text);
        scroll.setVerticalScrollBarPolicy(
                scroll.VERTICAL_SCROLLBAR_ALWAYS);
        scroll.setPreferredSize(new Dimension(100, 100));

        date = GetCalendarDate();
        days = Load();

        calendar.addPropertyChangeListener(new OnDaySelected());
        calendar.addPropertyChangeListener(new OnDeleteEvent());

        setLayout (new BorderLayout());
            add(calendar, BorderLayout.NORTH);
            add(scroll, BorderLayout.SOUTH);
    }

    private static Value Load()
    {
        Value days;
        try
        {
            days = Value.loadFile("Diary.dat");
        }
        catch( Exception e )
        {
            days = Value.makeTable();
        }
        return days;
    }

    private static void Save( Value days )
    {
            try
            {
                Value.saveFile(days,"Diary.dat");
            }
            catch( Exception e)
            {
                Value.makeTable();
            }

    }

    private String GetCalendarDate()
    {
        Date d = calendar.getDate();
        String dat = sdf.format(d);
                return dat;
    }

    private String GetDayText( String date )
    {
              try
              {
                  Value v = Value.makeString(date);
                  String texti = days.get(v).asString();
                  return texti;
              }
              catch( Exception e)
              {
                  return "";
              }
    }

    private void SetDayText( String date, String text )
    {
        Value v = Value.makeString(date);
                if(text == null)
                    days.put(v,null);
                else
                    days.put(v, Value.makeString(text));

    }

    public void SetDate( String newdate )
    {
        if( newdate==date ) return;
        if( date!=null && GetDayText( date )!= text.getText() )
        {
            SetDayText(date,text.getText());
            Save(days);
        }
        date = newdate;
        text.setText(GetDayText(date));
    }

    private class OnDaySelected implements java.beans.PropertyChangeListener
    {
        public void propertyChange(PropertyChangeEvent evt)
        {
            SetDate(GetCalendarDate());
        }
    }

    private class OnDeleteEvent implements java.beans.PropertyChangeListener
    {
        public void propertyChange(PropertyChangeEvent evt)
        {
            if(GetDayText(date) != text.getText())
            {
                SetDayText(date, text.getText());
                Save(days);
            }
            // how to close the program and ensure that unsaved text is saved
            // before the application closed?


        }
    }

    public static void main(String[] args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) {}

        JFrame frame = new JFrame("Diary");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new Diary());
        frame.pack();
        frame.setVisible(true);
    }
}

最佳答案

if( date!=null && GetDayText( date )!= text.getText() )

您是说if( newdate!=null && GetDayText( newdate)!= text.getText() )是吗?


  当关闭窗口时,如何确保书面文本保存在OnDeleteEvent方法中?


在窗口上注册WindowListener

编辑:

请注意,对于相等的文本,这可能不一定返回true:
newdate==date
 这取决于newdate是与date完全相同的对象(这很有可能是因为字符串是不可变的,并且可能来自JVM的字符串池)。但是,最好使用(newdate == null && date == null) || (newdate != null && newdate.equals(date)),即,如果两个字符串均为null或它们具有相同的内容,则它们相等。

10-06 05:30