java课程设计报告实用万历记事本.doc

上传人:仙人指路1688 文档编号:2387037 上传时间:2023-02-17 格式:DOC 页数:18 大小:1,002KB
返回 下载 相关 举报
java课程设计报告实用万历记事本.doc_第1页
第1页 / 共18页
java课程设计报告实用万历记事本.doc_第2页
第2页 / 共18页
java课程设计报告实用万历记事本.doc_第3页
第3页 / 共18页
java课程设计报告实用万历记事本.doc_第4页
第4页 / 共18页
java课程设计报告实用万历记事本.doc_第5页
第5页 / 共18页
点击查看更多>>
资源描述

《java课程设计报告实用万历记事本.doc》由会员分享,可在线阅读,更多相关《java课程设计报告实用万历记事本.doc(18页珍藏版)》请在三一办公上搜索。

1、Java应用技术实践报告题 目 实用万年历记事本 起讫日期 所在院系 学生姓名 专 业 班 级 学 号 指导教师 职 称 所在单位 2010年 1 月 11 日一总体设计1类之间的关系如图1-1所示。CalendarWindowCalendarImageCalendarMessageCalendarPadClockNotePad图1-1 类之间的关系2java源文件及其功能如表1-1所示。表1-1 java源文件及其功能序号文件名主要功能备注1CalendarWindow.java创建实用万年历记事本程序的主窗口含有main方法2CalendarMessage.java该文件用来刻画和“日期”

2、有关的数据3CalendarPad.java该文件负责显示和修改 CalendarMessage对象中的日期数据4NotePad.java该文件负责提供编辑、读取、保存和删除日志的5CalendarImage.java该文件负责绘制图像6Clock.java该文件负责显示时钟二详细设计1 CalendarWindow(1)成员变量主要成员变量(属性)成员变量描述变量类型名称年、月、日IntYear,month,day显示日期JTextFieldshowDay日历对象CalendarPadcalendarPad日志对象NotePadnotepad月JTextFieldShowMonth年JTex

3、tFieldShowYear图像对象CalendarImagecalendarImage时钟对象Clockclock 处理日期CalendarMessaecalendarMessae按钮对象JButtonnextYear、nextMonth、previousYear、previousMonth、saveDailyRecord、deleteDailyRecord、readDailyRecord(2)方法见表主要方法方法名功能备注CalendarWindow创建程序主窗口构造方法doMark对日志日期做出标记focusGained处理FocusListener事件mousePressed处理Mou

4、seEvent事件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.j

5、avaimport 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 setD

6、ay(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

7、|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=星期几+dayAmo

8、unt;i42;i+) dayi=; return day; 3CalendarPad(1)成员变量见表表1-6 主要成员变量(属性)成员变量描述变量类型名称确定年份、月份、日期的整数Intyear、month、day处理和日期有关的数据CalendarPadcalendarPad显示日期JTextFieldshowDay星期String(2)方法见表表1-7 主要方法方法名功能备注CalendarPad创建calendarPad对象构造方法setShowDayTextField(JTextField)负责设置showDay数组setCalendarMessage(CalendarMessag

9、e)负责设置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日,M

10、ON一,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;

11、 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)

12、; 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 c

13、alendarMessage) 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下月,

14、上月复制,剪切,粘贴,清空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 j

15、ava.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)

16、; 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 JMenuIt

17、em(剪切); 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 JTextAr

18、ea(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 s

19、etShowMessage(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;

20、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); RandomAcc

21、essFile 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, JOptionP

22、ane.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 d

23、ay) 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_MES

24、SAGE); 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)

25、 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.

26、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(); inTw

27、o.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()=itemPas

28、te) 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创建imagepaintCompone

29、nt(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(imageF

30、ile.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(Graphi

31、cs)负责设置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);

32、 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

33、 stop() clock=null;public void paint(Graphics g) Graphics2D g2=(Graphics2D)g; Calendar now=new GregorianCalendar(); String timeInfo=; int hour=now.get(Calendar.HOUR_OF_DAY); int minute=now.get(Calendar.MINUTE); int second=now.get(Calendar.SECOND); if(hour=9) timeInfo+=0+hour+:; else timeInfo+=hour+:

34、; if(minute=9) timeInfo+=0+minute+:; else timeInfo+=minute+:; if(second=9) timeInfo+=0+second; else timeInfo+=second; g.setColor(Color.PINK); Dimension dim=getSize(); g.fillRect(0,0,dim.width,dim.height); g.setColor(Color.BLACK); g.drawString(timeInfo,40,80);三运行效果1使用eclipse软件运行该系统后,即可进入实用万年历记事本程序的主窗

35、口。2通过点击“上年”“下年”“上月”“下月”可以选择查看往年日期,如图查看2009年1月的日历,单击“上年”,如图1-3所示。图1-3 查看2009年1月的日历3,记录当天日志。在记事本中添加文本“5号报到”并保存,界面如图1-4所示。图1-4 添加文本界面4点击“是(Y)”,保存日志.当日期再度回到3月5日,日历上出现“有日志”,提示用户该日有日志。如图1-5图1-5 5点击读取日志,再点击“是(Y)”,查看日志图1-6 查看日志界面四工作总结经过了长时间的反复编写调试,课本和网上查阅相关资料相关文章,终于完成了整个课程设计。在这个过程中,在把书本上的知识给系统的复习了一下的同时,同时也发现了自己的太多不足,对JAVA2语言整体知识构架不

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号