需要帮助才能完成我的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);
}
}
答案 0 :(得分:5)
if( date!=null && GetDayText( date )!= text.getText() )
你的意思是if( newdate!=null && GetDayText( newdate)!= text.getText() )
不是吗?
当窗口关闭时,如何确保书写文本保存在OnDeleteEvent方法中?
在窗口注册WindowListener
。
编辑:
请注意,对于平等文本,这可能不一定会返回:
newdate==date
这取决于newdate
与date
完全相同的对象(这很可能是因为字符串是不可变的,可能来自JVM的字符串池)。但是,您最好使用(newdate == null && date == null) || (newdate != null && newdate.equals(date))
,即如果两个字符串都为空或者它们具有相同的内容,则它们是相等的。