java课程设计报告---实用万年历记事本.doc_第1页
java课程设计报告---实用万年历记事本.doc_第2页
java课程设计报告---实用万年历记事本.doc_第3页
java课程设计报告---实用万年历记事本.doc_第4页
java课程设计报告---实用万年历记事本.doc_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

java应用技术实践报告题 目 实用万年历记事本 起讫日期 所在院系 学生姓名 专 业 班 级 学 号 指导教师 职 称 所在单位 2010年 1 月 11 日一总体设计1类之间的关系如图1-1所示。calendarwindowcalendarimagecalendarmessagecalendarpadclocknotepad图1-1 类之间的关系2java源文件及其功能如表1-1所示。表1-1 java源文件及其功能序号文件名主要功能备注1calendarwindow.java创建实用万年历记事本程序的主窗口含有main方法2calendarmessage.java该文件用来刻画和“日期”有关的数据3calendarpad.java该文件负责显示和修改 calendarmessage对象中的日期数据4notepad.java该文件负责提供编辑、读取、保存和删除日志的5calendarimage.java该文件负责绘制图像6clock.java该文件负责显示时钟二详细设计1 calendarwindow(1)成员变量主要成员变量(属性)成员变量描述变量类型名称年、月、日intyear,month,day显示日期jtextfieldshowday日历对象calendarpadcalendarpad日志对象notepadnotepad月jtextfieldshowmonth年jtextfieldshowyear图像对象calendarimagecalendarimage时钟对象clockclock 处理日期calendarmessaecalendarmessae按钮对象jbuttonnextyear、nextmonth、previousyear、previousmonth、savedailyrecord、deletedailyrecord、readdailyrecord(2)方法见表主要方法方法名功能备注calendarwindow创建程序主窗口构造方法domark对日志日期做出标记focusgained处理focuslistener事件mousepressed处理mouseevent事件actionperformed处理actionevent事件接口方法main程序开始运行(3)源代码见文件calendarwindow 二calendarmessage(1)成员变量见表表1-4 主要成员变量(属性)成员变量描述变量类型名称 记录日志的年、月、日intyear、month、day(2)方法见表1-5表1-5 主要方法方法名功能备注setday、getday设置、获取日期setmonth/getmonth设置、获取月份setyear、getyear 设置、获取年份getmonthcalendar()处理日期数据(3)源代码见文件calendarmessage.javaimport java.util.calendar;public class calendarmessage int year=-1,month=-1,day=-1; public void setyear(int year) this.year=year; public int getyear() return year; public void setmonth(int month) if(month=1) this.month=month; else this.month=1; public int getmonth() return month; public void setday(int day) this.day=day; public int getday() return day; public string getmonthcalendar() string day=new string42; calendar rili=calendar.getinstance(); rili.set(year,month-1,1);/将日历翻到year年month月1日,注意0表示一月.11表示十二月 int 星期几=rili.get(calendar.day_of_week)-1; int dayamount=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; else dayamount=28; for(int i=0;i星期几;i+) dayi=; for(int i=星期几,n=1;i星期几+dayamount;i+) dayi=string.valueof(n) ; n+; for(int i=星期几+dayamount;i42;i+) dayi=; return day; 3calendarpad(1)成员变量见表表1-6 主要成员变量(属性)成员变量描述变量类型名称确定年份、月份、日期的整数intyear、month、day处理和日期有关的数据calendarpadcalendarpad显示日期jtextfieldshowday星期string(2)方法见表表1-7 主要方法方法名功能备注calendarpad创建calendarpad对象构造方法setshowdaytextfield(jtextfield)负责设置showday数组setcalendarmessage(calendarmessage)负责设置calendarmessage对象showmonthcalendar()负责显示“日历”(3)源代码见文件calendarpad.javaimport javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;public class calendarpad extends jpanel int year,month,day; calendarmessage calendarmessage; jtextfield showday; jlabel title; string 星期=sun日,mon一,tue二,wed三,thu四,fri五,sat六; jpanel north,center; public calendarpad() setlayout(new borderlayout(); north=new jpanel(); north.setlayout(new gridlayout(1,7); center=new jpanel(); center.setlayout(new gridlayout(6,7); add(center,borderlayout.center); add(north,borderlayout.north); title=new jlabel7; for(int j=0;j7;j+) titlej=new jlabel(); titlej.setfont(new font(timesroman,font.bold,12); titlej.settext(星期j); titlej.sethorizontalalignment(jlabel.center); titlej.setborder(borderfactory.createraisedbevelborder(); north.add(titlej); title0.setforeground(color.red); title6.setforeground(color.blue); public void setshowdaytextfield(jtextfield text) showday=text; for(int i=0;ishowday.length;i+) showdayi.setfont(new font(timesroman,font.bold,15); showdayi.sethorizontalalignment(jtextfield.center); showdayi.seteditable(false); center.add(showdayi); public void setcalendarmessage(calendarmessage calendarmessage) this.calendarmessage=calendarmessage; public void showmonthcalendar() string a=calendarmessage.getmonthcalendar(); for(int i=0;i42;i+) showdayi.settext(ai); validate(); 4notepad(1)成员变量见表1-8表1-8 主要成员变量(属性)成员变量描述变量类型名称可编辑多行文本jtextareatext显示和日期有关的数据jtextfieldshowmessagemenujpopupmenu下月,上月复制,剪切,粘贴,清空jmenuitemitmecopy、itmecut、itempaste、itmeclear(2)方法见表9表9 主要方法方法名功能备注notepad创建notepad构造方法setshowmessage(int,int,int)指定日期数据,显示日期信息save(file,int,int,int)指定日期,保存日志save(file,int,int,int)指定日期,删除日志read(file,int,int,int)读取参数日期日志(3)源代码见文件notepad.javaimport java.awt.*;import javax.swing.*;import java.io.*;import java.awt.event.*;public class notepad extends jpanel implements actionlistener jtextarea text; jtextfield showmessage; jpopupmenu menu; jmenuitem itemcopy,itemcut,itempaste,itemclear; public notepad() showmessage=new jtextfield(); showmessage.sethorizontalalignment(jtextfield.center); showmessage.setfont(new font(timesroman,font.bold,16); showmessage.setforeground(color.blue); showmessage.setbackground(color.pink); showmessage.setborder(borderfactory.createraisedbevelborder(); showmessage.seteditable(false); menu=new jpopupmenu(); itemcopy=new jmenuitem(复制); itemcut=new jmenuitem(剪切); itempaste=new jmenuitem(粘贴); itemclear=new jmenuitem(清空); itemcopy.addactionlistener(this); itemcut.addactionlistener(this); itempaste.addactionlistener(this); itemclear.addactionlistener(this); menu.add(itemcopy); menu.add(itemcut); menu.add(itempaste); menu.add(itemclear); text=new jtextarea(10,10); text.addmouselistener(new mouseadapter() public void mousepressed(mouseevent e) if(e.getmodifiers()=inputevent.button3_mask) menu.show(text,e.getx(),e.gety(); );setlayout(new borderlayout(); add(showmessage,borderlayout.north); add(new jscrollpane(text),borderlayout.center); public void setshowmessage(int year,int month,int day) showmessage.settext(+year+年+month+月+day+日); public void save(file dir,int year,int month,int day) string dailycontent=text.gettext(); string filename=+year+month+day+.txt; string key=+year+month+day; string dayfile=dir.list(); boolean boo=false; for(int k=0;kdayfile.length;k+) if(dayfilek.startswith(key) boo=true; break; if(boo) string m=+year+年+month+月+day+已有日志,将新的内容添加到日志吗?; int ok=joptionpane.showconfirmdialog(this,m,询问,joptionpane.yes_no_option, joptionpane.question_message); if(ok=joptionpane.yes_option) try file f=new file(dir,filename); randomaccessfile out=new randomaccessfile(f,rw); long fileend=out.length(); byte bb=dailycontent.getbytes(); out.seek(fileend); out.write(bb); out.close(); catch(ioexception exp) else string m=+year+年+month+月+day+还没有日志,保存日志吗?; int ok=joptionpane.showconfirmdialog(this,m,询问,joptionpane.yes_no_option, joptionpane.question_message); if(ok=joptionpane.yes_option) try file f=new file(dir,filename); randomaccessfile out=new randomaccessfile(f,rw); long fileend=out.length(); byte bb=dailycontent.getbytes(); out.write(bb); out.close(); catch(ioexception exp) public void delete(file dir,int year,int month,int day) string key=+year+month+day; string dayfile=dir.list(); boolean boo=false; for(int k=0;kdayfile.length;k+) if(dayfilek.startswith(key) boo=true; break; if(boo) string m=删除+year+年+month+月+day+日的日志吗?; int ok=joptionpane.showconfirmdialog(this,m,询问,joptionpane.yes_no_option, joptionpane.question_message); if(ok=joptionpane.yes_option) string filename=+year+month+day+.txt; file deletefile=new file(dir,filename); deletefile.delete(); else string m=+year+年+month+月+day+无日志记录; joptionpane.showmessagedialog(this,m,提示,joptionpane.warning_message); public void read(file dir,int year,int month,int day) string filename=+year+month+day+.txt; string key=+year+month+day; string dayfile=dir.list(); boolean boo=false; for(int k=0;kdayfile.length;k+) if(dayfilek.startswith(key) boo=true; break; if(boo) string m=+year+年+month+月+day+有日志,显示日志内容吗?; int ok=joptionpane.showconfirmdialog(this,m,询问,joptionpane.yes_no_option, joptionpane.question_message); if(ok=joptionpane.yes_option) text.settext(null); try file f=new file(dir,filename); filereader inone=new filereader(f); bufferedreader intwo= new bufferedreader(inone); string s=null; while(s=intwo.readline()!=null) text.append(s+n); inone.close(); intwo.close(); catch(ioexception exp) else string m=+year+年+month+月+day+无日志记录; joptionpane.showmessagedialog(this,m,提示,joptionpane.warning_message); public void actionperformed(actionevent e) if(e.getsource()=itemcopy) text.copy(); else if(e.getsource()=itemcut) text.cut(); else if(e.getsource()=itempaste) text.paste(); else if(e.getsource()=itemclear) text.settext(null); 5calendarimage(1)成员变量见表1-8表1-8 主要成员变量(属性)成员变量描述变量类型名称存放图像文件的引用fileimagefileimageimage负责创建image对象toolkittool(2)方法见表9表9 主要方法方法名功能备注calendarimage()创建calendarimage对象构造方法setimagefile(file)负责设置imagefile对象,根据imagefile创建imagepaintcomponent(graphics)负责绘制image(3)源代码见文件calendarimage.javaimport javax.swing.*;import java.io.*;import java.awt.*;public class calendarimage extends jpanel file imagefile; image image; toolkit tool; calendarimage() tool=gettoolkit(); public void setimagefile(file f) imagefile=f; try image=tool.getimage(imagefile.touri().tourl(); catch(exception exp) repaint(); public void paintcomponent(graphics g) super.paintcomponent(g); int w=getbounds().width; int h=getbounds().height; g.drawimage(image,0,0,w,h,this); 6clock(1)成员变量见表主要成员变量(属性)成员变量描述变量类型名称时钟clockclock(2)方法见表主要方法方法名功能备注clock()创建clock对象构造方法paint(graphics)负责设置imagefile对象,根据imagefile创建imagestart()开始线程stop()停止线程run()(3)源代码见文件clock.javaimport java.awt.*;import java.util.*;import javax.swing.*;import java.awt.geom.*;public class clock extends jpanel implements runnablethread clock;public clock() super(); setfont(new font(times new roman,font.bold,60); start(); setsize(280,100); dimension screen=toolkit.getdefaulttoolkit().getscreensize();public void start() if(clock=null) clock=new thread(this); clock.start(); public void run() while(clock!=null) repaint(); try thread.sleep(1000); catch (interruptedexception ex) ex.printstacktrace(); public void stop() clock=null;public void paint(graphics g) graphics2d g2=(graphics2d)g; calendar now=new gregoriancalendar(); string t

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论