1、课程设计阐明书 NO.1多功能电子时钟得设计1、课程设计目Java语言是面向对象开发办法是当今世界最流行开发办法,是当今流行网络编程语言。它具备面向对象、跨平台、分布应用等特点。它不但具备更贴近自然语义,并且有助于软件维护和继承。诸多程序开发人员得第一选取。为了将课堂上得知识掌握得更加透彻,深刻把握Java语言重要概念及其面向对象特性,锻炼学生纯熟应用面向对象思想和设计办法解决实际问题能力,开设了Java程序设计课程设计。让学生自己动手动脑,将课本上得知识运用到实践当中去,使知识能更好得融会贯通。学习任何知识得目都是要将它运用到实践中去,因此咱们要运用已有知识,独立得将这个课程设计完毕,只有这
2、样,咱们才干将知识变成本领,变成属于自己得东西,通过一种学习得学习,咱们已有可一定得Java基本,当前咱们就要运用这些基本,来完毕课程设计。这次课程设计我重要研究了运用已学Java知识编辑一种多功能电子时钟。通过本次课程设计,来巩固所学Java语言基本知识,增进Java语言编辑基本功,掌握JDK、JCreator等开发工具运用,拓宽惯用类库应用。并通过自己动手,自主得学习,对Java这门课程有更深得理解与结识。2、设计方案论证2.1课程设计基本规定多功能电子时钟是一种多功能得计时工具,它得功能与钟表不同,而是可以将年,月,日通过java程序得到实现,并具备更多功能,更简洁外观,更以便用法,更大
3、实用价值。通过多功能电子时钟,咱们可以实现诸多功能,随意看某年得年份,某年得月份,随意调节某年得某天,并将其显示出来,并且有判断闰年闰月得功能,十分以便万年历有诸多作用,顾名思义它是一种很以便得计时计年工具,随着科技得发展,咱们已经可以通过计算机制作出精美万年历,通过计算机制作得万年历不但外观美观,并且功能强大。除此之外,万年历还具备记事本功能,通过记事本功能可以备忘某些事情,备忘得事情可以随意添加在万年历得任意年月日里,如果记录得内容已经不再需要,还可以任意将记录得内容进行删除,在删除得时候还会提示。2.2方案论证 沈 阳 大 学课程设计阐明书 NO.22.2.1办法表1 办法表办法名功能备
4、注(须输入参数)calendar初始化各种数据int year,int month,int daySwitchMonth选取月份int monthPrintMonth显示月份int year,int month,int dayPrintMonthBody月份体int startday,int dayinmonthIsLeapYearGetStartDayGetTotalNumOfDaysGetNumOfDaysInMonthWriteRecordReadRecordDeleteFileAboutActionListenerWayactionPerformed(ActionEvent)mouse
5、PressedmouseClickedmouseReleasedmouseEnteredmouseExitedSaveLogMain判断与否为闰年得到开始天得到总天数得到当月天数写日记看日记删除日记监听途径时间体现鼠标点击鼠标点击生点击释放鼠标进入鼠标推出保存日期主函数int yearint year,int monthint year,int monthint year,int month唯一主函数 沈 阳 大 学课程设计阐明书 NO.3开始初始化读、写日期、时间和温度分离日期时间温度显示值显示子程序农历自动更新子程序日期、时间修改子程序闰月子程返回定期闹铃子程序2.2.2程序流程框图 图1
6、 主程序流程图图2计算阳历程序流程图 沈 阳 大 学课程设计阐明书 NO.4图3时间调节程序流程图 沈 阳 大 学课程设计阐明书 NO.52.3程序阐明2.3.1成员变量见表2变量表表2变量表成员变量描述变量类型名称年、月、日intYear、month、day下一年,下一月IntYearafterquery,monthafterquey开始时间Intstartday选取月,天StringSwitchMonth,key,day变化年变化月前一月,前一年判断与否变化左、又面板年、月左上角信息标签显示月、年标签空格标签右上部时间标签星期标签数组月份下拉列表时间拟定按钮保存、删除按钮前、下一月按钮文本
7、区星期字符串数组显示天文本区输入年文本区IntIntIntbooleanJPanelJLabelJLabelJLabelJLabelJLabelJLabelJComboBoxJButtonJButtonJButtonJTextAreaStringJTextFieldJTextFieldChangeyearmessagechangemonthmessagepriormonth,prioryearischange,ischange_priornextLeftPane,RightPaneYearLabel,MonthLabelAskShowDateBlankNorthMonthDayYearTopB
8、arTitleMonthComboboxQuerySouthSave,SouthDeletePriorMonth,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
9、java.io.*; import java.io.IOException; public class 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; bool
10、ean ischange=false; boolean ischange_priornext=false; 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 ShowD
11、ate; private JLabel Blank; private JLabel TopBarTitle=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 JButton SouthSave,SouthDelete; priva
12、te 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(); /JPanel LeftSouth = new JPanel(); LeftPane.setLayo
13、ut(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.setBackground(Color.cyan); ToToday.addActionListener(this); LeftPane.validat
14、e(); /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 FlowLayout(0,8,0); LeftNorth.add(North); North.add(YearLabel=new JLabel(Year:); North.add(YearT
15、ext = new JTextField(4); YearText.setBackground(Color.getHSBColor(30,20,50);YearText.setForeground(Color.blue); YearText.setFont(new Font(TimesRoman,Font.BOLD,17); YearText.addActionListener(this); YearText.setFocusable(true);North.add(Blank=new JLabel( ); North.add(MonthLabel = new JLabel(Month:);
16、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 month MonthCombobox.setForeground(Color.blue); MonthCombobox.setFont(new Font(TimesRoman,Font.BOLD,12); North.add(Blank=new JLabel( ); Nort
17、h.add(Query=new JButton(Query); Query.setForeground(Color.blue); Query.addActionListener(this); JPanel North2=new JPanel(new FlowLayout(); LeftNorth.add(North2); North2.add(PriorMonth=new JButton(new ImageIcon(./images/prior.gif); PriorMonth.addActionListener(this); PriorMonth.setActionCommand(prior
18、); priormonth=month;prioryear=year; SwitchMonth(month); North2.add(ShowDate = new JLabel(SwitchMonth+ +,+ +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
19、.gif); NextMonth.addActionListener(this); NextMonth.setActionCommand(next); /LeftPane_Center LeftCenter.setLayout(new GridLayout(7,7); /print title for(int i=0;i7;i+) TopBarTitlei=new JLabel(); TopBarTitlei.setText(weeki); TopBarTitlei.setForeground(Color.darkGray); TopBarTitlei.setHorizontalAlignme
20、nt(0); TopBarTitlei.setBackground(Color.MAGENTA ); TopBarTitlei.setBorder(BorderFactory.createRaisedBevelBorder();LeftCenter.add(TopBarTitlei); /print screen and add listener for(int i=0;i+year+,+SwitchMonth+,+day+);key=year+_+SwitchMonth+_+day;NorthMonthDayYear.setForeground(Color.blue); NorthMonth
21、DayYear.setFont(new Font(TimesRoman,Font.BOLD,17);RightCenter.add(CenterText=new JTextArea(please write todays things.);CenterText.setLineWrap(true);CenterText.setSelectedTextColor(Color.blue);/CenterText.addActionListener(this);RightSouth.add(SouthSave=new JButton( Save );SouthSave.setBackground(Co
22、lor.cyan);SouthSave.addActionListener(this);SouthSave.setActionCommand(Save);RightSouth.add(SouthDelete=new JButton( Delete );SouthDelete.setBackground(Color.cyan);SouthDelete.addActionListener(this);SouthDelete.setActionCommand(Delete);this.year = year;this.month = month;this.day = day;/add contain
23、er to put LeftPane and RightPane Container con=getContentPane(); JSplitPane split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,RightPane,LeftPane);/gai bian con.add(split,BorderLayout.CENTER); con.validate();/add CenterPane to notepad/CenterPane initializesetFont(new Font(Times New Roman,Font.PLAIN,12
24、); 沈 阳 大 学课程设计阐明书 NO.10JScrollPane scrollpane = new JScrollPane(CenterText);scrollpane.setPreferredSize(new Dimension(220,250); RightCenter.add(scrollpane); /init randomaccessfile /switch the month in english public void SwitchMonth(int month) switch(month) case 1: SwitchMonth=Jan;break; case 2: Swi
25、tchMonth=Feb;break; case 3: SwitchMonth=Mar;break; case 4: SwitchMonth=Apr;break; case 5: SwitchMonth=May;break; case 6: 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;
26、case 12: SwitchMonth=Dec;break; /print the 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
27、(year,month); /Print header /PrintTitleAndScreen(); /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
28、number of days in the month int dayinmonth = 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;istartday+dayinmont
29、h;i+) ShowDaysi.setText(+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.setForegroun
30、d(Color.white); ShowDaysi.setBackground(Color.DARK_GRAY); n+; for(int i=0;istartday;i+) 沈 阳 大 学课程设计阐明书 NO.12ShowDaysi.setText();ShowDaysi.setBackground(Color.DARK_GRAY);for(int i=startday+dayinmonth;i42;i+)ShowDaysi.setText();ShowDaysi.setBackground(Color.DARK_GRAY);/judge leapyear is or notpublic b
31、oolean 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=GetTotalNumOfDays(year,month);/return the start dayr
32、eturn (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;iyear;i+)if(IsLeapYear(i)total=total+366;elsetotal=total+365;/Add days from jan to the month prior to the calenda
33、r monthfor(int i=1;imonth;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=10 |month=12) return 31; if(month=4 | month=6 | month=9 | m
34、onth=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;in;i+) contentarri=content.charAt(i); File Diary = new File(Diary);
35、 Diary.mkdir(); File myfile=new File(Diary+key+.txt); FileWriter Record=new FileWriter(myfile);for(i=0;icontentarr.length;i+) Record.write(contentarri); Record.close(); JOptionPane.showMessageDialog(this,Save success!); 沈 阳 大 学课程设计阐明书 NO.14catch(IOException ex) public void ReadRecord() try String co
36、ntent=; File myfile=new File(Diary+key+.txt); FileReader Record=new FileReader(myfile); if(myfile.exists() long b=myfile.length(); /char contentarr=new charb; int n=JOptionPane.showConfirmDialog(this,Today has logs,are you read?,Confirm,JOptionPane.YES_NO_CANCEL_OPTION); if(n=JOptionPane.YES_OPTION)
37、 while(b=Record.read()!=-1) content=content+(char)b; CenterText.setText(content); Record.close(); catch(IOException ex)CenterText.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 su
38、re delete the file?,Confirm,JOptionPane.YES_NO_CANCEL_OPTION); if(n=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.); else JOptionPane.showMessageDialog(this,The file doesnt exist,delete failured!); public void AboutActionListenerWay() try prioryear=Integer.parseInt(YearText.getText();