java课程设计报告_第1页
java课程设计报告_第2页
java课程设计报告_第3页
java课程设计报告_第4页
java课程设计报告_第5页
免费预览已结束,剩余17页可下载查看

付费下载

下载本文档

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

文档简介

?Java程序设计?课程设计报告题目:万年历学院:数学与统计专业:直:二学生姓名:梦超然学号:指导教师:李志新2021年7月9日1需求分析需求分析本程序的要求为:(1)能够显示实现1800-2100年的日期,包括公历、农历、星期、二十四节气.(2)能够根据月份格式显示,或根据星期格式显示.(3)能够显示系统时间.(4)能够设置闹钟.(5)能够查询假设干国际大城市(分布在不同时区)的时间.功能设计本程序要构建的万年历程序,其功能有以下几个方面:一、设计程序运行界面,具有显示各种功能的面板模块,并对各功能按钮就行时间监听;二、程序需要显示1800年到2100年的日历,包括公历、农历、时间所对应的星期.要求系统能够从界面上的操作获取公历日期,计算其所对应的星期和相应的农历并显示到界面上;三、程序要求显示系统时间,并能设置闹铃.要求可以自己设置一个时间点,在这一时间点做出相应的响应;四、查询显示假设干国际城市不同时区的时间,这里只需要调用Java的系统里就可以解决.五、对时间的更新,日历显示要求关联系统时间,能在进入程序时显示系统时间对应的日历,并能返回当天日历;六、程序界面的美观设计和人性化结构,要求具有良好的人机交互,容易理解界面内容,易操作;七、就现在的水平来说,功能1、2、3、5是可以实现,功能4有待研究,可能遇到的问题是按钮对时间的监听处理可能难度较大,还有就是在公历转农历中可能比拟难获取相应的公历变化日期.2.概要设计程序设计思路1.总天数的算法:首先用if语句判断定义年到输入年之间每一年是否为闰年,是闰年,该年的总天数为366,否那么,为365o然后判断输入的年是否为定义年,假设是,令总天数S=1,否那么,用累加法计算出定义年到输入年之间的总天数,再把输入年的一月到要输出的月份之间的天数累加起来,假设该月是闰年中的月份并且该月还大于二月,再使总天数加加,既算出从定义年一月一日到输出年的该月一日的总天数..输出月份第一天为星期几的算法:使总天数除以7取余加2得几既为星期几,假设是7,那么为星期日..算出输出月份第一天为星期几的算法:算出输出月份第一天为星期几后,把该日期以前的位置用空格补上,并总该日起一次输出天数直到月底,该月中的天数加上该月一日为星期几的数字再除以7得0换行,即可完整的输出该月的日历..获取系统时间,计算当天农历日期并显示2.2程序运行界面接口,MouseListener{privateCalendarcld=Calendar.getInstance();//获取一个Calendar类的实例对象,Calendar类用于获取某个时间或日期privateString口astr={"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};

priprivateDefaultTableModeldtm=newDefaultTableModel(null,astr);//创立了一个表格模型privateJTabletable=newJTable(dtm);//JTable用来显示和编辑常规二维单元表privateJScrollPanesp=newJScrollPane(table);//JScrollPane类可提供轻量级组件的scrollable视图.JScrollPane治理视口、可选的垂直和水平滚动条以及可选的行和列标题视口.privateJButtonbLastYear=newJButton("上一年");//建立一个“上一年〞的按钮privateJButtonbNextYear=newJButton("下一年");privateJButtonbLastMonth=newJButton("上月");privateJButtonbNextMonth=newJButton("下月");//JPanel是Java图形用户界面(GUI)工具包swing中的面板容器类,包含在javax.swing包中,是一种轻量级容器,可以参加到JFrame窗体中.JPanel默认的布局治理器是FlowLayout,其自身可以嵌套组合,在不同子容器中可包含其他组件(component),如JButton、JTextArea、JTextField等privateJPanelp1=neprivateJPanelp1=newJPanel();//设立八个中间容器,装入布局限制日期的按钮模块privateJPanelp2=newJPanel(newGridLayout(3,2));〃网格布局privateJPanelp3=newJPanel(newBorderLayout());〃边界布局privateJPanelp4=newJPanel(newGridLayout(2,1));//创立//创立具有指定行数和列数的网格布局privateJPanelp5=newJPanel(newBorderLayout());//边//边界布局治理器,构造一个组件之间没有间距(默认间距为0像素)的新边框布局.prprivateJPanelp6=newJPanel(privateJPanelp7=newJPanel(newGridLayout(2,2));newGridLayout(2,1));privprivateJPanelp8=newJPanel(privateJComboBoxtimeBoxnewBorderLayout());=newJComboBox(TimeZone.getAvailableIDs());//对所有支持时区进行迭代,获取所有的id;JComboBox组件可以实现在组合框内编辑//J//JTextField(intcolumns),构造一个具有指定列数的新的空TextField.JTextField是一个轻量级组件,它允许编辑单行文本.privateJTextFieldjtfYear=newJTextField(5);//jtfYeaar年份显示输入框privateJTextFieldjtfMonth=newJTextField(2);//jtfMouth月份显示输入框privateJTextFieldtimeField=newJTextField();//各城市时间显示框privatestaticJTextAreajta=newJTextArea(10,5);//农历显示区privateJScrollPanejsp=newJScrollPane(jta);//JScrollPane(Componentview);建立建立一个新的JScrollPane对象,当组件内容大于显示区域时会自动产生滚动轴OprivateJLabell=newJLabel("梦落痴狂:亲!你可以直接输入年月查询.");//JLabel(Stringtext)创立具有指定文本的JLabel实例.privateJLabellt=newJLabel();privateJLabelld=newJLabel();//JLabel()创立无图像并且其标题为空字符串的JLabel.privateJLabellu=newJLabel("农历和节气");privateJLabelnull1=newJLabel();privateintlastTime;//privateStringlocalTime=null;privateStrings=null;privateSimpleDateFormatdateFormat=newSimpleDateFormat("yyyy年MMRdd日hh时mm^ss秒");年MM月dd日HH时mm分ss秒");//SimpleDateFormat允许以为日期-时间格式化年MM月dd日HH时mm分ss秒");SimpleDateFormatmyFmt=newSimpleDateFormat("yyyySimpleDateFormatmyFmt1=newSimpleDateFormat("yy/MM/ddHH:mm");SimpleDateFormatmyFmt2=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");得至1J:

2004年12月16日17时24分27秒04/12/1617:242004-12-1617:24:27.绘制程序界面publicwannianli(){super("万年历一生痴狂一世繁华BY梦落痴狂");〃框架命名this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//窗口关闭函数this.getContentPane().setLayout(newBorderLayout(9,10));//设置用户界面上的屏幕组件的格式布局,是java图形界面编程的常用方法jta.setLineWrap(true);〃长度大于分配长度时候那么换行jta.setFont(newFont("黑体",Font.BOLD,16));table.setBackground(Color.white);table.setGridC010r(Color.pink);//星期之间的网格线是灰色的tatable.setColumnSelectionAllowed(table.setSelectionBackground(Color.色table.setSelectionForeground(Color.true);〃将table中的列设置为可选择pink);〃中选定某一天时背景颜色为黑GREEN);table.setBackground(newColor(184,207,229));〃日期显示表格为浅蓝色table.setFont(newFont("黑体",Font.BOLD,24));//日期数字字体格式table.setRowHeight(26);//表格的高度table.addMouseListener(this);〃鼠标监听器、lu.setFont(newFont("黑体",Font.BOLD,22));//农历标签格氏jtfYear.addActionListener(this);//可输入年份的文本框//为各个按钮添加监听函数bLastYear.addActionListener(this);bNextYear.addActionListener(this);bLastMonth.addActionListener(this);bNextMonth.addActionListener(this);timeBox.addItemListener(newTimeSelectedChangedListener());//根据运行结果,可以设置类实现ActionListener接口和ItemListener接口以进行动作事件处理和选项变化事件处理.//将按钮添加到Jpane上pl.add(bLastYear);pl.add(jtfYear);〃年份输入文本框pl.add(bNextYear);pl.add(bLastMonth);pl.add(jtfMonth);pl.add(bNextMonth);p3.add(jsp,BorderLayout.SOUTH);p3.add(lu,BorderLayout.CENTER);p3.add(Id,BorderLayout.NORTH);p4.add(lt);p4.add(l);p5.add(p4,BorderLayout.SOUTH);p5.add(sp,BorderLayout.CENTER);p5.add(pl,BorderLayout.NORTH);p6.add(timeBox);p6.add(nulll);p6.add(timeField);p8.add(p2,BorderLayout.CENTER);p8.add(p7,BorderLayout.SOUTH);this.getContentPane().add(p3,BorderLayout.this.getContentPane().add(p5,BorderLayout.this.getContentPane().add(p6,BorderLayout.this.getContentPane().add(p8,BorderLayout.String[]strDate=DateFormat.getDateInstanceEAST);CENTER);SOUTH);WEST);().format(newDate()).split("-");//获取日期“2021-6-1"----->"2021""6""1"cld.set(Integer.parseInt(strDate[0]),Integer.parseInt(strDate[1]),0);//paresInt强转成int类型,Integer就是整数型,//月份从0开始,存到数组里012showCalendar(Integer.Integer.parseIntparseInt(strDate[0]),(strDate[1]),cld);jtfMonth.setEditable(false);//设置月份文本框为不可编辑jtfYear.setText(strDate[0]);jtfMonth.setText(strDate[1]);this.showTextArea(strDate[2]);ld.setFont(newFont("新宋体",Font.BOLD,24));newTimer(lt).start();newTimeThread().start();this.setBounds(200,200,700,350);〃显示窗口在屏幕的位置以及窗口自身的大小this.setResizable(false);//resizeable值为true时,表示在生成的窗体可以自由改变大小;this.setVisible(true);〃窗体可见计算各个月的天数运用if语句判断各个月的最大天数publicintgetDaysOfMonth(intYear,intMonth){//计算各月的天数if(Month==1||Month==3||Month==5||Month==7||Month==8||Month==10||Month==12){return31;}if(Month==4||Month==6||Month==9||Month==11){return30;}if(Year%4==0&&Year%100!=0||Year%400==0)//闰年{return29;}else{return28;}}年份输入限制这个监听事件用于监听程序界面的操作,获取年月的相应数据,用if语句对月份操作进行控制,如当前年月为2021年1月时,点击“上一月〞,年份减1,月份为12,即为2021年12月;当前月份为12时,点击“下一月〞,年份加1,月份为1,如图publicvoidactionPerformed(ActionEvente)//从界面上获取年月数据{if(e.getSource(尸jtfYear||e.getSource()==bLastYear||e.getSource()==bNextYear||e.getSource()==bLastMonth||e.getSource()==bNextMonth)(intm,y;try//限制输入的年份正确,异常限制(if(jtfYear.getText().length()!=4)(thrownewNumberFormatException();//数字格式错误}y=Integer.parseInt(jtfYear.getText());m=Integer.parseInt(jtfMonth.getText());}catch(NumberFormatExceptionex)(JOptionPane.showMessageDialog(this,"请输入4位0-9的数字!〞,JOptionPane.ERROR_MESSAGE);return;}ld.setText("没有选择日期〞);fofor(inti=0;i<lastTime+1;i++){dtm.removeRow(0);}if(e.getSource()==bLastYear){jtfYear.setText(Stringif(e.getSource()==bNextYear){jtfYear.setText(Stringif(e.getSource()==bLastMonth)(if(m==1)valueOf(--y));}valueOf(++y));}{jtfYear.setText(String.valueOf(--y));m=12;jtfMonth.setText(String.valueOf(m));}else{jtfMonth.setText(String.valueOf(--m));}ifif(e.getSource()==bNextMonth)(if(m==12)(jtfYear.setText(String.valueOf(++y));m=1;jtfMonth.setText(String.valueOf(m));

else(jtfMonth.setText(String.valueOf(++m));))cld.set(y,m-1,0);showCalendar(y,m,cld);))获取点击表格详细日期publicvoidmouseClicked(MouseEvente)(jta.setText(null);intr=table.getSelectedRow();intc=table.getSelectedColumn();if(table.getValueAt(r,c)==null)(ld.setText("没有选择日期〞);)else(this.showTextArea(table.getValueAt(r,c));))打印日期privatevoidshowTextArea(Objectselected)(ld.setText(jtfYear.getText()+"年"+jtfMonth.getText()+"月"+selected+"日");国际时间系统调用显示privatevoidupdateTimeText(StringtimeZoneId){if(timeZoneId!=null){TimeZonetimeZone=TimeZone.getTimeZone(timeZoneId);dateFormat.setTimeZone(timeZone);Calendarcalendar=Calendar.getInstance();calendar.setTimeZone(timeZone);timeField.setText(dateFormat.format(calendar.getTime()));}else{timeField.setText(null);)privateclassTimeSelectedChangedListenerimplementsItemListener{publicvoiditemStateChanged(ItemEvente){if(e.getStateChange()==ItemEvent.SELECTED){if(e.getItem()instanceofString){s=e.getItem().toString();))))privateclassTimeThreadextendsThread{publicvoidrun(){while(true){updateTimeText(s);try{Thread.sleep(100);}catch(InterruptedExceptione){e.printStackTrace();}}}}显示系统时间classTimerextendsThread//显示系统时间{privateJLabellt;privateSimpleDateFormatfy=newSimpleDateFormat("GHH:mm:ss");//日期显示格式publicTimer(JLabellt){this.lt=lt;}publicvoidrun(){while(true){try{lt.setText(fy.format(newDate()));this.sleep(500);}catch(InterruptedExceptionex)ex.printStackTrace();))))农历计算模块本模块中,由于没有解决获取公历日期变动数据,只能显示系统时间,即只能显示当天农历finalprivatestaticlong[]lunarInfo=newlong[]{0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x055d2,0x04ae0,0x0a5b6,0x0a4d0,0x0d250,0x1d255,0x0b540,0x0d6a0,0x0ada2,0x095b0,0x14977,0x04970,0x0a4b0,0x0b4b5,0x06a50,0x06d40,0x1ab54,0x02b60,0x09570,0x052⑵0x04970,0x06566,0x0d4a0,0x0ea50,0x06e95,0x05ad0,0x02b60,0x186e3,0x092e0,0x1c8d7,0x0c950,0x0d4a0,0x1d8a6,0x0b550,0x056a0,0x1a5b4,0x025d0,0x092d0,0x0d2b2,0x0a950,0x0b557,0x06ca0,0x0b550,0x15355,0x04da0,0x0a5d0,0x14573,0x052d0,0x0a9a8,0x0e950,0x06aa0,0x0aea6,0x0ab50,0x04b60,0x0aae4,0x0a570,0x05260,0x0f263,0x0d950,0x05b57,0x056a0,0x096d0,0x04dd5,0x04ad0,0x0a4d0,0x0d4d4,0x0d250,0x0d558,0x0b540,0x0b5a0,0x195a6,0x095b0,0x049b0,0x0a974,0x0a4b0,0x0b27a,0x06a50,0x06d40,0x0af46,0x0ab60,0x09570,0x04af5,0x04970,0x064b0,0x074a3,0x0ea50,0x06b58,0x055c0,0x0ab60,0x096d5,0x092e0,0x0c960,0x0d954,0x0d4a0,0x0da50,0x07552,0x056a0,0x0abb7,0x025d0,0x092d0,0x0cab5,0x0a950,0x0b4a0,0x0baa4,0x0ad50,0x055d9,0x04ba0,0x0a5b0,0x15176,0x052b0,0x0a930,0x07954,0x06aa0,0x0ad50,0x05b52,0x04b60,0x0a6e6,0x0a4e0,0x0d260,0x0ea65,0x0d530,0x05aa0,0x076a3,0x096d0,0x04bd7,0x04ad0,0x0a4d0,0x1d0b6,0x0d250,0x0d520,0x0dd45,0x0b5a0,0x056d0,0x055b2,0x049b0,0x0a577,0x0a4b0,0x0aa50,0x1b255,0x06d20,0x0ada0};finalprivatestaticint[]year20=newint[]{1,4,1,2,1,2,1,1,2,1,2,1};finalprivatestaticint[]year19=newint[]{0,3,0,1,0,1,0,0,1,0,1,0};finalprivatestaticint[]year2000=newint[]{0,3,1,2,1,2,1,1,2,1,2,1};publicfinalstaticString[]nStr1=newString[]{","五","六","七","八","九","十","H,"十二"};privatefinalstaticString[]Gan=newString[]{","己","庚","辛","壬","癸"};privatefinalstaticString[]Zhi=newString[]{"","正","二","三","四"甲","乙","丙","丁","戊"子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"};privatefinalstaticString[]Animals=newString[]{"鼠","牛","虎","兔","龙","蛇","马","羊","3ft","鸡","狗","猪"};//传回农历y年的总天数finalprivatestaticintlYearDays(inty){inti,sum=348;for(i=0x8000;i>0x8;i>>=1){if((lunarInfo[y-1900]&i)!=0)sum+=1;}return(sum+leapDays(y));}//传回农历y年闰月的天数finalprivatestaticintleapDays(inty){if(leapMonth(y)!=0){if((lunarInfo[y-1900]&0x10000)!=0)return30;elsereturn29;}elsereturn0;}//传回农历y年闰哪个月1-12,没闰传回0finalprivatestaticintleapMonth(inty){return(int)(lunarInfo[y-1900]&0xf);}//传回农历y年mM的总天数finalprivatestaticintmonthDays(inty,intm){if((lunarInfo[y-1900]&(0x10000>>m))==0)return29;elsereturn30;}//传回农历y年的生肖finalpublicstaticStringAnimalsYear(inty){returnAnimals[(y-4)%12];}//传入月日的offset传回干支,0=甲子finalprivatestaticStringcyclicalm(intnum){return(Gan[num%10]+Zhi[num%12]);}//传入offset传回干支,0=甲子finalpublicstaticStringcyclical(inty){intnum=y-1900+36;return(cyclicalm(num));}//传出农历.year0.month1.day2.yearCyl3.monCyl4.dayCyl5.isLeap6finalprivatelong[]Lunar(inty,intm){long口nongDate=newlong[7];inti=0,temp=0,leap=0;DatebaseDate=newGregorianCalendar(1900+1900,1,31).getTime();DateobjDate=newGregorianCalendar(y+1900,m,1).getTime();longoffset=(objDate.getTime()-baseDate.getTime())/if(y<2000)offset+=year19[m-1];if(y>2000)offset+=year20[m-1];if(y==2000)offset+=year2000[m-1];nongDate[5]=offset+40;nongDate[4]=14;for(i=1900;i<2050&&offset>0;i++){temp=lYearDays(i);offset-=temp;nongDate[4]+=12;}if(offset<0){offset+=temp;i--;nongDate[4]-=12;)nongDate[0]=i;nongDate[3]=i-1864;leap=leapMonth(i);//闰哪个月nongDate[6]=0;for(i=1;i<13&&offset>0;i++){//闰月if(leap>0&&i==(leap+1)&&nongDate[6]==0){--i;nongDate[6]=1;temp=leapDays((int)nongDate[0]);}else{temp=monthDays((int)nongDate[0],i);}//解除闰月if(nongDate[6]==1&&i==(leap+1))nongDate[6]=0;offset-=temp;if(nongDate[6]==0)nongDate[4]++;}if(offset==0&&leap>0&&i==leap+1){if(nongDate[6]==1){nongDate[6]=0;}else{nongDate[6]=1;--i;--nongDate[4];}}if(offset<0){offset+=temp;--i;--nongDate[4];}nongDate[1]=i;nongDate[2]=offset+1;returnnongDate;}//传出y年mMd日对应的农历.year0.month1.day2.yearCyl3.monCyl4.dayCyl5.isLeap6finalpublicstaticlong口calElement(inty,intm,intd){long[]nongDate=newlong[7];inti=0,temp=0,leap=0;DatebaseDate=newGregorianCalendar(0+1900,0,31).getTime();DateobjDate=newGregorianCalendar(y,m-1,d).getTime();longoffset=(objDate.getTime()-baseDate.getTime())/nongDate[5]=offset+40;nongDate[4]=14;for(i=1900;i<2050&&offset>0;i++){temp=lYearDays(i);offset-=temp;nongDate[4]+=12;}if(offset<0){offset+=temp;i--;nongDate[4]-=12;}nongDate[0]=i;nongDate[3]=i-1864;leap=leapMonth(i);//闰哪个月nongDate[6]=0;for(i=1;i<13&&offset>0;i++){//闰月if(leap>0&&i==(leap+1)&&nongDate[6]==0){--i;nongDate[6]=1;temp=leapDays((int)nongDate[0]);}else{temp=monthDays((int)nongDate[0],i);}//解除闰月if(nongDate[6]==1&&i==(leap+1))nongDate[6]=0;offset-=temp;if(nongDate[6]==0)nongDate[4]++;}if(offset==0&&leap>0&&i==leap+1){if(nongDate[6]==1){nongDate[6]=0;}else{nongDate[6]=1;--i;--nongDate[4];}

)if(offset<0){offset+=temp;--i;--nongDate[4];)nongDate[1]=i;nongDate[2]=offset+1;returnnongDate;inintday){publicfinalstaticStringgetChinaDate(Stringa=if(day==10)return"初十"

温馨提示

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

最新文档

评论

0/150

提交评论