用Java实现日历记事本源代码2660新版.docx
《用Java实现日历记事本源代码2660新版.docx》由会员分享,可在线阅读,更多相关《用Java实现日历记事本源代码2660新版.docx(20页珍藏版)》请在冰豆网上搜索。
用Java实现日历记事本源代码2660新版
CalendarPad类
importjava.util.Calendar;
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.io.*;
importjava.util.Hashtable;
publicclassCalendarPadextendsJFrameimplementsMouseListener
{
intyear,month,day;
Hashtablehashtable;
Filefile;
JTextFieldshowDay[];
JLabeltitle[];
Calendar日历;
int星期几;
NotePadnotepad=null;
Month负责改变月;
Year负责改变年;
String星期[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
JPanelleftPanel,rightPanel;
publicCalendarPad(intyear,intmonth,intday)
{
leftPanel=newJPanel();
JPanelleftCenter=newJPanel();
JPanelleftNorth=newJPanel();
leftCenter.setLayout(newGridLayout(7,7));
rightPanel=newJPanel();
this.year=year;
this.month=month;
this.day=day;
负责改变年=newYear(this);
负责改变年.setYear(year);
负责改变月=newMonth(this);
负责改变月.setMonth(month);
title=newJLabel[7];
showDay=newJTextField[42];
for(intj=0;j<7;j++)
{
title[j]=newJLabel();
title[j].setText(星期[j]);
title[j].setBorder(BorderFactory.createRaisedBevelBorder());
leftCenter.add(title[j]);
}
title[0].setForeground(Color.red);
title[6].setForeground(Color.blue);
for(inti=0;i<42;i++)
{
showDay[i]=newJTextField();
showDay[i].addMouseListener(this);
showDay[i].setEditable(false);
leftCenter.add(showDay[i]);
}
日历=Calendar.getInstance();
Boxbox=Box.createHorizontalBox();
box.add(负责改变年);
box.add(负责改变月);
leftNorth.add(box);
leftPanel.setLayout(newBorderLayout());
leftPanel.add(leftNorth,BorderLayout.NORTH);
leftPanel.add(leftCenter,BorderLayout.CENTER);
leftPanel.add(newLabel("请在年份输入框输入所查年份(负数表示公元前),并回车确定"),
~6/25~
BorderLayout.SOUTH);
leftPanel.validate();
Containercon=getContentPane();
JSplitPanesplit=newJSplitPane(JSplitPane.HORIZONTAL_SPLIT,
leftPanel,rightPanel);
con.add(split,BorderLayout.CENTER);
con.validate();
hashtable=newHashtable();
file=newFile("日历记事本.txt");
if(!
file.exists())
{
try{
FileOutputStreamout=newFileOutputStream(file);
ObjectOutputStreamobjectOut=newObjectOutputStream(out);
objectOut.writeObject(hashtable);
objectOut.close();
out.close();
}
catch(IOExceptione)
{
}
}
notepad=newNotePad(this);
rightPanel.add(notepad);
设置日历牌(year,month);
~7/25~
addWindowListener(newWindowAdapter()
{publicvoidwindowClosing(WindowEvente)
{
System.exit(0);
}
});
setVisible(true);
setBounds(100,50,524,285);
validate();
}
publicvoid设置日历牌(intyear,intmonth)
{
日历.set(year,month-1,1);
星期几=日历.get(Calendar.DAY_OF_WEEK)-1;
if(month==1||month==2||month==3||month==5||month==7
||month==8||month==10||month==12)
{
排列号码(星期几,31);
}
elseif(month==4||month==6||month==9||month==11)
{
排列号码(星期几,30);
}
elseif(month==2)
{
if((year%4==0&&year%100!
=0)||(year%400==0))
{
排列号码(星期几,29);
}
~8/25~
else
{
排列号码(星期几,28);
}
}
}
publicvoid排列号码(int星期几,int月天数)
{
for(inti=星期几,n=1;i<星期几+月天数;i++)
{
showDay[i].setText(""+n);
if(n==day)
{
showDay[i].setForeground(Color.green);
showDay[i].setFont(newFont("TimesRoman",Font.BOLD,20));
}
else
{
showDay[i].setFont(newFont("TimesRoman",Font.BOLD,12));
showDay[i].setForeground(Color.black);
}
if(i%7==6)
{
showDay[i].setForeground(Color.blue);
}
if(i%7==0)
{
showDay[i].setForeground(Color.red);
~9/25~
}
n++;
}
for(inti=0;i<星期几;i++)
{
showDay[i].setText("");
}
for(inti=星期几+月天数;i<42;i++)
{
showDay[i].setText("");
}
}
publicintgetYear()
{
returnyear;
}
publicvoidsetYear(inty)
{
year=y;
notepad.setYear(year);
}
publicintgetMonth()
{
returnmonth;
}
publicvoidsetMonth(intm)
{
month=m;
notepad.setMonth(month);
}
publicintgetDay()
{
returnday;
}
publicvoidsetDay(intd)
{
day=d;
notepad.setDay(day);
}
publicHashtablegetHashtable()
{
returnhashtable;
}
publicFilegetFile()
{
returnfile;
}
publicvoidmousePressed(MouseEvente)
{
JTextFieldsource=(JTextField)e.getSource();
try{
day=Integer.parseInt(source.getText());
notepad.setDay(day);
notepad.设置信息条(year,month,day);
notepad.设置文本区(null);
notepad.获取日志内容(year,month,day);
}
catch(Exceptionee)
{
}
}
publicvoidmouseClicked(MouseEvente)
{
}
publicvoidmouseReleased(MouseEvente)
{
}
publicvoidmouseEntered(MouseEvente)
{
}
publicvoidmouseExited(MouseEvente)
{
}
publicstaticvoidmain(Stringargs[])
{
Calendarcalendar=Calendar.getInstance();
inty=calendar.get(Calendar.YEAR);
intm=calendar.get(Calendar.MONTH)+1;
intd=calendar.get(Calendar.DAY_OF_MONTH);
newCalendarPad(y,m,d);
}
}
2、)Month类
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
~11/25~
publicclassMonthextendsBoximplementsActionListener
{
intmonth;
JTextFieldshowMonth=null;
JButton下月,上月;
CalendarPad日历;
publicMonth(CalendarPad日历)
{
super(BoxLayout.X_AXIS);
this.日历=日历;
showMonth=newJTextField
(2);
month=日历.getMonth();
showMonth.setEditable(false);
showMonth.setForeground(Color.blue);
showMonth.setFont(newFont("TimesRomn",Font.BOLD,16));
下月=newJButton("下月");
上月=newJButton("上月");
add(上月);
add(showMonth);
add(下月);
上月.addActionListener(this);
下月.addActionListener(this);
showMonth.setText(""+month);
}
publicvoidsetMonth(intmonth)
{
if(month<=12&&month>=1)
{
this.month=month;
}
else
{
this.month=1;
}
showMonth.setText(""+month);
~12/25~
}
publicintgetMonth()
{
returnmonth;
}
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource()==上月)
{
if(month>=2)
{
month=month-1;
日历.setMonth(month);
日历.设置日历牌(日历.getYear(),month);
}
elseif(month==1)
{
month=12;
日历.setMonth(month);
日历.设置日历牌(日历.getYear(),month);
}
showMonth.setText(""+month);
}
elseif(e.getSource()==下月)
{
if(month<12)
{
month=month+1;
日历.setMonth(month);
日历.设置日历牌(日历.getYear(),month);
}
elseif(month==12)
{
month=1;
日历.setMonth(month);
日历.设置日历牌(日历.getYear(),month);
}
showMonth.setText(""+month);
}
~13/25~
}
}
3、)NotePad类
importjava.awt.*;
importjava.awt.event.*;
importjava.util.*;
importjavax.swing.*;
importjavax.swing.event.*;
importjava.io.*;
publicclassNotePadextendsJPanelimplementsActionListener
{
JTextAreatext;
JButton保存日志,删除日志;
Hashtabletable;
JLabel信息条;
intyear,month,day;
Filefile;
CalendarPadcalendar;
publicNotePad(CalendarPadcalendar)
{
this.calendar=calendar;
year=calendar.getYear();
month=calendar.getMonth();
day=calendar.getDay();;
table=calendar.getHashtable();
file=calendar.getFile();
信息条=newJLabel(""+year+"年"+month+"月"+day+"日",JLabel.CENTER);
信息条.setFont(newFont("TimesRoman",Font.BOLD,16));
信息条.setForeground(Color.blue);
text=newJTextArea(10,10);
~14/25~
保存日志=newJButton("保存日志");
删除日志=newJButton("删除日志");
保存日志.addActionListener(this);
删除日志.addActionListener(this);
setLayout(newBorderLayout());
JPanelpSouth=newJPanel();
add(信息条,BorderLayout.NORTH);
pSouth.add(保存日志);
pSouth.add(删除日志);
add(pSouth,BorderLayout.SOUTH);
add(newJScrollPane(text),BorderLayout.CENTER);
}
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource()==保存日志)
{
保存日志(year,month,day);
}
elseif(e.getSource()==删除日志)
{
删除日志(year,month,day);
}
}
publicvoidsetYear(intyear)
{
this.year=year;
}
publicintgetYear()
{
returnyear;
}
publicvoidsetMonth(intmonth)
{
this.month=month;
}
publicintgetMonth()
{
returnmonth;
}
publicvoidsetDay(intday)
{
this.day=day;
}
publicintgetDay()
{
returnday;
}
publicvoid设置信息条(intyear,intmonth,intday)
{
信息条.setText(""+year+"年"+month+"月"+day+"日");
}
publicvoid设置文本区(Strings)
{
text.setText(s);
}
publicvoid获取日志内容(intyear,intmonth,intday)
{
Stringkey=""+year+""+month+""+day;
try
{
FileInputStreaminOne=newFileInputStream(file);
ObjectInputStreaminTwo=newObjectInputStream(inOne);
table=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
}
catch(Exceptionee)
{
}
if(table.containsKey(key))
{
Stringm=""+year+"年"+month+"月"+day+"这一天有日志记载,想看吗?
";
intok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,
~16/25~
JOptionPane.QUESTION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{
text.setText((String)table.get(key));
}
else
{
text.setText("");
}
}
else
{
text.setText("无记录");
}
}
publicvoid保存日志(intyear,intmonth,intday)
{
String日志内容=text.getText();
Stringkey=""+year+""+month+""+day;
Stringm=""+year+"年"+month+"月"+day+"保存日志吗?
";
intok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{
try
~17/25~
{
FileInputStreaminOne=newFileInputStream(file);
ObjectInputStreaminTwo=newObjectInputStream(inOne);
table=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
table.put(key,日志内容);
FileOutputStreamout=newFileOutputStream(file);
ObjectOutputStreamobjectOut=newObjectOutputStream(out);
objectOut.writeObject(table);
objectOut.close();
out.close();
}
catch(Exceptionee)
{
}
}
}
publicvoid删除日志(intyear,intmonth,intday)
{
Stringkey=""+year+""+month+""+day;
if(table.containsKey(key))
{
Stringm="删除"+year+"年"+month+"月"+day+"日的日志吗?
";
~18/25~
intok=JOptionPane.showConfirmDialog(this,m,"询问",JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(ok==JOptionPane.YES_OPTION)
{
try
{
FileInputStreaminOne=newFileInputStream(file);
ObjectInputStreaminTwo=newObjectInputStream(inOne);
table=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
table.remove(key);
FileOutp