1、java课程设计报告实用万年历记事本java课程设计报告-实用万年历记事本Java应用技术实践报告 题 目 实用万年历记事本 起讫日期 所在院系 学生姓名 专 业 班 级 学 号 指导教师 职 称 所在单位 2010年 1 月 11 日一(总体设计 NotePad 1(类之间的关系如图1-1所示。 CalendarWindow Clock CalendarImagCalendarMessCalendarPad e age 图1-1 类之间的关系 2(java源文件及其功能如表1-1所示。 表1-1 java源文件及其功能 序文件名 主要功能 备注 号 CalendarWindow.jav创建实
2、用万年历记事本程序的主窗含有main方法 1 a 口 CalendarMessage.ja该文件用来刻画和“日期”有关的数 2 va 据 该文件负责显示和修改 3 CalendarPad.java CalendarMessage对象中的日期数据 该文件负责提供编辑、读取、保存和 4 NotePad.java 删除日志的 该文件负责绘制图像 5 CalendarImage.java 该文件负责显示时钟 6 Clock.java - 1 - 二(详细设计 1( CalendarWindow (1)成员变量 主要成员变量(属性) 成员变量描述 变量类型 名称 年、月、日 Year,month,day
3、 Int 显示日期 showDay JTextField 日历对象 calendarPad CalendarPad 日志对象 notepad NotePad 月 JTextField ShowMonth 年 JTextField ShowYear 图像对象 CalendarImage calendarImage 时钟对象 Clock clock 处理日期 CalendarMessae calendarMessae nextYear、nextMonth、previousYear、previousMonth、按钮对象 JButton saveDailyRecord、deleteDailyRecor
4、d、readDailyRecord (2)方法见表 主要方法 方法名 功能 备注 CalendarWindow 创建程序主窗口 构造方法 doMark 对日志日期做出标记 focusGained 处理FocusListener事件 mousePressed 处理MouseEvent事件 处理ActionEvent事件 actionPerformed 接口方法 程序开始运行 main - 2 - (3)源代码见文件CalendarWindow 二(CalendarMessage (1)成员变量见表 表1-4 主要成员变量(属性) 成员变量描述 变量类型 名称 记录日志的年、月、日 int yea
5、r、month、day (2)方法见表1-5 表1-5 主要方法 方法名 功能 备注 SetDay、getDay 设置、获取日期 setMonth/getMonth 设置、获取月份 setYear、getYear 设置、获取年份 getMonthCalendar() 处理日期数据 (3)源代码见文件CalendarMessage.java import java.util.Calendar; public class CalendarMessage int year=-1,month=-1,day=-1; public void setYear(int year) this.year=year
6、; 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; - 3 - Calendar
7、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)
8、&(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; 3(CalendarPad (1)成员变量见表 表1-6 主要成员变量(属性) 成员变量描述 变量类型 名称 确定年份、月份、日期的整数 year、month、day Int calend
9、arPad 处理和日期有关的数据 CalendarPad showDay 显示日期 JTextField 星期 String (2)方法见表 表1-7 主要方法 方法名 功能 备注 CalendarPad 创建calendarPad对象 构造方法 - 4 - setShowDayTextField负责设置showDay数组 (JTextField) setCalendarMes负责设置sage(CalendarM calendarMessage对象 essage) showMonthCalen负责显示“日历” dar() (3)源代码见文件CalendarPad.java import jav
10、ax.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
11、(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.
12、BOLD,12); j); titlej.setText(星期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;isho
13、wDay.length;i+) showDayi.setFont(new Font(TimesRoman,Font.BOLD,15); showDayi.setHorizontalAlignment(JTextField.CENTER); - 5 - showDayi.setEditable(false); center.add(showDayi); public void setCalendarMessage(CalendarMessage calendarMessage) this.calendarMessage=calendarMessage; public void showMonth
14、Calendar() String a=calendarMessage.getMonthCalendar(); for(int i=0;i42;i+) showDayi.setText(ai); validate(); 4(NotePad 1)成员变量见表1-8 (表1-8 主要成员变量(属性) 成员变量描述 变量类型 名称 JTextArea 可编辑多行文本 text 显示和日期有关的数据 showMessage JTextField menu JpopupMenu 下月,上月 itmeCopy、itmeCut、复制,剪切,粘贴,清空 JMenuItem itemPaste、itmeClea
15、r (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.java - 6 - import java.awt.*; import javax.swing.*; import java.io.*; import java.awt.even
16、t.*; 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 Fo
17、nt(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 JMenuI
18、tem(粘贴); 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.addMouseList
19、ener(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
20、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; - 7 - String key=+year+month+day; String dayFile=dir.list(); boolean boo=false; for(int k=0;kdayFile.length;k+) i
21、f(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 Rando
22、mAccessFile(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)
23、; 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+
24、month+day; - 8 - 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=JO
25、ptionPane.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 fileNam
26、e=+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,
27、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) - 9 - text.append(s+n); inOne.close(); inTwo.close()
28、; 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.
29、paste(); else if(e.getSource()=itemClear) text.setText(null); 5(CalendarImage 1)成员变量见表1-8 (表1-8 主要成员变量(属性) 成员变量描述 变量类型 名称 File 存放图像文件的引用 imageFile Image Image 负责创建image对象 Toolkit tool (2)方法见表9 表9 主要方法 方法名 功能 备注 CalendarImage创建calendarImage对构造方法 () 象 负责设置imageFile对setImageFile象,根据imageFile创 (File) 建image paintComponent负责绘制image - 10 - (Graphics) (3)源代码见文件Calendar
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1