Java日歷記事本課程設(shè)計報告_第1頁
Java日歷記事本課程設(shè)計報告_第2頁
Java日歷記事本課程設(shè)計報告_第3頁
Java日歷記事本課程設(shè)計報告_第4頁
Java日歷記事本課程設(shè)計報告_第5頁
已閱讀5頁,還剩18頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、Java日歷記事本課程設(shè)計報告在設(shè)計日歷記事本時,需要編寫6個JAVA源文件:CalendarWindow. java CaIendarPad java 、 NotePad java 、 Calendarlmage java 、 Clock java 話口 CaIendarMesssage java效果圖如下.CaIendarWi ndow 類 mport javax swing *; mport java awt. *;mport java awt. event *; mport java util.*;import java io. *;pub Iic cI ass CaIendarWi

2、ndowextendsJFrameimpIementsAct i onLi stener,MouseLi stener, FocusLi stenerint year,month, day;CalendarMessage calendarMessage;CalendarPad calendarPad;NotePad notePad;JTextField showYear, showMonth;JTextField showDay;CalendarImage calendarImage;String picturename;Clock clock;JButton nextYear,previou

3、sYear,nextMonth,previousMonth;JButton saveDa iIyRecord,de IeteDa iIyRecord,readDa iIyRecord;JButton getPicture;File dir;Color backColor=Color, white ; pub I ic CalendarWindowO d i r=new File(. /daiIyRecordu);di r. mkdi r ();showDay=new JTextF i eId42;for (int i二0;ishowDay. I ength;i +) showDayi=new

4、JTextFieId ();showDayi setBackground (backCoI or);showDayi setLayout (new Gr idLayout (3,3);showDayi addMouseL i stener (th i s);showDayi addFocusL i stener (th i s);caIendarMessage=new CaIendarMessage ();caIendarPad=new CaIendarPad(); notePad=new NotePad ();CaIendar caIendar二CaIendar get Instance()

5、;ca I endar setT ime (new Date (); year=caIendar get(CaIendar YEAR); month二caIendar. get(CaIendar. MONTH)+1; day=caIendar. get (CaIendar. DAY_0F_M0NTH);caIendarMessage setYear(year);caIendarMessage setMonth(month);caIendarMessage setDay(day);caIendarPad setCaIendarMessage(caIendarMessage);caIendarPa

6、d setShowDayTextFi eId(showDay);notePad setShowMessage (year, month, day);caIendarPad showMonthCaIendar();doMark ();caIendarI mage二new CaIendarImage();caIendarImage. set ImageFiIe(new FiIe (HfIower jpg);cIock=new Clock ();JSpIitPanespIitV1=new JSpIi tPane(JSpIi tPane. VERT I CAL SPLIT, caIendarPad,

7、caIendarImage);JSpIitPanespIi tV2=newJSpIi tPane (JSpIitPane. VERT ICAL_SPLIT, notePad, cIock);JSpIitPanespIitH=newJSpIitPane (JSpIitPane.HORIZONTALSPLIT,splitV1,splitV2);add (spIitH,BorderLayout. CENTER);showYear=new JTextField(+year,6);showYear setFont (new Font (TimesRoman, Font BOLD, 12); showYe

8、ar. setHor i zontaI Alignment(JTextF i eId. CENTER); showMonth=new JTextField(M+month,4);showMonth. setFont (new Font (HTimesRomanH,Font. BOLD, 12); showMonth. setHor i zontaI Ali gnment (JTextF i eId. CENTER); nextYear=new JButton (H 下年”); previousYear=new JButton(H Ji年”);nextMonth=new JButton (n 下

9、月);previousMonth=new JButton(uJh月);nextYear addAct i onLi stener(thi s); prev i ousYear addAct i onL i stener (th i s); nextMonth. addAct ionL i stener(thi s); prev i ousMonth. addAct i onL i stener(th i s); JPaneI north二new JPaneI ();north. add (previousYear);north. add (showYear);north. add (nextY

10、ear);north.add (previousMonth);north. add (showMonth);north. add (nextMonth);add (north,BorderLayout. NORTH);saveDai lyRecord=new JButton (H保存日志);de I eteDa i I yRecord=new JButton (H 刪除日志);readDai lyRecord=new JButton (H讀取日志); saveDa iIyRecord addAct i onL i stener(th i s); de IeteDa iIyRecord addA

11、ct i onL i stener(th i s); readDa i IyRecord addAct i onL i stener (th i s);JPaneI pSouth=new JPaneI ();pSouth. add (saveDai IyRecord);pSouth. add (deleteDaiIyRecord);pSouth. add (readDai IyRecord);add (pSouth,BorderLayout. SOUTH);getPicture=new JButton (選擇日歷圖像); getPicture addAct i onL i stener(thi

12、 s);pSouth. add (getPicture);add (pSouth,BorderLayout. SOUTH);setVisible(true);setBounds (60, 60, 660, 480);va I idate (); setDefauItCIoseOperat i on(JFrame. EX IT ON CLOSE);) pub Iic void actionPerformed (ActionEvent e) i f (e get Source () =nex tY ear) year+;showYear. setText (+year);caIendarMessa

13、ge setYear (year);calendarPad setCaIendarMessage (caIendarMessage); ca I endarPad showMorithCa I endar ();notePad setShowMessage (year, month,day); doMark ();e I se i f (e get Source ()二二 prev i ousYear) year;showYear setText(H+year);caIendarMessage setYear (year);calendarPad setCalendarMessage (cal

14、endarMessage); calendarPad showMonthCalendar ();notePad setShowMessage (year, month,day); doMark ();e I se i f (e. get Source () =nex tMonth) month+;if(month1) month=12; showMonth. setText(”+month);calendarMessage setMonth (month);calendarPad setCalendarMessage (calendarMessage); calendarPad showMon

15、thCalendar ();notePad setShowMessage (year,month,day); doMark ();1e I se i f (e. get Source () =prev i ousMon th) month-一;i f (month1) month=12; showMonth. setText(+month);ca lendarMessage setMonth (morith);calendarPad setCalendarMessage (calendarMessage);caIendarPad showMonthCaIendar ();notePad set

16、ShowMessage (year,month,day); doMark ();e I se i f (e. get Source ()二二showYear) Str ing s=showYear. getText () tr im();char a=s.toCharArrayO;boolean boo=faIse;for (i nt i 二0;ia I ength;i+)if(!(Character. isDigit (ai)boo二true;if (boo二二true)JOpt i onPane. showMessageD i a I og (th i s,您輸入了 非 法年份11, ”警

17、告11, JOpt i onPane. WARN I NGMESSAGE);eIse if (boo二二fa Ise)year=lnteger parseint(s);showYear setText(+year);caIendarMessage setYear (year);calendarPad setCalendarMessage (calendarMessage);caIendarPad showMonthCaIendar ();notePad setShowMessage (year,month,day); doMark ();eIse i f (e getSource ()二二sa

18、veDa i IyRecord) not ePad save (d i r, year, mon th, day); doMark ();eIse i f (e getSource ()二二de IeteDa i IyRecord) not ePad de I ete (d i r, year, mon th, day); doMark ();e I se i f (e. get Source () =readDa i I yRecord) not ePad read (d i r, year, mon th, day);e I se i f (e. get Source () =ge tPi

19、c ture ) Fi leDialog fd=new Fi leDialog(this,打開文件對話框”);fd. setV i s i b I e (true);String fileopen = null, filename = null;f i I eopen = fd get Di rec toryO;f i I ename = f d getF i I e ();caIendarI mage set ImageF iIe (new F iIe (f iIeopen, f iI ename);pub Iic void mousePressed(MouseEvent e) JTextF

20、ield text二(JTextField)e. getSource (); Str ing str 二 text, get Tex t () t r im();try day=Integer.parselnt (str);)catch (NumberFormatException exp) )calendarMessage setDay (day);notePad setShowMessage (year, month,day);pub Iic void pub Iic void pub Iic void pub Iic void pub Iic voidmouseReI eased (Mo

21、useEvent e) mouseEntered (MouseEvent e) mouseEx ited (MouseEvent e) mouseCIicked (MouseEvent e) ) focusGa i ned (FocusEvent e) Compo nent = (Compo nent) e. get Source ();.setBackground (Color.pink);pub Iic void focusLost(FocusEvent e) Compo nent 二(Compo nent) e. get Source (); setBackground (backCol

22、or);)pub I ic void doMark () for (i nt i 二0;i showDay I ength;i+) showDayi. removeAl I ();St r ing st r=showDay i get Tex t () t r im(); tryint n =ln teger .parse In t (st r);if(isHaveDa iIyRecord (n)=true) JLabe I mess二new JLabe I (有”); mess. setFont (newFont CTimesRoman, Font. PLAIN, 11);mess setF

23、oreground(Color bIue); showDayi add(mess);1catch(Exception exp) )caIendarPad repa i nt 0;ca I endarPad va I i date ();pub Iic boo Iean i sHaveDa iIyRecord (i nt n) Str i ng key=H ,+year+M ,+month+ +n;Str ing dayFile=dir. Iist0; boolean boo=faIse;for (int k=0;kdayFile. Iength;k+)if (dayFilek. equaIs(

24、key+. txt) boo二true;break;1) return boo;) pub Iic Str ing getPicture address () String address = null;try 1InputStreamoutOne=newFilelnputStream (picture address.txt);Ob jec 11 npu tSt ream out Two = new Ob jec 11 npu tSt ream (ou tO ne); try address = (String) outTwo. readOb jec t (); catch (Excepti

25、on ex) outTwo. closeO ; catch (lOExcept ion eee) if (address 匸 nulI) return address; eIse return picturejpg;)pub Iic void actionPerformedl (ActionEvent e) if (e. getAct ionCommand (). equa I s (更改圖片背景)Fi I eDialog d i a = new Fi leDialog(this,選擇圖片 H, Fi leDialog. LOAD);d i a setModal (true);d i a. s

26、etV i s i b I e (true);i f (di a. getDi rectory ()!= null) & (di a. getFi le() !=null) FiIeOutputStream inOne = new FileOutputStream( pictureaddress txt”);Ob jectOutputStreaminTwo=newObjectOutputStream(i nOne);i nTwo. wr i teOb ject (d i a. getD i rectory ()+d i a. getFi le();inTwo. close。;)catch (l

27、OException ee) )String picturename = getPicture address (); caIendarImage setImageFiIe (new FiIe (p icturename);pub Iic static void ma i n (Stri ng args) new Ca I endarWi ndow();)CaIendarPad 類import javax.swing*;import javaawt *;import java awt. event *;import java util.*;import javax swing JPaneI;p

28、ub Iic cI ass CaIendarPad extends JPaneI int year,month, day;CalendarMessage calendarMessage;JTextField showDay;JLabeI title;String星期=SUN 日 ”,MON 一,TUE 二,WED 三, THU 四,”FRI 四u, SAT 六;JPaneI north,center;pub I ic Ca IendarPad () setLayout(new BorderLayoutO);nor th 二 new JPane I ();nor th. set Lay out

29、(new Gr i dLay out (1,7);center二new JPane I ();center. setLayout(new Gr i dLayout(6, 7);add (center ,BorderLayout. CENTER );add (north,BorderLayout. NORTH );title二new JLabeI7;for (int j=0;j7;j+)titlej=new JLabeI ();t i 11ej. setFont (new Font (TimesRomanH,Font. BOLD ,12);tit lej. setText (星期j);t itl

30、ej. setHor izontaIAIignment (JLabeI. CENTER);t i 11ej setBorder (BorderFactory. createRa i sedBeveI Border (); north, add (t it lej);t i 11e0 setForeground(Co I or. red);ti11e6 setForeground(Color bIue);)pub Iic void setShowDayTextFieId (JTextFieId text) showDay二text;for (int i二0;ishowDay. I ength;i

31、+) showDay i . setFont (new Font (HT imesRoman11, Font. BOLD , 15); showDayi. setHor izontaIAIignment (JTextFieId. CENTER); showDayi setEditable (fa Ise);center, add (showDay i);)pub IicvoidsetCaIendarMessage(CaIendarMessagecaIendarMessage) this.caIendarMessage=caIendarMessage;1pub Iic void showMont

32、hCaIendar()Str i ng a二caIendarMessage getMonthCaIendar ();for (i nt i 二0;i 42;i+)showDayi. setText(ai);va I idate ();)CaIendarMesssage 類import java util.CaIendar;pub Iic cI ass CaIendarMessage int year=-1,month二一1,day二一1;pub I ic int getYear () return year;)pub Iic void setMonth (int month)if (month

33、=1)th i s. month二month;e I sethis month =1;pub I i c i nt getMonth () return month;pub Iic void setDay (int day) this day 二day;1pub I ic int get Day () return day ;pub I ic Str ing getMonthCa I endar () Str ing day二new Str ing42;Calendar r iIi 二Calendar .get Instance ();r i I i. set (year, morith-1,

34、 1);int 星期幾=r i I i. get (Calendar. DAYOF.WEEK )-1;int dayAmount二0;i f (month=11|month=3|month=5|month=7|month=8|month=10|month=12)dayAmount=31;if (month=4| |month6| |month9| |month=11)dayAmount=30;if (month=2)i f(year%4=0)&(year%100!=0)|year%400=0)dayAmount=29;dayAmount=28;for (int i =0; i星期幾;i+) d

35、ayi=for (int i二星期幾,n=1; i星期幾+dayAmount; i+) day i =Str ing. va IueOf (n);n+;)for (int i二星期幾+dayAmount; i42; i +) day i二return day;pub Iic void setYear (i nt year) this year = year;NotePad 類import java awt *;import javax swing *;import javax swing border Border;import javax swing event ListSelectionE

36、vent;import javax swing event ListSelectionListener;import java io. *;import java awt. event *;pub Ii c cI ass NotePad extends JPaneI imp I ements ActionListener JTextArea text;JTextField showMessage;JPopupMenu menu;JMenuItem itemCopy, itemCut, itemPaste, itemClear, btn;pub I ic NotePad () showMessa

37、ge = new JTextFieldO;showMessage setHor izontaIAIignment(JTextFieId CENTER); showMessage setFont(new Font(HT imesRoman11, Font. BOLD, 16); showMessage setForeground (Co I or bIue);showMessage setBackground(Co I or p i nk);showMessage setBorder (BorderFactory. createRa i sedBeveI Border (); showMessa

38、ge setEd i tab Ie(fa Ise);menu = new JPopupMenu();i temCopy = new JMenul tem(H 復制”); itemCut = new JMenultem(M剪切”); itemPaste = new JMenultemC粘貼”); itemClear = new JMenultem(清空”);btn = new JMenultem(H字體”);itemCopy. addAct i onL i stener(th i s);itemCut addAct i onL i stener(th i s);itemPaste. addAct

39、 ionLi stener(thi s);itemCI ear addAct i onL i stener(th i s);btn.addAct i onLi stener(thi s);menu.add (itemCopy);menu.add (itemCut);menu.add (itemPaste);menu.add (itemCI ear);menu. add (btn);text = new JTextArea (10, 10);text.addMouseL i stener (new MouseAdapter () pub Iic void mousePressed (MouseE

40、vent e) i f (e. getMod i f i ers () = I nputEvent. BUTT0N3_MASK)menu. show (text, e. getX (), e. getY (););setLayout(new BorderLayout();add (showMessage, BorderLayout. NORTH);add (new JScroI I Pane (text), BorderLayout. CENTER);)pub Iic void setShowMessage (int year, int month, int day) showMessage.

41、setTex+ year + 年+ month + 月 ” + day + 日);showMessage setForeground (Co I or bIue);showMessage. setFont (new Font (宋體,F(xiàn)ont. BOLD, 15);pub Iic void save (File d i r, int year, int month, int day) St r i ng dai lyCon tent 二 text, get Tex t();St r i ng f i I eName =+ year + + mon th + + day +Str ing key

42、 = Hn + year + + month + + day;Str i ng dayF i I e = dir. I i st ();boo Iean boo = fa Ise;for (int k = 0; k dayFiIe. Iength; k+) if (dayFilek. startsWith (key) boo = true; break;)if (boo) Str ing m = + year + ”年+ month + 月+ day+ 已有日志,將新的容添加到日志嗎? ”;int ok 二 JOptionPaneshowConfirmDia Iog (this, m, H H

43、,JOptionPane.YES_N0_0PTI ON,JOptionPane. QUESTION_MESSAGE);if (ok = JOptionPane. YES OPTION) try File f = new F iIe (d i r, f iI eName);RandomAccessFile out = new RandomAccessFi Ie(f, rw); long f i I eEnd = out. lengt h ();byte bb = da iIyContent.getBytes 0; out.seek (fileEnd);out. wr ite (bb);out.

44、closeO ; catch (lOException exp) ) eIse String m = ” + year + 年+ month + 月+ day + 還沒有 日志,保存日志嗎? “;int ok 二 JOpt ionPane. showConf i rmDi a I og (th i s, m,詢問, JOptionPane. YES NO OPTION,JOptionPane. QUESTION.MESSAGE);if (ok = JOptionPane.YES_OPTI ON) try File f 二 new F iIe (d i r, fiI eName);RandomA

45、ccessFile out = new RandomAccessFi Ie (f, rw); long f i leEnd = out length ();byte bb = da i I yContent. getBytes ();out. seek (fileEnd);out. wr i te (bb);out. close (); catch (lOException exp) )1)pub Iic void delete(File d i r, int year, int month, int day) Str ing key =+ year + + month + HH + day;

46、Str i ng dayFi le = dir. I i st ();boo Iean boo = fa Ise;for (int k = 0; k dayFile. length; k+) if (dayFilek.startsWith (key) boo = true; break;1i f (boo) String m =刪除+ year + 年+ month + 月+ day + ”日 的日志嗎? u;i nt ok 二 JOpt ionPane. showConf i rmDi a I og (th i s, m,詢問,JOpti onPane. YESNO OPT I ON, JO

47、ptionPane. QUESTION_MESSAGE);if (ok = JOptionPane. YES_OPTI ON) Str ing f i I eName = H + year + N + mon th + ” + day + H. txt; File de IeteFile = new Fi le (di r, fiI eName); de I e teFile de I e te();)e I se String m = ” + year + + month + “ + day + ;JOpt ionPane. showMessageDia Iog (th s, m,提示,JO

48、ptionPane. WARN ING_MESSAGE);)pub Iic void read (File d i r, int year, int month, int day) Str i ngfiI eName=+ year + H + month+ +day +Str ingkey =+year + + month + HH+ day;Str i ng dayFi le = dir. I i st ();boo Iean boo = fa Ise;for (int k = 0; k dayFile. length; k+) if (dayFilek.startsWith (key) b

49、oo = true; break;)1i f (boo) String m = + year + + month + + day + H;i nt ok 二 JOpt i onPane. showConf i rmD i a I og (th i s, m,詢問,JOpt i onPane. YES_N00PT I ON, JOptionPane. QUESTIONMESSAGE);if (ok = JOptionPane.YES OPTION) text.setText (nulI);try File f 二 new F iIe (d i r, f iI eName);FiI eReader

50、 inOne = new FiI eReader (f);BufferedReader inTwo = new BufferedReader (inOne); Str ing s = null;while (s = inTwo. readLine () != null) text.append (s + Hn);inOne. close ();inTwo. close (); catch (lOException exp) String m = + year + M + month + M + day + JOpt ionPane. showMessageDi a I og (th i s,

51、m,提示, JOpti onPane. WARN ING_MESSAGE);)pub Iic void actionPerformed (ActionEvent e) i f (e. get Source () = i t emCopy)text, copy ();itemCut)itemPaste)e I se i f (e. get Source () = text, cut ();e I se i f (e. get Source () = text, paste ();e I se i f (e. get Source () = ite mC I ear) text. setText

52、(nulI);if (e. get Source () = btn) JFontDialog nFD = new JFontDialog(M選擇字體); nFD. setModa I (true);nFD. setVisible (true);text. setFont (nFD. myFont);cI ass JFontDialog extends JDialog pr i va te static final long ser i a I Vers i onll ID = 1L;JLi st fontpoIics, fontstyIe, fonts i ze;JTextFieId font

53、poIict, fontstylet, fontsizet;String example;JLabeI FontResoI vent;JButton buttonok, buttoncanceI;Font myFont;pub Iic JFontD i a Iog (Str i ng title) Container container = getContentPane(); conta i ner setLayout(new BorderLayout ();JPane I pane I = new JPane I ();pane I .setLayout(new Gr idLayout(2,

54、 1);JPaneI FontSet, FontView;FontSet = new JPaneI (new Gr i dLayout (1, 4);FontView = new JPaneI (new Gr i dLayout (1, 2); examp Ie = AaBbCcDdEe;FontResoI vent = new JLabeI (examp Ie, JLabeI.CENTER);FontResoI vent. setBackground(Co I or. WHITE);L i stSeIect i onL i stenerseIect i onL i stener=newL i stSe I ect i onL i stener () pub Iic void vaIueChanged(ListSelectionEvent e) if

溫馨提示

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

最新文檔

評論

0/150

提交評論