用Java實現(xiàn)日歷記事本_第1頁
用Java實現(xiàn)日歷記事本_第2頁
用Java實現(xiàn)日歷記事本_第3頁
用Java實現(xiàn)日歷記事本_第4頁
用Java實現(xiàn)日歷記事本_第5頁
已閱讀5頁,還剩15頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、用Java實現(xiàn)日歷記事本1. 實驗?zāi)康恼莆誖andomAccessFile類的使用;掌握字符輸入、輸出流和緩沖輸入、輸出流的使用。2. 實驗要求 實驗前,應(yīng)事先熟悉相關(guān)知識點,擬出相應(yīng)的實驗操作步驟,明確實驗?zāi)康暮鸵螅粚嶒炦^程中,服從實驗指導(dǎo)教師安排,遵守實驗室的各項規(guī)章制度,愛護實驗儀器設(shè)備;實驗操作完成后,認真書寫實驗報告,總結(jié)實驗經(jīng)驗,分析實驗過程中出現(xiàn)的問題。3. 實驗內(nèi)容編程實現(xiàn)日歷記事本。具體要求如下:(1)該日歷可以通過在文本框中輸入年份和月份設(shè)置日期,也可按年前后翻動,用鼠標(biāo)左鍵單擊“上年”和“下年”按鈕,將當(dāng)前日歷的年份減一和加一。還可以在某年內(nèi)按月前后翻動,用鼠標(biāo)左鍵單擊

2、“上月”和“下月”按鈕,將日歷的月份減一和加一。(2)左鍵單擊日歷上的日期,可以通過右側(cè)的記事本(文本區(qū))編輯有關(guān)日志,并將日志保存到一個文件,該文件的名字是由當(dāng)前日期組成的字符序列。用戶可以讀取、刪除某個日期的日志,也可以繼續(xù)向某個日志添加新的內(nèi)容。在保存、刪除和讀取日志時都會先彈出一個確認對話框,以確認保存、刪除和讀取操作。(3)當(dāng)某個日期有日志時,該日期以粗體16號字顯示,表明這個日期有日志;當(dāng)用戶刪除某個日期的日志后,該日期恢復(fù)原來的外觀。實現(xiàn)效果圖(參考)如下:提示:(1)組件調(diào)用public void setFont(Font f)方法可以設(shè)置組件上的字體,F(xiàn)ont類的構(gòu)

3、造方法為:public Font(String name,int style,int size),其中name是字體的名字,style決定字體的樣式(如Font.BOLD表示粗體)size決定字體的大小。(具體請參考JDK API)(2)當(dāng)左鍵單擊日歷上的日期時,如要獲取該日期,可通過處理該組件上的鼠標(biāo)事件來實現(xiàn)。4實驗步驟、實施過程、關(guān)鍵代碼、實驗結(jié)果及分析說明等1. CalendarPad類(1) 進行整體布局,建立日歷記事本文件,設(shè)置日歷卡,把日期按星期順序排列,并用窗口監(jiān)視器實現(xiàn)。(2)用窗口監(jiān)視器實現(xiàn),結(jié)果如下:2. Notepad類(1) 對日期的設(shè)置

4、和獲取,設(shè)置信息條,對文本框進行設(shè)置,保存日志、刪除日志和讀取日志按鈕的事件實現(xiàn)。(2)保存日志按鈕事件實現(xiàn)如下:(3)讀取日志按鈕事件實現(xiàn)如下:(4.)刪除日志按鈕事件實現(xiàn)如下:3 Year類(1)輸入年份可以實現(xiàn)輸出,給上下年按鈕設(shè)置監(jiān)視器,對上下年按鈕事件的實現(xiàn)。(2)其結(jié)果如下:4. Month類(1)設(shè)置上下月監(jiān)視器,對上下月按鈕的事件實現(xiàn),區(qū)分閏年和平年,對天數(shù)不同的月份進行分類。(2)其結(jié)果如下:(續(xù)上頁,可加頁)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(); JPane

7、l leftCenter=new JPanel(); JPanel leftNorth=new JPanel(); leftCenter.setLayout(new 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 JLabel

8、7; showDay=new JTextField42; for(int j=0;j<7;j+) titlej=new JLabel(); titlej.setText(weekj); titlej.setBorder(BorderFactory.createRaisedBevelBorder(); titlej.setForeground(Color.blue); leftCenter.add(titlej); for(int i=0;i<42;i+) showDayi=new JTextField(); showDayi.addMouseListener(this); show

9、Dayi.setEditable(false); leftCenter.add(showDayi); Save=new JButton("保存日志") ; Delete=new JButton("刪除日志") ; Read=new JButton("讀取日志") ; Read.addActionListener(this); Save.addActionListener(this); Delete.addActionListener(this); /設(shè)置監(jiān)視器 JPanel pSouth=new JPanel(); pSouth.ad

10、d(Save); pSouth.add(Delete); pSouth.add(Read); Date=Calendar.getInstance(); Box box=Box.createHorizontalBox(); box.add(ChangeYear); box.add(ChangeMonth); leftNorth.add(box); leftPanel.setLayout(new BorderLayout(); add(leftNorth,BorderLayout.NORTH); leftPanel.add(leftCenter,BorderLayout.CENTER); add(

11、pSouth,BorderLayout.SOUTH); leftPanel.validate(); notepad=new NotePad(this); rightPanel.add(notepad); Container con=getContentPane(); JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel,rightPanel); con.add(split,BorderLayout.CENTER); /進行整體布局 con.validate(); hashtable=new Hashtabl

12、e(); file=new File("日歷記事本.txt"); if(!file.exists() try FileOutputStream out=new FileOutputStream(file); ObjectOutputStream objectOut=new ObjectOutputStream(out); objectOut.writeObject(hashtable); objectOut.close(); out.close(); catch(IOException e) setCalendar(year,month); addWindowListene

13、r(new WindowAdapter() /設(shè)置窗口監(jiān)視器 public void windowClosing(WindowEvent e) System.exit(0); ); setVisible(true); setBounds(100,60,524,335); validate(); public void actionPerformed(ActionEvent e) if(e.getSource()=Save) notepad.Save(file,year,month,day); else if(e.getSource()=Delete) notepad.Delete(file,y

14、ear,month,day); else if(e.getSource()=Read) notepad.Read(file,year,month,day); public void setCalendar(int year,int month) /設(shè)置日歷卡 Date.set(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(mo

15、nth=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 Arrange(Weekday,28); public void Arrange(int Weekday,int Monthday) /把日期,按星期排列 for(int i=Weekday,n=1;i<Weekday+Monthday;i+) showDayi.setText(""+n

16、); if(n=day) showDayi.setForeground(Color.blue); showDayi.setFont(new Font("TimesRoman",Font.BOLD,20); else showDayi.setFont(new Font("TimesRoman",Font.BOLD,12); showDayi.setForeground(Color.black); n+; for(int i=0;i<Weekday;i+) showDayi.setText(""); for(int i=Weekda

17、y+Monthday;i<42;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; notepad.setMonth(month); public int getDay() return day; public void setDay(int

18、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.parseInt(source.getText(); notepad.setDay(day); notepad.setNews(year,month,day); note

19、pad.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(String args) Calendar calendar=Calendar.getInstance(); int y=calendar.get(Calendar.YE

20、AR); 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 javax.swing.event.*;import java.io.*;public class NotePad extends JPanel implem

21、ents 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.getMonth(); day=calendar.getDay(); table=calendar.getHashtable(); file=calendar.

22、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.NORTH); add(pSouth,B

23、orderLayout.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; public int getDay() retu

24、rn 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) /保存日志按鈕事件實現(xiàn) St

25、ring LogCenter=text.getText(); 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.

26、containsKey(key) 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 FileInp

27、utStream(file); ObjectInputStream inTwo=new ObjectInputStream(inOne); table=(Hashtable)inTwo.readObject(); inOne.close(); inTwo.close(); table.remove(key); FileOutputStream out=new FileOutputStream(file); ObjectOutputStream objectOut=new ObjectOutputStream(out); objectOut.writeObject(table); objectO

28、ut.close(); out.close(); text.setText(null); catch(Exception ee) String f="日志已更新" JOptionPane.showMessageDialog(this,f,"提示",JOptionPane.WARNING_MESSAGE); else String m=""+year+"年"+month+"月"+day+"還沒有日志,是否保存日志?" /保存還沒有的日志 int ok=JOptionPane.s

29、howConfirmDialog(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(); inTwo.close();

30、 table.put(key,LogCenter); FileOutputStream out=new FileOutputStream(file); ObjectOutputStream objectOut=new ObjectOutputStream(out); objectOut.writeObject(table); objectOut.close(); out.close(); catch(Exception ee) String f="日志保存成功" JOptionPane.showMessageDialog(this,f,"提示",JOpt

31、ionPane.WARNING_MESSAGE); public void Delete(File file,int year,int month,int day) /刪除日志按鈕事件實現(xiàn) String key=""+year+""+month+""+day; if(table.containsKey(key) String m="刪除"+year+"年"+month+"月"+day+"日的日志嗎?" int yes=JOptionPane.showCon

32、firmDialog(this,m,"詢問",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if(yes=JOptionPane.YES_OPTION) try FileInputStream inOne=new FileInputStream(file); ObjectInputStream inTwo=new ObjectInputStream(inOne); table=(Hashtable)inTwo.readObject(); inOne.close(); inTwo.close(); tabl

33、e.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,"提示&qu

34、ot;,JOptionPane.WARNING_MESSAGE); else String f=""+year+"年"+month+"月"+day+" 無記錄" JOptionPane.showMessageDialog(this,f,"提示",JOptionPane.WARNING_MESSAGE); public void Read(File file,int year,int month,int day) /讀取日志按鈕事件實現(xiàn) String key=""+year+&

35、quot;"+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+"年"+mon

36、th+"月"+day+"已有日志,是否讀取?" int ok=JOptionPane.showConfirmDialog(this,m,"詢問",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE); if(ok=JOptionPane.YES_OPTION) text.setText(String)table.get(key); text.setText(""+year+"年"+month+"月"+day+"

37、;日的日志"); else text.setText(""); else String f=""+year+"年"+month+"月"+day+"日,暫無記錄" JOptionPane.showMessageDialog(this,f,"提示",JOptionPane.WARNING_MESSAGE); Year類/Year.java其源代碼為:import javax.swing.*;import java.awt.*;import java.awt.event.

38、*;public class Year extends Box implements ActionListener int year; JTextField showYear=null; JButton Year1,Year2; CalendarPad CAL; public Year(CalendarPad CAL) super(BoxLayout.X_AXIS); showYear=new JTextField(4); showYear.setFont(new Font("TimesRomn",Font.BOLD,14);this.CAL=CAL; year=CAL.g

39、etYear(); Year2=new JButton("下年"); Year1=new JButton("上年"); add(Year1); add(showYear); add(Year2); showYear.addActionListener(this); Year1.addActionListener(this); Year2.addActionListener(this); /設(shè)置監(jiān)視器 public void setYear(int year) this.year=year; showYear.setText(""+ye

40、ar); public int getYear() return year; public void actionPerformed(ActionEvent e) /上下年按鈕事件的實現(xiàn) if(e.getSource()=Year1) /上年按鈕 year=year-1; showYear.setText(""+year); CAL.setYear(year); CAL.setCalendar(year,CAL.getMonth(); else if(e.getSource()=Year2) /下年按鈕 year=year+1; showYear.setText("

41、;"+year); CAL.setYear(year); CAL.setCalendar(year,CAL.getMonth(); else if(e.getSource()=showYear) /輸入年份 try year=Integer.parseInt(showYear.getText(); showYear.setText(""+year); CAL.setYear(year); CAL.setCalendar(year,CAL.getMonth(); catch(NumberFormatException ee) showYear.setText(""+year); CAL.setYear(year); CAL.setCalendar(year,CAL.getMonth(); Month類/Mont

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論