版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Java日历记事本课程设计报告在设计日历记事本时,需要编写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 (宋体,Font. 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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 二零二五年度彩钢棚钢结构材料质量检测合同3篇
- 2025年度年度财务报表编制保密合作协议3篇
- 年度非开挖导向钻机铺管机市场分析及竞争策略分析报告
- 二零二五年恒大地产商业地产租赁及产权变更代理合同样本3篇
- 2025厂长任期企业可持续发展战略合同3篇
- 二零二五版创业园区租赁合同参考模板(含孵化服务)3篇
- 2025年度瓷砖批发市场入驻经营合同4篇
- 2025年蔬菜运输合同含蔬菜品牌推广效果评估条款2篇
- 2025年度豪华品牌4S店新车销售与服务保障合同3篇
- 2025年LED广告屏租赁与品牌推广服务合同模板3篇
- GB/T 37238-2018篡改(污损)文件鉴定技术规范
- 普通高中地理课程标准简介(湘教版)
- 河道治理工程监理通知单、回复单范本
- 超分子化学简介课件
- 高二下学期英语阅读提升练习(一)
- 易制爆化学品合法用途说明
- 【PPT】压力性损伤预防敷料选择和剪裁技巧
- 大气喜庆迎新元旦晚会PPT背景
- DB13(J)∕T 242-2019 钢丝网架复合保温板应用技术规程
- 心电图中的pan-tompkins算法介绍
- 羊绒性能对织物起球的影响
评论
0/150
提交评论