1、用Java实现日历记事本用Java实现日历记事本用Java实现日历记事本1. 实验目的掌握RandomAccessFile类的使用;掌握字符输入、输出流和缓冲输入、输出流的使用。2. 实验要求 实验前,应事先熟悉相关知识点,拟出相应的实验操作步骤,明确实验目的和要求;实验过程中,服从实验指导教师安排,遵守实验室的各项规章制度,爱护实验仪器设备;实验操作完成后,认真书写实验报告,总结实验经验,分析实验过程中出现的问题。3. 实验内容编程实现日历记事本。具体要求如下:(1)该日历可以通过在文本框中输入年份和月份设置日期,也可按年前后翻动,用鼠标左键单击“上年”和“下年”按钮,将当前日历的年份减一和
2、加一。还可以在某年内按月前后翻动,用鼠标左键单击“上月”和“下月”按钮,将日历的月份减一和加一。(2)左键单击日历上的日期,可以通过右侧的记事本(文本区)编辑有关日志,并将日志保存到一个文件,该文件的名字是由当前日期组成的字符序列。用户可以读取、删除某个日期的日志,也可以继续向某个日志添加新的内容。在保存、删除和读取日志时都会先弹出一个确认对话框,以确认保存、删除和读取操作。(3)当某个日期有日志时,该日期以粗体16号字显示,表明这个日期有日志;当用户删除某个日期的日志后,该日期恢复原来的外观。实现效果图(参考)如下:提示:(1)组件调用public void setFont(Fontf)方法
3、可以设置组件上的字体,Font类的构造方法为:public Font(Stringname,intstyle,intsize),其中name是字体的名字,style决定字体的样式(如Font.BOLD表示粗体)size决定字体的大小。(具体请参考JDK API)(2)当左键单击日历上的日期时,如要获取该日期,可通过处理该组件上的鼠标事件来实现。4实验步骤、实施过程、关键代码、实验结果及分析说明等1. CalendarPad类(1) 进行整体布局,建立日历记事本文件,设置日历卡,把日期按星期顺序排列,并用窗口监视器实现。(2)用窗口监视器实现,结果如下:2. Notepad类(1) 对日期的设置
4、和获取,设置信息条,对文本框进行设置,保存日志、删除日志和读取日志按钮的事件实现。(2)保存日志按钮事件实现如下:(3)读取日志按钮事件实现如下:(4.)删除日志按钮事件实现如下:3 Year类(1)输入年份可以实现输出,给上下年按钮设置监视器,对上下年按钮事件的实现。(2)其结果如下:4. Month类(1)设置上下月监视器,对上下月按钮的事件实现,区分闰年和平年,对天数不同的月份进行分类。(2)其结果如下:(续上页,可加页)5 各个类的源代码如下:CalendarPad 类:/CalendarPad.javaimport java.util.Calendar;import javax.sw
5、ing.*;import java.awt.*;import java.awt.event.*;import java.io.*;import java.util.Hashtable;public class CalendarPad extends JFrame implements MouseListener,ActionListener int year,month,day; Hashtable hashtable; JButton Save,Delete,Read; File file; JTextField showDay; JLabel title; Calendar Date; i
6、nt Weekday; NotePad notepad=null; Month ChangeMonth; Year ChangeYear ; String week=星期日,星期一,星期二,星期三,星期四,星期五,星期六; JPanel leftPanel,rightPanel; public CalendarPad(int year,int month,int day) leftPanel=new JPanel(); JPanel leftCenter=new JPanel(); JPanel leftNorth=new JPanel(); leftCenter.setLayout(new
7、GridLayout(7,7); rightPanel=new JPanel(); this.year=year; this.month=month; this.day=day; ChangeYear=new Year(this); ChangeYear.setYear(year); ChangeMonth=new Month(this); ChangeMonth.setMonth(month); title=new JLabel7; showDay=new JTextField42; for(int j=0;j7;j+) titlej=new JLabel(); titlej.setText
8、(weekj); titlej.setBorder(BorderFactory.createRaisedBevelBorder(); titlej.setForeground(Color.blue); leftCenter.add(titlej); for(int i=0;i42;i+) showDayi=new JTextField(); showDayi.addMouseListener(this); showDayi.setEditable(false); leftCenter.add(showDayi); Save=new JButton(保存日志) ; Delete=new JBut
9、ton(删除日志) ; Read=new JButton(读取日志) ; Read.addActionListener(this); Save.addActionListener(this); Delete.addActionListener(this); /设置监视器 JPanel pSouth=new JPanel(); pSouth.add(Save); pSouth.add(Delete); pSouth.add(Read); Date=Calendar.getInstance(); Box box=Box.createHorizontalBox(); box.add(ChangeYe
10、ar); box.add(ChangeMonth); leftNorth.add(box); leftPanel.setLayout(new BorderLayout(); add(leftNorth,BorderLayout.NORTH); leftPanel.add(leftCenter,BorderLayout.CENTER); add(pSouth,BorderLayout.SOUTH); leftPanel.validate(); notepad=new NotePad(this); rightPanel.add(notepad); Container con=getContentP
11、ane(); JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel,rightPanel); con.add(split,BorderLayout.CENTER); /进行整体布局 con.validate(); hashtable=new Hashtable(); file=new File(日历记事本.txt); if(!file.exists() try FileOutputStream out=new FileOutputStream(file); ObjectOutputStream object
12、Out=new ObjectOutputStream(out); objectOut.writeObject(hashtable); objectOut.close(); out.close(); catch(IOException e) setCalendar(year,month); addWindowListener(new WindowAdapter() /设置窗口监视器 public void windowClosing(WindowEvent e) System.exit(0); ); setVisible(true); setBounds(100,60,524,335); val
13、idate(); public void actionPerformed(ActionEvent e) if(e.getSource()=Save) notepad.Save(file,year,month,day); else if(e.getSource()=Delete) notepad.Delete(file,year,month,day); else if(e.getSource()=Read) notepad.Read(file,year,month,day); public void setCalendar(int year,int month) /设置日历卡 Date.set(
14、year,month-1,1); Weekday=Date.get(Calendar.DAY_OF_WEEK)-1; if(month=1|month=2|month=3|month=5|month=7 |month=8|month=10|month=12) Arrange(Weekday,31); else if(month=4|month=6|month=9|month=11) Arrange(Weekday,30); else if(month=2) if(year%4=0&year%100!=0)|(year%400=0) Arrange(Weekday,29); else Arran
15、ge(Weekday,28); public void Arrange(int Weekday,int Monthday) /把日期,按星期排列 for(int i=Weekday,n=1;iWeekday+Monthday;i+) showDayi.setText(+n); if(n=day) showDayi.setForeground(Color.blue); showDayi.setFont(new Font(TimesRoman,Font.BOLD,20); else showDayi.setFont(new Font(TimesRoman,Font.BOLD,12); showDa
16、yi.setForeground(Color.black); n+; for(int i=0;iWeekday;i+) showDayi.setText(); for(int i=Weekday+Monthday;i42;i+) showDayi.setText(); public int getYear() return year; public void setYear(int y) year=y; notepad.setYear(year); public int getMonth() return month; public void setMonth(int m) month=m;
17、notepad.setMonth(month); public int getDay() return day; public void setDay(int d) day=d; notepad.setDay(day); public Hashtable getHashtable() return hashtable; public File getFile() return file; public void mousePressed(MouseEvent e) JTextField source=(JTextField)e.getSource();try day=Integer.parse
18、Int(source.getText(); notepad.setDay(day); notepad.setNews(year,month,day); notepad.setText(null); catch(Exception ee) public void mouseClicked(MouseEvent e)public void mouseReleased(MouseEvent e)public void mouseExited(MouseEvent e)public void mouseEntered(MouseEvent e)public static void main(Strin
19、g args) Calendar calendar=Calendar.getInstance(); int y=calendar.get(Calendar.YEAR); int m=calendar.get(Calendar.MONTH)+1; int d=calendar.get(Calendar.DAY_OF_MONTH); new CalendarPad(y,m,d); NotePad类/ NotePad.javaimport java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;import
20、 javax.swing.event.*;import java.io.*;public class NotePad extends JPanel implements ActionListener JTextArea text; Hashtable table; JLabel News; int year,month,day; File file; CalendarPad calendar; public NotePad(CalendarPad calendar) this.calendar=calendar; year=calendar.getYear(); month=calendar.
21、getMonth(); day=calendar.getDay(); table=calendar.getHashtable(); file=calendar.getFile(); News=new JLabel(+year+年+month+月+day+日,JLabel.CENTER); News.setFont(new Font(TimesRoman,Font.BOLD,20); text=new JTextArea(10,15); setLayout(new BorderLayout(); JPanel pSouth=new JPanel(); add(News,BorderLayout.
22、NORTH); add(pSouth,BorderLayout.SOUTH); add(new JScrollPane(text),BorderLayout.CENTER); public void setYear(int year) this.year=year; public int getYear() return year; public void setMonth(int month) this.month=month; public int getMonth() return month; public void setDay(int day) this.day=day; publ
23、ic int getDay() return day; public void setNews(int year,int month,int day) News.setText(+year+年+month+月+day+日); public void setText(String s) text.setText(s); public void actionPerformed(ActionEvent e) public void Save(File file,int year,int month,int day) /保存日志按钮事件实现 String LogCenter=text.getText(
24、); String key=+year+month+day; try FileInputStream inOne=new FileInputStream(file); ObjectInputStream inTwo=new ObjectInputStream(inOne); table=(Hashtable)inTwo.readObject(); inOne.close(); inTwo.close(); catch(Exception ee) if(table.containsKey(key) String m=+year+年+month+月+day+已存在日志,是否更新?; /是更新已有的
25、日志 int ok=JOptionPane.showConfirmDialog(this,m,询问,JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if(ok=JOptionPane.YES_OPTION) try FileInputStream inOne=new FileInputStream(file); ObjectInputStream inTwo=new ObjectInputStream(inOne); table=(Hashtable)inTwo.readObject(); inOne.close(); inT
26、wo.close(); table.remove(key); FileOutputStream out=new FileOutputStream(file); ObjectOutputStream objectOut=new ObjectOutputStream(out); objectOut.writeObject(table); objectOut.close(); out.close(); text.setText(null); catch(Exception ee) String f=日志已更新; JOptionPane.showMessageDialog(this,f,提示,JOpt
27、ionPane.WARNING_MESSAGE); 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 FileInputStream inOne=new FileInputStream(file); ObjectInputStream inTwo=new
28、 ObjectInputStream(inOne); table=(Hashtable)inTwo.readObject(); inOne.close(); inTwo.close(); table.put(key,LogCenter); FileOutputStream out=new FileOutputStream(file); ObjectOutputStream objectOut=new ObjectOutputStream(out); objectOut.writeObject(table); objectOut.close(); out.close(); catch(Excep
29、tion ee) String f=日志保存成功; JOptionPane.showMessageDialog(this,f,提示,JOptionPane.WARNING_MESSAGE); public void Delete(File file,int year,int month,int day) /删除日志按钮事件实现 String key=+year+month+day; if(table.containsKey(key) String m=删除+year+年+month+月+day+日的日志吗?; int yes=JOptionPane.showConfirmDialog(this,m,询问,JOptionPane.YES_N