万年历Java课程设计_第1页
万年历Java课程设计_第2页
万年历Java课程设计_第3页
万年历Java课程设计_第4页
万年历Java课程设计_第5页
已阅读5页,还剩18页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、课程设计说明书 NO.1万年历程序设计1、课程设计的目的这学期我们学习了Java语言,Java语言是面向对象的开发方法是当今世界最流行的开发方法,是当今流行的网络编程语言。它具有面向对象、跨平台、分布应用等特点。它不仅具有更贴近自然的语义,而且有利于软件的维护和继承。很多程序开发人员得第一选择。为了进一步巩固课堂上所学到的知识,深刻把握Java语言的重要概念及其面向对象的特性,锻炼学生熟练的应用面向对象的思想和设计方法解决实际问题的能力,开设了Java程序设计课程设计。让学生自己动手动脑,将书本上得知识运用到实践当中去,使知识能更好得融会贯通。为了达到这个目的,我们需要自己动手做一个Java的

2、课程设计,在这个课程设计里我们需要根据我们手中现有得软件与知识基础,独立得将它完成,只有这样,我们才能把理论变成实践,将知识真正得做到学以致用。这次课程设计我主要研究了利用已学的Java知识编辑一个电子万年历。通过此次课程设计,来巩固所学Java语言基本知识,增进Java语言编辑基本功,掌握JDK、JCreator等开发工具的运用,拓宽常用类库的应用。并通过自己动手,自主得学习,对Java这门课程有更深得了解与认识。2、设计方案论证2.1课程设计的基本要求万年历有很多作用,顾名思义它是一种很方便得计时计年工具,随着科技得发展,我们已经可以通过计算机制作出精美的万年历,通过计算机制作得万年历不但

3、外观美观,而且功能强大。通过万年历,我们可以实现很多功能,随意看某年得年份,某年得月份,随意调整某年得某天,并将其显示出来,而且有判断闰年闰月得功能,十分方便。除此之外,万年历还具有记事本功能,通过记事本功能可以备忘一些事情,备忘得事情可以随意添加在万年历得任意年月日里,如果记录得内容已经不再需要,还可以任意的将记录得内容进行删除,在删除得时候还会提示,是否确定删除,如果是,则程序会将它删除,如果不是,则系统不会将它删除。2.2方案论证 沈 阳 大 学课程设计说明书 NO.2方法表1 方法表方法名功能备注(须输入的参数)calendar初始化各种数据int year,int month,int

4、 daySwitchMonth选择月份int monthPrintMonth显示月份int year,int month,int dayPrintMonthBody月份体int startday,int dayinmonthIsLeapYearGetStartDayGetTotalNumOfDaysGetNumOfDaysInMonthWriteRecordReadRecordDeleteFileAboutActionListenerWayactionPerformed(ActionEvent)mousePressedmouseClickedmouseReleasedmouseEnteredm

5、ouseExitedSaveLogMain判断是否为闰年得到开始的天得到总天数得到当月的天数写日记看日记删除日记监听路径时间表现鼠标点击鼠标点击生点击释放鼠标进入鼠标推出保存日期主函数int yearint year,int monthint year,int monthint year,int month唯一的主函数 沈 阳 大 学课程设计说明书 NO.3开始初始化读、写日期、时间和温度分离日期时间温度显示值显示子程序农历自动更新子程序日期、时间修改子程序闰月子程返回定时闹铃子程序程序流程框图 图1 主程序流程图图2计算阳历程序流程图 沈 阳 大 学课程设计说明书 NO.4图3时间调整程序流

6、程图 沈 阳 大 学课程设计说明书 NO.52.3程序说明成员变量见表2变量表表2变量表成员变量描述变量类型名称年、月、日intYear、month、day下一年,下一月IntYearafterquery,monthafterquey开始的时间Intstartday选择月,天StringSwitchMonth,key,day改变年改变月前一月,前一年判断是否改变左、又面板年、月左上角信息标签显示月、年标签空格标签右上部时间标签星期标签数组月份下拉列表时间确定按钮保存、删除按钮前、下一月按钮文本区星期字符串数组显示天的文本区输入年的文本区IntIntIntbooleanJPanelJLabelJ

7、LabelJLabelJLabelJLabelJLabelJComboBoxJButtonJButtonJButtonJTextAreaStringJTextFieldJTextFieldChangeyearmessagechangemonthmessagepriormonth,prioryearischange,ischange_priornextLeftPane,RightPaneYearLabel,MonthLabelAskShowDateBlankNorthMonthDayYearTopBarTitleMonthComboboxQuerySouthSave,SouthDeletePri

8、orMonth,NextMonthCenterTextweekShowDaysYearText 沈 阳 大 学课程设计说明书 NO.624程序源代码import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; import java.util.Calendar; import java.util.*; import java.io.*; import java.io.IOException; public cla

9、ss calendar extends JFrame implements ActionListener,MouseListener int year,month,day; int yearafterquery,monthafterquery; int startday; String SwitchMonth; String key; int changeyearmessage; int changemonthmessage; int priormonth; int prioryear; boolean ischange=false; boolean ischange_priornext=fa

10、lse; private JPanel LeftPane,RightPane; /Left sub private JLabel YearLabel; private JLabel MonthLabel; private JComboBox MonthCombobox; private JTextField ShowDays= new JTextField42; private JTextField YearText;private JLabel Ask; private JLabel ShowDate; private JLabel Blank; private JLabel TopBarT

11、itle=new JLabel7; private JButton ToToday; private JButton Query; private String week="SUN","MON","TUE","WED","THU","FRI","SAT"/right subprivate JLabel NorthMonthDayYear; 沈 阳 大 学课程设计说明书 NO.7private JTextArea CenterText; private JB

12、utton SouthSave,SouthDelete; private JButton PriorMonth; private JButton NextMonth; public calendar(int year,int month,int day) setTitle("My Calendar&Textbook"); /the layout about left of jpane LeftPane = new JPanel(); JPanel LeftCenter = new JPanel(); JPanel LeftNorth = new JPanel();

13、/JPanel LeftSouth = new JPanel(); LeftPane.setLayout(new BorderLayout(); LeftPane.add(LeftNorth,BorderLayout.NORTH); LeftPane.add(LeftCenter,BorderLayout.CENTER); LeftPane.add(ToToday = new JButton("Go to today",new ImageIcon("./images/Handle.gif"),BorderLayout.SOUTH); ToToday.se

14、tBackground(Color.cyan); ToToday.addActionListener(this); LeftPane.validate(); /the layout of LeftPane /LeftPane_NorthLeftNorth.setLayout(new GridLayout(3,1,0,-2); LeftNorth.add(Ask = new JLabel(" Plese input the informations which you want query:"); JPanel North = new JPanel(new FlowLayou

15、t(0,8,0); LeftNorth.add(North); North.add(YearLabel=new JLabel("Year:"); North.add(YearText = new JTextField(4); YearText.setBackground(Color.getHSBColor(30,20,50);YearText.setForeground(Color.blue); YearText.setFont(new Font("TimesRoman",Font.BOLD,17); YearText.addActionListener

16、(this); YearText.setFocusable(true);North.add(Blank=new JLabel(" "); North.add(MonthLabel = new JLabel("Month:"); North.add(MonthCombobox = new JComboBox(); /add month to monthcombobox for(int i=1;i<=12;i+) MonthCombobox.addItem(new Integer(i); 沈 阳 大 学课程设计说明书 NO.8/Switch the m

17、onth MonthCombobox.setForeground(Color.blue); MonthCombobox.setFont(new Font("TimesRoman",Font.BOLD,12); North.add(Blank=new JLabel(" "); North.add(Query=new JButton("Query"); Query.setForeground(Color.blue); Query.addActionListener(this); JPanel North2=new JPanel(new F

18、lowLayout(); LeftNorth.add(North2); North2.add(PriorMonth=new JButton(new ImageIcon("./images/prior.gif"); PriorMonth.addActionListener(this); PriorMonth.setActionCommand("prior"); priormonth=month;prioryear=year; SwitchMonth(month); North2.add(ShowDate = new JLabel(SwitchMonth+&

19、quot; "+","+" "+String.valueOf(year),SwingConstants.CENTER); ShowDate.setForeground(Color.blue); ShowDate.setFont(new Font("TimesRoman",Font.BOLD,14); North2.add(NextMonth=new JButton(new ImageIcon("./images/next.gif"); NextMonth.addActionListener(this);

20、NextMonth.setActionCommand("next"); /LeftPane_Center LeftCenter.setLayout(new GridLayout(7,7); /print title for(int i=0;i<7;i+) TopBarTitlei=new JLabel(); TopBarTitlei.setText(weeki); TopBarTitlei.setForeground(Color.darkGray); TopBarTitlei.setHorizontalAlignment(0); TopBarTitlei.setBac

21、kground(Color.MAGENTA ); TopBarTitlei.setBorder(BorderFactory.createRaisedBevelBorder();LeftCenter.add(TopBarTitlei); /print screen and add listener for(int i=0;i<42;i+) ShowDaysi=new JTextField(); ShowDaysi.addMouseListener(this); ShowDaysi.setEditable(false); 沈 阳 大 学课程设计说明书 NO.9LeftCenter.add(S

22、howDaysi); /print the body of month PrintMonth(year,month,day); /the layout about right of jpane RightPane = new JPanel(new BorderLayout(); JPanel RightCenter = new JPanel(); JPanel RightNorth = new JPanel();JPanel RightSouth = new JPanel(new FlowLayout();RightPane.add(RightNorth,BorderLayout.NORTH)

23、;RightPane.add(RightCenter,BorderLayout.CENTER);RightPane.add(RightSouth,BorderLayout.SOUTH);RightNorth.add(NorthMonthDayYear=new JLabel(">>"+year+","+SwitchMonth+","+day+"<<");key=year+"_"+SwitchMonth+"_"+day;NorthMonthDayYear.

24、setForeground(Color.blue); NorthMonthDayYear.setFont(new Font("TimesRoman",Font.BOLD,17);RightCenter.add(CenterText=new JTextArea("please write today's things.");CenterText.setLineWrap(true);CenterText.setSelectedTextColor(Color.blue);/CenterText.addActionListener(this);Right

25、South.add(SouthSave=new JButton(" Save ");SouthSave.setBackground(Color.cyan);SouthSave.addActionListener(this);SouthSave.setActionCommand("Save");RightSouth.add(SouthDelete=new JButton(" Delete ");SouthDelete.setBackground(Color.cyan);SouthDelete.addActionListener(this

26、);SouthDelete.setActionCommand("Delete");this.year = year;this.month = month;this.day = day;/add container to put LeftPane and RightPane Container con=getContentPane(); JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,RightPane,LeftPane);/gai bian con.add(split,BorderLayout.CENT

27、ER); con.validate();/add CenterPane to notepad/CenterPane initializesetFont(new Font("Times New Roman",Font.PLAIN,12); 沈 阳 大 学课程设计说明书 NO.10JScrollPane scrollpane = new JScrollPane(CenterText);scrollpane.setPreferredSize(new Dimension(220,250); RightCenter.add(scrollpane); /init randomacces

28、sfile /switch the month in english public void SwitchMonth(int month) switch(month) case 1: SwitchMonth="Jan"break; case 2: SwitchMonth="Feb"break; case 3: SwitchMonth="Mar"break; case 4: SwitchMonth="Apr"break; case 5: SwitchMonth="May"break; case 6

29、: SwitchMonth="Jun"break; case 7: SwitchMonth="Jul"break; case 8: SwitchMonth="Aug"break; case 9: SwitchMonth="Sep"break;case 10: SwitchMonth="Qct"break; case 11: SwitchMonth="Nov"break; case 12: SwitchMonth="Dec"break; /print the

30、 body of the month public void PrintMonth(int year,int month,int day) /Get start day of the week for the first date in the month int startday = GetStartDay(year,month); 沈 阳 大 学课程设计说明书 NO.11/Get number of days in the month int dayinmonth = GetNumOfDaysInMonth(year,month); /Print header /PrintTitleAnd

31、Screen(); /Print body PrintMonthBody(startday,dayinmonth,day); /PrintMonth(int year,int month,int day)'s burden public void PrintMonth(int year,int month) /Get start day of the week for the first date in the month int startday = GetStartDay(year,month); /Get number of days in the month int dayin

32、month = GetNumOfDaysInMonth(year,month); /Print header /Print body PrintMonthBody(startday,dayinmonth); /PrintMonthBody(int startday,int dayinmonth,int day)'s burden public void PrintMonthBody(int startday,int dayinmonth) for(int i=startday,n=1;i<startday+dayinmonth;i+) ShowDaysi.setText(&quo

33、t;"+n); ShowDaysi.setHorizontalAlignment(0);/let center if(n=day) ShowDaysi.setForeground(Color.green); ShowDaysi.setFont(new Font("TimesRoman",Font.BOLD,20); ShowDaysi.setBackground(Color.DARK_GRAY); else ShowDaysi.setFont(new Font("TimesRoman",Font.BOLD,12); ShowDaysi.setF

34、oreground(Color.white); ShowDaysi.setBackground(Color.DARK_GRAY); n+; for(int i=0;i<startday;i+) 沈 阳 大 学课程设计说明书 NO.12ShowDaysi.setText("");ShowDaysi.setBackground(Color.DARK_GRAY);for(int i=startday+dayinmonth;i<42;i+)ShowDaysi.setText("");ShowDaysi.setBackground(Color.DARK

35、_GRAY);/judge leapyear is or notpublic boolean IsLeapYear(int year)if(year%400=0)|(year%4=0&&year%100!=0)return true;elsereturn false; /judge the start day of a monthpublic int GetStartDay(int year,int month)/get total number of day since1/1/0000int startday0001=-32768;long totalnumofdays=Ge

36、tTotalNumOfDays(year,month);/return the start dayreturn (int)(totalnumofdays+startday0001)%7);/judge the days of a yearpublic long GetTotalNumOfDays(int year,int month)long total=0;/get the total days from -32767 to yearfor(int i=-32767;i<year;i+)if(IsLeapYear(i)total=total+366;elsetotal=total+36

37、5;/Add days from jan to the month prior to the calendar monthfor(int i=1;i<month;i+)total=total+GetNumOfDaysInMonth(year,i); 沈 阳 大 学课程设计说明书 NO.13return total; /judge the days of a month public int GetNumOfDaysInMonth(int year,int month) if(month=1 | month=3 | month=5 | month=7 | month=8 | month=1

38、0 |month=12) return 31; if(month=4 | month=6 | month=9 | month=11) return 30; if(month=2) if(IsLeapYear(year) return 29; else return 28; return 0; public void WriteRecord() String content; content=CenterText.getText(); int n=content.length(); char contentarr=new charn; try int i=0; for(i=0;i<n;i+

39、) contentarri=content.charAt(i); File Diary = new File("Diary"); Diary.mkdir(); File myfile=new File("Diary"+key+".txt"); FileWriter Record=new FileWriter(myfile);for(i=0;i<contentarr.length;i+) Record.write(contentarri); Record.close(); JOptionPane.showMessageDialog

40、(this,"Save success!"); 沈 阳 大 学课程设计说明书 NO.14catch(IOException ex) public void ReadRecord() try String content="" File myfile=new File("Diary"+key+".txt"); FileReader Record=new FileReader(myfile); if(myfile.exists() long b=myfile.length(); /char contentarr=new

41、 charb; int n=JOptionPane.showConfirmDialog(this,"Today has logs,are you read?","Confirm",JOptionPane.YES_NO_CANCEL_OPTION); if(n=JOptionPane.YES_OPTION) while(b=Record.read()!=-1) content=content+(char)b; CenterText.setText(content); Record.close(); catch(IOException ex)CenterTe

42、xt.setText("Today has not logs."); public void DeleteFile() String filepath="Diary"+key+".txt" File myfile=new File(filepath); int n=JOptionPane.showConfirmDialog(this,"Are you sure delete the file?","Confirm",JOptionPane.YES_NO_CANCEL_OPTION); if(n=

43、JOptionPane.YES_OPTION) 沈 阳 大 学课程设计说明书 NO.15 if(myfile.exists() Runtime rt = Runtime.getRuntime();try rt.exec("cmd /c del "+filepath); catch (IOException e) e.printStackTrace();JOptionPane.showMessageDialog(this,"Delete successed!"); CenterText.setText("Today has not logs.&q

44、uot;); else JOptionPane.showMessageDialog(this,"The file doesn't exist,delete failured!"); public void AboutActionListenerWay() try prioryear=Integer.parseInt(YearText.getText(); priormonth=MonthCombobox.getSelectedIndex()+1; String StrYearText=YearText.getText(); changeyearmessage=Int

45、eger.parseInt(StrYearText); changemonthmessage=MonthCombobox.getSelectedIndex()+1; monthafterquery=changemonthmessage; yearafterquery=changeyearmessage; SwitchMonth(changemonthmessage);ShowDate.setText(SwitchMonth+" "+","+" "+String.valueOf(changeyearmessage); PrintMont

46、h(changeyearmessage,changemonthmessage); ischange=true; catch(Exception ee) JOptionPane.showMessageDialog(this,"The input format doesn't match","Error",JOptionPane.ERROR_MESSAGE); 沈 阳 大 学课程设计说明书 NO.16 /do actonlistener things public void actionPerformed(ActionEvent eAction) S

47、tring ActionCommand=eAction.getActionCommand(); /Handle button events if(eAction.getSource() instanceof JButton) /Handle the query if("Query".equals(ActionCommand) try AboutActionListenerWay(); catch(Exception ee) JOptionPane.showMessageDialog(this,"The input format doesn't match&

48、quot;,"Error",JOptionPane.ERROR_MESSAGE); /Handle prior month if("prior".equals(ActionCommand) if(priormonth>1) priormonth=priormonth-1; else priormonth=12; prioryear=prioryear-1; PrintMonth(prioryear,priormonth,day); SwitchMonth(priormonth); ShowDate.setText(SwitchMonth+"

49、; , "+prioryear); NorthMonthDayYear.setText(">>"+SwitchMonth+","+day+","+prioryear+"<<"); key=prioryear+"_"+SwitchMonth+"_"+day; ischange_priornext=true; 沈 阳 大 学课程设计说明书 NO.17/Handle next month if("next".equals(Act

50、ionCommand) if(priormonth<12) priormonth=priormonth+1; else priormonth=1; prioryear=prioryear+1; PrintMonth(prioryear,priormonth,day); SwitchMonth(priormonth); ShowDate.setText(SwitchMonth+" , "+prioryear); NorthMonthDayYear.setText(">>"+SwitchMonth+","+day+&qu

51、ot;,"+prioryear+"<<"); key=prioryear+"_"+SwitchMonth+"_"+day; ischange_priornext=true; /Handle the "Go to today" if("Go to today".equals(ActionCommand) PrintMonth(year,month,day); YearText.setText(""); MonthCombobox.setSelectedIn

52、dex(0); SwitchMonth(month); ShowDate.setText(SwitchMonth+" "+","+" "+String.valueOf(year); NorthMonthDayYear.setText(">>"+year+SwitchMonth+","+day+","+"<<"); key=year+"_"+SwitchMonth+"_"+day;priormo

53、nth=month; prioryear=year; ischange=false; /Handle the "Save"if("Save".equals(ActionCommand) WriteRecord(); if("Delete".equals(ActionCommand) DeleteFile(); 沈 阳 大 学课程设计说明书 NO.18 /Handle JTextField events if(eAction.getSource() instanceof JTextField) /Handle the query AboutActionListenerWay(); public void mousePressed(MouseEvent eMouse) int day; try /Handle ShowDays events if(ischange=false) JTextField source=(JTextField)eMouse.getSource();day=Integer.pars

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论