用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(Fontf)方法可以設(shè)置組件上的字體,F(xiàn)ont類的構(gòu)造方法為:p

3、ublic Font(Stringname,intstyle,intsize),其中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è)置和獲取,設(shè)置信息條,對文本框進行設(shè)置,保存日志、

4、刪除日志和讀取日志按鈕的事件實現(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.swing.*;import java.awt.*;

5、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; int Weekday; NotePad note

6、pad=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 GridLayout(7,7); rightPa

7、nel=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(weekj); titlej.setBorde

8、r(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 JButton(刪除日志) ; Read=new JBu

9、tton(讀取日志) ; Read.addActionListener(this); Save.addActionListener(this); Delete.addActionListener(this); /設(shè)置監(jiān)視器 JPanel pSouth=new JPanel(); pSouth.add(Save); pSouth.add(Delete); pSouth.add(Read); Date=Calendar.getInstance(); Box box=Box.createHorizontalBox(); box.add(ChangeYear); box.add(ChangeMonth

10、); 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=getContentPane(); JSplitPane split=

11、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 objectOut=new ObjectOutputStre

12、am(out); objectOut.writeObject(hashtable); objectOut.close(); out.close(); catch(IOException e) setCalendar(year,month); addWindowListener(new WindowAdapter() /設(shè)置窗口監(jiān)視器 public void windowClosing(WindowEvent e) System.exit(0); ); setVisible(true); setBounds(100,60,524,335); validate(); public void act

13、ionPerformed(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) /設(shè)置日歷卡 Date.set(year,month-1,1); Weekday

14、=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 Arrange(Weekday,28); public v

15、oid 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); showDayi.setForeground(Color.b

16、lack); 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; notepad.setMonth(month);

17、 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.parseInt(source.getText(); no

18、tepad.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(String args) Calendar calenda

19、r=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 javax.swing.event.*;imp

20、ort 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.getMonth(); day=calendar

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

22、rLayout.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() return d

23、ay; 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) String LogCenter=text.getText(); String key=+year+mont

24、h+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+已存在日志,是否更新?; /是更新已有的日志 int ok=JOptionPane.sh

25、owConfirmDialog(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(); table.remove

26、(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,提示,JOptionPane.WARNING_MESSAGE)

27、; 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 ObjectInputStream(inOne

28、); 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(Exception ee) String f=日志保存成功

29、; JOptionPane.showMessageDialog(this,f,提示,JOptionPane.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.showConfirmDialog(this,m,詢問,JOptionPane.YES_NO

30、_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(); table.remove(key); FileOutputStream out=new FileOutputS

31、tream(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,提示,JOptionPane.WARNING_MESSAGE); else String f=+year+年+month+月+day+ 無記錄;

32、JOptionPane.showMessageDialog(this,f,提示,JOptionPane.WARNING_MESSAGE); public void Read(File file,int year,int month,int day) /讀取日志按鈕事件實現(xiàn) String key=+year+month+day; try FileInputStream inOne=new FileInputStream(file); ObjectInputStream inTwo=new ObjectInputStream(inOne); table=(Hashtable)inTwo.readO

33、bject(); inOne.close(); inTwo.close(); catch(Exception ee) if(table.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) text.setText(String)table.get(key); text

34、.setText(+year+年+month+月+day+日的日志); 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.*;public class Year extends Box implements Action

35、Listener 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.getYear(); Year2=new JButton(下年); Year1=new JButton(上年); add(Y

36、ear1); 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(+year); public int getYear() return year; public void actionPerformed(ActionEvent e) /上下年按鈕事件的實現(xiàn) if(

37、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(+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類/Mo

溫馨提示

  • 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. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論