




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、日历记事本1. 课程设计目的通过此次课程设计,巩固所学Java语言基本知识,增进Java语言编辑基本功,掌 握JDK Editplus、Eclipse、JCreator等开发工具的运用,拓宽常用类库的应用。使 我们通过该教学环节与手段,把所学课程及相关知识加以融会贯通,全面掌握Java语言的编程思想及面向对象程序设计的方法,为今后从事实际工作打下坚实的基础。本课程设计设计GUI界面的日历记事本,系统将日历和记事本结合在一起,可以方 便的保存、查看日志,即在任何日期记载下有关内容或查看某个日期记载的内容。2. 设计方案论证2.1设计思路2.1.1本系统界面的左侧是日历和一幅图像。该日历可以按年前
2、后翻动,用鼠标左键单 击“上年”按钮,可将当前日历的年份减一;用鼠标左键单击“下年”按钮,可将当前 日历的年份加一。该日历还可以在某年内按月前后翻动,用鼠标单击“上月”按钮,可 将当前日历的月份减一;用鼠标单击“下月”按钮,可将当前日历的月份增一。2.1.2系统界面的右侧是记事本和一个时钟。用鼠标单击日历上的日期,就可以通过该 记事本编辑有关日志,并将日志保存到一个文件。该文件的名字是由当前日期组成的字 符序列。用户可以查看、删除某个日期的日志,也可以继续向某个日志添加新的内容。2.1.3当某个日期有日志时,显示该日期的外观上就会出现“有”标记,表明这个日期 有日志;当用户删除某个日期的日志后
3、,该日期的外观上的“有” 标记就会消失。2.2设计方法在设计日历记事本时,需要编写6个Java源文件:CalendarWindow.java、Cale ndarPad.java 、 NotePad.java 、Cale ndarlmage.java、 Clock.java 和CalendarMessage.java。日历记事本除了需要编写的上述 6个Java源文件所给出的类外,还需要Java系统 提供的一些重要的类,如 JTextField、JTextArea和File 类。2.2.1 Cale ndarWi ndow.java主类该类创建动态日历记事本的主窗口,该类含有main方法,是程序的
4、入口程序,CalenderWindow类的成员变量中有 5种重要类型的对象,CalendarPad、NotePad、 Calendarlmage、Clock、CalendarMessage 对象。2.2.2 Cale nderMessage.java该类用来刻画和“日期”有关的数据。2.2.3 Cale nderPad.java该类是JPanel类的子类,所创建的对象是 CalendarWindow类的重要成员之一,用 来表示”日历”,即负责显示和修改Cale ndarMessage对象中的日期数据。2.2.4 Notepad.java该类是JPanel的一个子类,创建的对象表示“记事本”,提
5、供编辑读取和保存阐述 日志的功能。2.2.5 Cale ndarlmage.java该类所创建的对象是CalendarWindow类的成员之一,负责绘制图像。2.2.6 Clock.java该类创建的对象是CalendarWindow类的成员之一,负责显示时钟。日历记事本用到的一些重要的类以及之间的关系如图1所示。图1类之间的组合关系图 2.3功能模块此设计共有以下几个功能模块。2.3.1年份改变点击一次“上年”按钮可增加一年,点击一次“下年”按钮可减少一年,多次连 续点击即可依次改变多个年份。2.3.2月份改变点击一次“上月”按钮可以增加一月,点击一次“下年”按钮可以减少一月,多 次连续点击
6、即可一次改变多个月份。2.3.3日期显示用于显示当前所选择的所在的年份月份的所有日期、星期,并在记载有日志的日 期格中左上角显示“有”标记,而没有记载日志的日期则没有任何标记。2.3.4 图片显示为了使本设计界面美观,设置了图片显示模块,使整个设计更加实用、美观、大方。2.3.5 日志显示界面的右侧上半部的文本框中,可以选择某天记载日志,并且读取的日志在此部 分显示出来。2.3.6 时钟显示以传统时钟(有时针、分针、秒针)的模式显示当前的时间。2.3.7保存、删除、读取日志。可以在没有记载日志的日期里记载日志并保存,可以对以保存的日志进行读取、修改或 者删除。2.4流程图如下选择日期保存并标记
7、 有”显示日志删除并去掉 有图2流程图2.5类、方法、属性说明2.5.1CalendarWindow 类(1)成员变量year,mo nth和day是int型数据,它们的值分别确定年份、月份和日期。-calendarMessage是CalendarMessage对象,该对象可以处理和日期有关的数据。-calendarPad是CalendarPad对象,该对象可以显示和日期有关的数据。-notePad是NotePad对象,具有编辑、读取、保存和删除日志的功能。-showYear和showMonth是JTextField 类创建的文本框,用来显示年份和月份。-showDay数组的每个单元是JTex
8、tField类创建的用来显示日期的文本框。 calendarlmage是Calendarlmage创建的对象,用来显示图像。 clock是Clock创建的对象,用来显示时钟。 nextYear等是JButton创建的按钮对象,名字依次为“下年”、“上年”、“下月”、“上月”。这些按钮都将当前窗口注册为自己的ActionEvent事件监听者。saveDauktRecord,deleteDailyRecord,readDailyRecord是 JButton 创建的按钮对象,名字依次为“保存日志”、“删除日志”、“读取日志”。这些按钮都当前窗口注册为自 己的ActionEvent事件监听者。(2)
9、方法 CalendarWindow ()是构造方法,负责完成窗口的初始化。action Performed (Acri on Eve nt )方法是 Cale ndarWi ndow 类实现的 Actio nList ner接口中的方法。 mousePressed(MouseEvent)方法是 CalendarWindow类实现的 MouseListener 接口中 的方法。 doMark ()方法负责对有日志的日期作出标记。 main方法是程序运行的人口方法。2.5.2CalenderMessage 类(1) 成员变量year、mo nth和day是int型数据,它们的值分别确定年份、月份和
10、日期。(2) 方法 setYear (int )和getYear ()方法分别用来设置 year和获取year的值。 setMo nth (int ) 和 getMo nth ()方法分别用来设置 mon th和获取mon th的值。 setDay (int ) 和 getDay ()方法分别用来设置 day和获取day的值。2. 5.3CalenderPad 类(1) 成员变量 year、month和day是int型数据,它们的值分别确定年份、月份和日期。 calenderMessage是CalenderMessage对象,该对象可以处理和日期有关的数据。 showDay数组的每个单元是JT
11、extField类创建的用来显示日期的文本框。(2) 方法 CalenderPad ()是构造方法,用来创建 calenderPad对象。 setShowDayTextField (JTextField)方法负责设置 showDay数组。 showMonthCalendar ()方法负责显示“日历”。2.5.4Notepad 类(1) 成员变量 text是JTextArea创建的文本区,可在text中编辑多行文本。 menu是JpopupMenu创建的弹出式菜单。 showMessage是 JTextField创建的文本条,用来显示和日期有关的数据。(2) 方法 Notepad ()是构造方法
12、,用来创建 notePad对象。 setShowMessage(int , int , int )方法根据参数指定的日期数据显示有关日期信息 save (File , int , int , int )方法根据参数指定的日期保存日志。 delete (File , int , int , int )方法删除参数日期指定的日志。2.5.5Calendarlmage 类(1) 成员变量 imageFile是File对象,用来存放图像文件的引用。 image 是 Image 对象。 tool是Toolkit 对象,负责创建image对象。(2) 方法 Calendarlmage () 是构造方法,负
13、责创建 calendarlmage 对象。 pa in tComp onen t(Graphics)方法负责绘希 9 image。2.5.6Clock 类(1) 成员变量 date是Date对象,负责圭寸装本地时间。 sec on dTime 是javax.sw ing 包中 Timer 类中创建的计时器。 pointSX , pointSY是double型数组,其单元值用来表示秒针端点坐标。(2) 方法 Clock ()是构造方法,用来创建 clock对象。 paintComonent (Graphics )负责完成绘制工作。3. 设计结果与分析将 6 个 Java 源文件:Calendar
14、Window.java、CalendarPad.java、NotePad.java、 Calendarlmage.java 、Clock.java 和 CalendarMessage.java,以及所需要的名为 flower.jpg的图像文件保存到同一目录中。分别编译这6个Java源文件,或运行“javac*.java ”命令,编译全部的源文件,然后运行主类,即运行CalendarWindow类。运行结果图中可以看出记载日志的日期中会有“有”标记,而没有记载日志的日期 中没有“有”标记。右下侧是时钟,左下角显示图片。图3运行结果图在此结果中,点击上方的“上年”或“下年”可以改变年份,点击“上月
15、”或“下 月”可以改变月份。右下角显示当前的时间。可以在任意一天记日志,在有“有”标记 的日期查看日志或删除日志。当想保存日志时,选中某一天,在右侧记下日志点击“保存”即可。当想查看某天的日志是选中带“有”标记的当天,点击“读取日志”可出现图4所示的提示框,点击是即可读取日志就可以查看日志内容,如图5所示。图4提示显示页面图图5查看日志图若想要删除已有的日志,则选中要删除的日期,点击“删除日志”,则出现提示框, 如图6所示,询问是否要删除,点击“是”,则删除了此篇日志,此时,原有的“有” 字标记就会消失,此篇日志也就随之消失,如图7所示。图6提示删除页面图图7删除后页面图4. 设计体会经过几天
16、的努力,参考了一些书和网上的资料,完成了一个日历记事本的设计。由于只学过一学期的Java课程,知识掌握的有限,所以此程序不是特别复杂。在 这个过程中,在把书本上的知识给系统的复习了一下的同时,同时也发现了自己的太多 不足,对JAVA语言整体知识构架不够熟悉,不能灵活地运用所学的知识点。自己的编 程方式也不是太标准,经常出现代码写错而调试很久的低级错误情况出现。通过这次课 程设计,我明白了编写程序,不仅要求对课本知识有较深刻的了解,同时要求程序设计 者有较强的思维和动手能力。还要熟悉常用的编程技巧和编程思想,不能只关心程序的 运行结果,更要注重程序代码的结构,编程者更要有严谨地态度。本次课程设计
17、不仅巩固了以前所学的知识,还有通过设计过程中大量地查阅网络文章,对一些关键知识点逐个地了解并解决相关问题。所以网络这个强大的工具,确实是 我们学习之中可以借助的一个很好的帮手。 网络是我们学习的最好的老师之一。有问题, 问网络,是一个很好的习惯。因此学到了不少书本上没有的知识;学到了很多java编程知识。可以说它给了我一次自我评估的机会。通过设计让我知道了那些方面我还是有 欠缺的,促使我花时间弥补。总之,本次课程设计收获颇丰。5. 参考文献1陈国君,陈磊,刘洋.Java2程序设计基础M 北京:清华大学出版社;2008.62耿祥义,张跃平.Java课程设计M.北京:清华大学出版社;2008.11
18、:228-2513张思民,梁维娜.Java程序设计实践教程M.北京:清华大学出版社;2006.8:1-214叶核亚.JAVA2程序设计实用教程M(第2版).北京:电子工业出版社;2008.4.6.附录:(1) /CalendarWindow 类import javax.swi ng.*;import java.awt.*;import java.awt.eve nt.*;import java.util.*;import java.io.*;public class Cale ndarWi ndow exte nds JFrame impleme nts Actio nListe ner, M
19、ouseListe ner,FocusListe nerint year,m on th,day;Cale ndarMessage cale ndarMessage;Cale ndarPad cale ndarPad;NotePad notePad;JTextField showYear,showM on th;JTextField showDay;Cale ndarlmage cale ndarlmage;Clock clock;JButt on n extYear,previousYear, nextM on th,previousM on th;JButt on saveDailyRec
20、ord,deleteDailyRecord,readDailyRecord;File dir;Color backColor=Color.white;public Cale ndarWi ndow()dir= new File(./dailyRecord);dir.mkdir();showDay=new JTextField42;for(i nt i=0;i12) month=1;showMo nth.setText( +m on th);cale ndarMessage.setM on th(m on th);cale ndarPad.setCale ndarMessage(cale nda
21、rMessage); cale ndarPad.showM on thCale ndar();no tePad.setShowMessage(year,m on th,day); doMark();else if(e.getSource()=previousM on th)mon th-;if(mo nth1) mon th=12;showMo nth.setText(” +m on th);cale ndarMessage.setM on th(m on th);cale ndarPad.setCale ndarMessage(cale ndarMessage);cale ndarPad.s
22、howM on thCale ndar();no tePad.setShowMessage(year,m on th,day); doMark();else if(e.getSource()=showYear)String s=showYear.getText().trim();char a=s.toCharArray();boolea n boo=false;for(i nt i=0;iaen gth;i+)if(!(Character.isDigit(ai)boo=true;if(boo=true) II弹出警告”肖息对话框JOptionPane.showMessageDialog(thi
23、s,您输入了非法年份 ,警告,JOptio nPa ne.WARNING_MESSAGE); else if(boo=false)year =ln teger.parse In t(s);showYear.setText(+year);cale ndarMessage.setYear(year);cale ndarPad.setCale ndarMessage(cale ndarMessage);cale ndarPad.showM on thCale ndar();no tePad.setShowMessage(year,m on th,day);doMark();else if(e.get
24、Source()=saveDailyRecord)no tePad.save(dir,year,m on th,day);doMark();else if(e.getSource()=deleteDailyRecord)no tePad.delete(dir,year,m on th,day);doMark();else if(e.getSource()=readDailyRecord)no tePad.read(dir,year,m on th,day);public void mousePressed(MouseEve nt e)JTextField text=(JTextField)e.
25、getSource();Stri ng str=text.getText().trim();try day =ln teger.parse In t(str);catch(NumberFormatExcepti on exp)cale ndarMessage.setDay(day);no tePad.setShowMessage(year,m on th,day);public void mouseReleased(MouseEve nt e)public void mouseE ntered(MouseEve nt e)public void mouseExited(MouseEve nt
26、e) public void mouseClicked(MouseEve nt e) public void focusGa in ed(FocusEve nt e)Comp onent com=(Comp onen t)e.getSource(); com.setBackgro un d(Color.p in k);public void focusLost(FocusEve nt e)Comp onent com=(Comp onen t)e.getSource(); com.setBackgro un d(backColor);public void doMark()for(i nt i
27、=0;ishowDa y.len gth;i+)showDayi.removeAII();String str=showDayi.getText().trim();tryint n=ln teger.parse In t(str);if(isHaveDailyRecord(n)=true) / 见后面的 isHaveDailyRecord()方法 JLabel mess=new JLabel(有);mess.setFo nt(new Fon t(TimesRoma n,Fo nt.PLAIN,11); mess.setForegro un d(Color.blue);showDayi.add(
28、mess); catch(Excepti on exp)cale ndarPad.repa in t();cale ndarPad.validate();public boolea n isHaveDailyRecord(i nt n)String key=+year+month+n;String dayFile=dir.list();boolea n boo=false;for(int k=0;kdayFile.length;k+)if(dayFilek.equals(key+.txt)boo=true;break; return boo;public static void main(St
29、ring args)new Cale ndarWi ndow();(2) / CalenderMessage 类import java.util.Cale ndar;public class Cale ndarMessageint year=-1,m on th=-1,day=-1;public void setYear(i nt year)this.year=year;public int getYear()retur n year;public void setM on th(i nt mon th)if(mo nth=1)this.m on th=mo nth;elsethis.m on
30、 th=1;public in t getMo nth()return mon th;public void setDay(i nt day)this.day=day;public int getDay()retur n day;public Stri ng getMo nthCale ndar()Stri ng day=new Strin g42;Cale ndar rili=Cale ndar.getI nsta nce();rili.set(year,mo nth-1,1);/将日历翻到year年mon th月1日,注意0表示一月.11表示十二月 int 星期几=rili.get(Cal
31、endar.DAY_OF_WEEK)-1;int dayAm oun t=0;if(mon th=1|m on th=3|m on th=5|m on th=7|m on th=8|m on th=10|m on th=12) dayAm oun t=31;if(mon th=4|m on th=6|m on th=9|m on th=11)dayAm oun t=30;if(mon th=2)if(year%4=0)&(year%100!=0)|(year%400=0)dayAm oun t=29;elsedayAmou nt=28;for(int i=0;i 星期几;i+)dayi=;fo
32、r(int i=星期几,n=1;i 星期几 +dayAmount;i+)dayi=String.valueOf(n);n+;for(i nt i=星期几 +dayAmou nt;i42;i+)dayi=;retur n day; (3) /CalenderPad 类import javax.swi ng.*;import java.awt.*;import java.awt.eve nt.*;import java.util.*;public class Cale ndarPad exte nds JPa nelint year,m on th,day;Cale ndarMessage cal
33、e ndarMessage;JTextField showDay;JLabel title;String 星期=SUN 日,”MON ,”TUE 二,”WED 三,”THU 四,”FRI 五,”SAT 六; JPa nel no rth,ce nter;public Cale ndarPad()setLayout (new BorderLayout();n orth=new JPa nel();no rth.setLayout (new GridLayout(1,7);center=new JPa nel();cen ter.setLayout (new GridLayout(6,7);add
34、(ce nter,BorderLayout.CENTER);add( north,BorderLayout.NORTH);title=new JLabel7;for(i nt j=0;j7;j+)titlej=new JLabel();titlej.setFo nt(new Fon t(TimesRoma n,Fo nt.BOLD,12);titlej.setText(星期j);titlej.setHorizo ntalAlig nmen t(JLabel.CENTER); titlej.setBorder(BorderFactory.createRaisedBevelBorder(); no
35、rth.add(titlej);title0.setForegro un d(Color.red);title6.setForegro un d(Color.blue);public void setShowDayTextField(JTextField text) showDay=text;for(i nt i=0;ishowDa y.len gth;i+)showDayi.setFo nt(new Fon t(TimesRoma n,Fo nt.BOLD,15); showDayi.setHorizo ntalAlig nmen t(JTextField.CENTER); showDayi
36、.setEditable(false);cen ter.add(showDayi);public void setCale ndarMessage(Cale ndarMessage cale ndarMessage) this.cale ndarMessage=cale ndarMessage;public void showM on thCale ndar()String a=cale ndarMessage.getM on thCale ndar();for(int i=0;i42;i+)showDayi.setText(ai);validate();(4) /Notepad 类impor
37、t java.awt.*;import javax.swi ng.*;import java.io.*;import java.awt.eve nt.*;public class NotePad exte nds JPa nel impleme nts Actio nListe nerJTextArea text;JTextField showMessage;JPopupMe nu menu;JMe nultem itemCopy,itemCut,itemPaste,itemClear;public NotePad()showMessage=new JTextField();showMessa
38、ge.setHorizo ntalAlig nmen t(JTextField.CENTER); showMessage.setFo nt(new Fon t(TimesRoma n,Fo nt.BOLD,16); showMessage.setForegro un d(Color.blue); showMessage.setBackgro un d(Color.p in k);showMessage.setBorder(BorderFactory.createRaisedBevelBorder(); showMessage.setEditable(false);menu=new JPopup
39、Me nu();itemCopy=new JMenultem(” 复制); itemCut=new JMenultem(” 剪切);itemPaste=new JMenultem(” 粘贴); itemClear= new JMenultem(” 清空); itemCopy.addActio nListe ner(this);itemCut.addAct ion Liste ner(this); itemPaste.addAct ion Liste ner(this); itemClear.addActio nListe ner(this); menu .add(itemCopy);menu
40、.add(itemCut);menu .add(itemPaste);menu .add(itemClear);text=new JTextArea(10,10);text.addMouseListe ner(new MouseAdapter()public void mousePressed(MouseEve nt e) if(e.getModifiers()=In putEve nt.BUTTON3_MASK) menu .show(text,e.getX(),e.getY(); );setLayout (new BorderLayout();add(showMessage,BorderL
41、ayout.NORTH);add(new JScrollPane(text),BorderLayout.CENTER);public void setShowMessage(i nt year,i nt mon th,i nt day)showMessage.setText(+year+年+month+月+day+日);public void save(File dir,i nt year,i nt mon th,i nt day)Stri ng dailyC onten t=text.getText();String fileName=+year+month+day+.txt;String
42、key=+year+month+day;String dayFile=dir.list();boolea n boo=false;for(int k=O;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, JOpti onPan e.QUESTION_MESSA
43、GE);if(ok=JOptio nPan e.YES_OPTION)tryFile f=new File(dir,fileName);Ran domAccessFile out =new Ran domAccessFile(f,rw);long fileE nd=out.le ngth();byte bb=dailyCo nten t.getBytes();out.seek(fileE nd);out.write(bb);out.close();catch(IOExcepti on exp) elseString m=+year+年+month+月+day+还没有日志,保存日志吗?;int
44、ok=JOptionPane.showConfirmDialog(this,m,询问,JOptionPane.YES_NO_OPTION,JOpti onPan e.QUESTION_MESSAGE); if(ok=JOptio nPan e.YES_OPTION)tryFile f=new File(dir,fileName);Ran domAccessFile out =new Ran domAccessFile(f,rw);long fileE nd=out.le ngth();byte bb=dailyCo nten t.getBytes();out.write(bb);out.clo
45、se();catch(IOExcepti on exp) public void delete(File dir,i nt year,i nt mon th,i nt day)String key=+year+month+day;String dayFile=dir.list();boolea n boo=false;for(int k=O;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,JOpti onPan e.QUESTION_MESSAGE); if(ok=JOptio nPa ne.YES_OPTION)Stri ng fileNam
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 定制包装合同范本
- 潜江租房合同范本
- 室内布线合同范本
- 代理合同范本规范
- 海运旅客运输合同范本
- 包装印刷居间合同协议书版
- 2025年姿态控制发动机合作协议书
- 2025年海上风电合作协议书
- 2025-2030年中国生物质炭行业竞争格局及发展规划分析报告
- 2025-2030年中国烧烤炉市场未来发展趋势及前景调研分析报告
- 优质护理与人文关怀课件
- 溶剂油MSDS危险化学品安全技术说明书
- 马工程西方经济学(第二版)教学课件-2
- 慢阻肺的慢病管理课件
- (中职)化学分析技术项目一 走进化学分析实验室教学课件
- 探放水工培训教材
- 某县某年度高标准基本农田建设项目复核报告
- 秘书实务完整版课件全套ppt教程
- 酒店电子商务全套课件
- 质量体系的职能架构
- 《旅游经济学》全书PPT课件
评论
0/150
提交评论