日历记事本实验报告Word下载.docx
《日历记事本实验报告Word下载.docx》由会员分享,可在线阅读,更多相关《日历记事本实验报告Word下载.docx(33页珍藏版)》请在冰豆网上搜索。
用来刻画和“日期”有关的数据。
3、数据显示和修改类(CalendarPad.java)
用来表示“日历”,即负责显示和修改CalendarMessage对象中的日期数据。
4、记事本类(NotePad.java)
创建以提供编辑、读取、保存和删除日志的功能。
5、时钟类(Clock.java)
负责显示时钟。
6、图像类(CalendarImage.java)
负责绘制图像。
六、程序详细设计:
代码如下:
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.util.*;
importjava.io.*;
publicclassCalendarWindowextendsJFrameimplementsActionListener,
MouseListener,FocusListener{
intyear,month,day;
CalendarMessagecalendarMessage;
CalendarPadcalendarPad;
NotePadnotePad;
JTextFieldshowYear,showMonth;
JTextField[]showDay;
CalendarImagecalendarImage;
Clockclock;
JButtonnextYear,previousYear,nextMonth,previousMonth;
JButtonsaveDailyRecord,deleteDailyRecord,readDailyRecord;
Filedir;
ColorbackColor=Color.white;
publicCalendarWindow(){
dir=newFile("
./dailyRecord"
);
dir.mkdir();
showDay=newJTextField[42];
for(inti=0;
i<
showDay.length;
i++){
showDay[i]=newJTextField();
showDay[i].setBackground(backColor);
showDay[i].setLayout(newGridLayout(3,3));
showDay[i].addMouseListener(this);
showDay[i].addActionListener(this);
showDay[i].addFocusListener(this);
}
calendarMessage=newCalendarMessage();
calendarPad=newCalendarPad();
notePad=newNotePad();
Calendarcalendar=Calendar.getInstance();
calendar.setTime(newDate());
year=calendar.get(Calendar.YEAR);
month=calendar.get(Calendar.MONTH)+1;
day=calendar.get(Calendar.DAY_OF_MONTH);
calendarMessage.setYear(year);
calendarMessage.setMonth(month);
calendarMessage.setDay(day);
calendarPad.setCalendarMessage(calendarMessage);
calendarPad.setShowDayTextField(showDay);
notePad.setShowMessage(year,month,day);
calendarPad.showMonthCalendar();
doMark();
//给有日志的号码做标记,见后面的doMark()方法
calendarImage=newCalendarImage();
calendarImage.setImageFile(newFile("
flower.jpg"
));
clock=newClock();
JSplitPanesplitV1=
newJSplitPane(JSplitPane.VERTICAL_SPLIT,calendarPad,calendarImage);
JSplitPanesplitV2=
newJSplitPane(JSplitPane.VERTICAL_SPLIT,notePad,clock);
JSplitPanesplitH=newJSplitPane(JSplitPane.HORIZONTAL_SPLIT,splitV1,splitV2);
add(splitH,BorderLayout.CENTER);
showYear=newJTextField("
"
+year,6);
showYear.setFont(newFont("
TimesRoman"
Font.BOLD,12));
showYear.setHorizontalAlignment(JTextField.CENTER);
showMonth=newJTextField("
"
+month,4);
showMonth.setFont(newFont("
showMonth.setHorizontalAlignment(JTextField.CENTER);
nextYear=newJButton("
下年"
previousYear=newJButton("
上年"
nextMonth=newJButton("
下月"
previousMonth=newJButton("
上月"
nextYear.addActionListener(this);
previousYear.addActionListener(this);
nextMonth.addActionListener(this);
previousMonth.addActionListener(this);
showYear.addActionListener(this);
JPanelnorth=newJPanel();
north.add(previousYear);
north.add(showYear);
north.add(nextYear);
north.add(previousMonth);
north.add(showMonth);
north.add(nextMonth);
add(north,BorderLayout.NORTH);
saveDailyRecord=newJButton("
保存日志"
);
deleteDailyRecord=newJButton("
删除日志"
readDailyRecord=newJButton("
读取日志"
saveDailyRecord.addActionListener(this);
deleteDailyRecord.addActionListener(this);
readDailyRecord.addActionListener(this);
//怎么实现的按钮上附加的日记读取功能?
?
JPanelpSouth=newJPanel();
pSouth.add(saveDailyRecord);
pSouth.add(deleteDailyRecord);
pSouth.add(readDailyRecord);
add(pSouth,BorderLayout.SOUTH);
setVisible(true);
setBounds(60,60,660,480);
validate();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
publicvoidactionPerformed(ActionEvente){//鼠标获取按钮内容的动作函数
if(e.getSource()==nextYear){//“下一年”
year++;
showYear.setText("
+year);
elseif(e.getSource()==previousYear){//“上一年”
year--;
elseif(e.getSource()==nextMonth){//“下一月”
month++;
if(month>
12)month=1;
showMonth.setText("
+month);
elseif(e.getSource()==previousMonth){//、“上一月”
month--;
if(month<
1)month=12;
elseif(e.getSource()==showYear){
Strings=showYear.getText().trim();
chara[]=s.toCharArray();
booleanboo=false;
a.length;
i++)
if(!
(Character.isDigit(a[i])))
boo=true;
if(boo==true)//弹出“警告”消息对话框
JOptionPane.showMessageDialog(this,"
您输入了非法年份"
"
警告"
JOptionPane.WARNING_MESSAGE);
elseif(boo==false)
year=Integer.parseInt(s);
elseif(e.getSource()==saveDailyRecord){//“保存日记”
notePad.save(dir,year,month,day);
elseif(e.getSource()==deleteDailyRecord){//“删除日记”
notePad.delete(dir,year,month,day);
}
elseif(e.getSource()==readDailyRecord)//“读取日记”
notePad.read(dir,year,month,day);
publicvoidmousePressed(MouseEvente){
JTextFieldtext=(JTextField)e.getSource();
Stringstr=text.getText().trim();
try{day=Integer.parseInt(str);
catch(NumberFormatExceptionexp){
publicvoidmouseReleased(MouseEvente){}
publicvoidmouseEntered(MouseEvente){}
publicvoidmouseExited(MouseEvente){}
publicvoidmouseClicked(MouseEvente){}
publicvoidfocusGained(FocusEvente){
Componentcom=(Component)e.getSource();
com.setBackground(Color.pink);
publicvoidfocusLost(FocusEvente){//此事件指示Component不再是焦点所有者。
指示Component已获得或失去输入焦点的低级别事件。
com.setBackground(backColor);
publicvoiddoMark(){
showDay[i].removeAll();
Stringstr=showDay[i].getText().trim();
try{
intn=Integer.parseInt(str);
if(isHaveDailyRecord(n)==true){//见后面的isHaveDailyRecord()方法
JLabelmess=newJLabel("
有"
mess.setFont(newFont("
Font.PLAIN,11));
mess.setForeground(Color.blue);
showDay[i].add(mess);
catch(Exceptionexp){}
calendarPad.repaint();
calendarPad.validate();
publicbooleanisHaveDailyRecord(intn){
Stringkey="
+year+"
+month+"
+n;
String[]dayFile=dir.list();
for(intk=0;
k<
dayFile.length;
k++){
if(dayFile[k].equals(key+"
.txt"
)){
break;
returnboo;
publicstaticvoidmain(Stringargs[]){
newCalendarWindow();
}
importjava.util.Calendar;
publicclassCalendarMessage{
intyear=-1,month=-1,day=-1;
//为什么要附上负值?
publicvoidsetYear(intyear){
this.year=year;
publicintgetYear(){
returnyear;
publicvoidsetMonth(intmonth){
=12&
&
month>
=1)
this.month=month;
else
this.month=1;
publicintgetMonth(){
returnmonth;
publicvoidsetDay(intday){
this.day=day;
publicintgetDay(){
returnday;
publicString[]getMonthCalendar(){
String[]day=newString[42];
Calendarrili=Calendar.getInstance();
rili.set(year,month-1,1);
//将日历翻到year年month月1日,注意0表示一月...11表示十二月
int星期几=rili.get(Calendar.DAY_OF_WEEK)-1;
//星期天对应是0,以此类推星期一是1....星期六是6。
intdayAmount=0;
//***********************************对年月的分类******************************//
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
dayAmount=31;
if(month==4||month==6||month==9||month==11)
dayAmount=30;
if(month==2)
if(((year%4==0)&
(year%100!
=0))||(year%400==0))
dayAmount=29;
dayAmount=28;
//*********************************日对应星期**********************************//
星期几;
i++)//Calendar所得到的星期几和i对比,小于它的为空标签
day[i]="
;
for(inti=星期几,n=1;
星期几+dayAmount;
day[i]=String.valueOf(n);
n++;
for(inti=星期几+dayAmount;
42;
i++)//当月所存在日数之后的都是空标签
}
publicclassCalendarPadextendsJPanel{
JTextField[]showDay;
JLabeltitle[];
//定义标签组
String[]星期={"
SUN日"
MON一"
TUE二"
WED三"
THU四"
FRI五"
SAT六"
};
JPanelnorth,center;
//定义面板
publicCalendarPad(){
setLayout(newBorderLayout());
//实现边界布局
north=newJPanel();
//创建靠上边的面板
north.setLayout(newGridLayout(1,7));
//网格布局管理一行七列
center=newJPanel();
//创建靠下边的面板
center.setLayout(newGridLayout(6,7));
//网格布局管理六行七列
add(center,BorderLayout.CENTER);
title=newJLabel[7];
for(intj=0;
j<
7;
j++){
tit