还剩11页未读,继续阅读
本资源只提供10页预览,全部文档请下载后查看!喜欢就下载吧,查找使用更方便
文本内容:
The Javatask-graphical calendarName:XiaotongYangBTEC-11222132012/5/25〃计算每月的第一天firstDay+=daysInMonth[i];if monthFEBRUARYcalendar.isLeapYearyear{firstDay++;}return firstDay%7;计算每年的第一天private intgetFirstDayOfYearint year{//int leap Years;int hundreds;int fourHundreds;int first;{//如果年份小于年if year15821582返回return-1;//-1}leap Years=year-1581/4;hundreds=year-1501/100;leap Years-=hundreds;fourHundreds=year-1201/400;leap Years+=fourHundreds;得到每年第一天first=5+year-15824-leap Years%7;//returnfirst;Effect diagram:•J•TXJJz年2012月六月▼更新|星期日星期一星期二星期三星期四星期五星期六123Q45789=1310111214151617181920212223J29242526272830星期日星期一星期二星期三星期四星期五星期六123456789101112131415161718192021222324W302526272829I年2000月;六月二更新〃得到每年第一天first=5+year^s-1582+leapYear%7;I putthe meaningof thetwo yearmistake,years shouldnot have1s andleapyear shouldhave”s”.for int i=0;i7;i++{g.drawStringdays[i],CELLWIOrH-fontMetric.stringWidthdays/2+i*CELLW工DTH,〃计算需要的行的数量numRows=getNumberRowsCsearchYear,searchMonth;totalHeight=numRows*CELLHEIGHT;〃得到总的表格高度for inti=0;i=totalwidth;i+=CELLWIDTH{g.drawLinei,TOP,i,TOP+totalHeight;〃绘匍袤格线The errorI tooka longtime tounderstand,finally foundI Idid notwrite in,so theresult isa mistake,unable tocontinue..Year.da.cal.ge.Firs.Da.O.Yea.calculate.that.Th.incomin.yea.parameter.representin.th.firs.da.retur.a.intege.value.Th.firs.da.o.th.mont.i.calculate.b.ge.Firs.Da.O.Mont.method.T.calculat.thes.tw.parameters.ca.b.positione.i.th.for.o.drawin.positio.data.In thisset ofprocedures useda lotof methods,for example,interface,background,drawing tables,computation,algorithm.
146.Summar.an.evaluation
151.Structure.programming
1.1Structured programmingprinciple.Luse..subroutine.cod.bIock..English.bloc.structure..fo.cycl.an.whil.cycLstructure.t.replac.th.traditiona.goto.Hopin.t.improv.compute.progra.clarity.qualit.an.developmen.time.an.avoi.t.writ.spaghett.code.
1.2Structured programmingfeaturesStructured programmingmust be the functionof thesystem ofrational analysis,decomposition andsynthesis,so therelated personnelrequirements higher,in addition,when usinga structuredprogrammingmethod,the needfor datamanagement.
1.3Structured programmingn.althoug.th.progra.ma.no.b..structure.programming.GeneralLconsidere.th.structure.programmin.theor.i.attribute.t.Boeh.an.Jaakk.Pin.i.l966publishe.papers.on.reaso.ma.b.tha.Dykstr.quote.i.thi.paper.Structure.programmin.theor.withou.mentio.o.ho.t.writ..structure.program.ther.i.n.mentio.o.structure.progra.analysis.thenl
96.tol970i.th.l970s.Deckar.Stella.Rober.Floyd.Ton.Hal.an.othe.compute.scientist.i.thi.fiel.hav.man.contributions.
2.1Structure design1File structuredesignComponent window,button,text box,select box,scroll bars,query the event processingand datecalculation2The designof datastructuresearchMonth=calendar.get Calendar.MONTH;searchYear=calendar.getCalendar.YEAR;tfYear.setText String.valueOf searchYear;forint i=0;i12;i++{〃增加下拉框选项monthChoice.additem months[i];设置当前颜色g.setColor color.lightGray;//设置当前使用字体g.setFont bigFont;//g.drawString searchYear-i-v年”,60,TOP+70;〃绘制字符串g.drawString searchMonth+1+月”,“200,TOP+130;For intI=0;I=totalWidth;I+=CELLWIDTH{g.drawLine i,TOP,i„TOP+totalHeight;
2.2The perpetualcalendar functiondesign1Overall functiondesignandprocess..desig.calenda.yea.an.th.mont.t.ente.th.query.clic.the.update.button.yo.ca.ge.th.correspondin.calendar.It.convenien.fo.th.use..query.Thi.calenda.i.th.graphica.use.interfac.o.th.calendar.The mainprocess isthe interfacedesign,query theevent processingand datecalculation.2Each modulefunctiondesignand flowQueryand display part:The queryinput portionincludes atext field,fortheinput year,a selecteddrop-down box,used toselect month,an updatebutton,to startquerying theevent.Query eventprocessing part:Event targetattribute canbetheevent source,it isused withbuttonexample arecompared,can knowthe timeis derivedfrom thebutton.Afte.th.incident.respectively.cal.th.getTex.metho.an.getSelectedlnde.metho.fro.th.yea.an.mont.selecto.inpu.tex.fiel.drop-dow.bo.t.obtai.quer.o.th.yea.an.th.month.Not.that.fro.th.tex.fiel.o.th.result.obtaine.fo.th.string.yo.nee.Leal.th.Intege.class\parseln.metho.t.conver.i.t.a.intege.value.Th.GregorianCalend.clas.tim.i.calculate.fro.th.beginnin.o.l581,whe.th.use.input..tim.earlie.tha.th.time.nee.t.handle.Progra.firs.call.th.repain.metho.t.redra.th.screen.pu.th.dat.informatio.display.The dateof calculationsection:Date ofthe calculationincludes mainlycalculate everyyear the firstday andthefirstday ofthe month.3User interfacedesignThe interfaceis mainlycomposed oftwo parts—the queryinput portionand adisplaypart.The queryinput portionhaving aselected drop-down box,used toselect month,an updatebutton,tostart queryingtheevent.3-1Procedures usedin thecontrol structureis introduced.Th.GregorianCalenda.clas.ca.b.use.t.obtai.th.cunen.time.t.initializ.th.quer.inpu.data.Ge..Calendar..Strin.valueO.metho.ca.b.converte.t..strin.o.othe.basi.types.Intege.valu.ca.b.Intege.class\toStrin.metho.t.achieve.The Graphicsclasss setColormethod tospecify thecurrent useof color,the setFontmethod toset thefontusedinthe specifiedlocation,the drawStringmethod todraw thestring..Dra.tabl.lin.whe.yo.nee.t.calculat.draw.position.an.the.us.th.drawLin.metho.t.dra..straigh.line.Th.followin.cod.dra..horizonta.line.perpendicula.t.th.lin.o.th.principl.o.drawin.th.same.
3.2Procedures forthe variousfunctions andparametersleap Years=year-1581/4;hundreds=year-1501/100;-二leapYearshundreds;fourHundreds=year-1201/400;{星期日「星期一丁,星期二“,“星期三期星期四“,星期五「星期六”};String days[]=口={”一月「二月」三月」四月五月」六月」七月」八月」九月「十月-一月String months7r“,“十二月”};〃每个月的天数int daysInMonth[]={31,28,31,30,31,30,31,31,30,31,30,31};
3.3Program allthe codepackageclock;import java.applet.Applet;import java.awt.*;import java.util.*;public classCalendarApplet extendsApplet{〃顶端距离static finalint TOP=70;〃单元格尺寸static finalint CELLWIDTH=50,CELLHEIGHT=30;〃边界距离static finalint MARGIN=3;static finalint FEBRUARY=1;TextField tfYear=new TextFieldH2004n,5;//显示年份的文本域〃月份选择下拉框Choice monthChoice=new ChoiceQ;更新〃更新按钮Button btUpdate=new Button日历对象GregorianCalendar calendar=new GregorianCalendar;//Font smallFont=new Font,,TimesRomann,Font.PLAIN,15;//显示小字体Font bigFont=new FontnTimesRomann,Font.BOLD,50;〃显示大字体{星期日一星期一「,星期二「星期三期星期四「星期五一星期六”};String days[]={一月—二月「三月“,“四月”五月「六月「七月”,”八月「九月?十月String months[]=J-一月“,“十二月”};7r每个月的天数int daysInMonth[]={31,28,31,30,31,30,31,31,30,31,30,31};//〃查询的年份及月份int searchMonth,search Year;public voidinit{〃设置背景颜色setBackgroundColor.white;〃得至系统年份searchMonth=calendar.getCalendar.MONTH;U〃得至系统月份searchYear=calendar.getCalendar.YEAR;lladdnew Labeln年:;//增加组件到Applet设置文本域文字tfYear.setTextString.valueOfsearchYear;//addtfYear;addnew LabelH月:;//设置月份选择下拉框的显示字体monthChoice.setFontsmallFont;for inti=0;i12;i++{;〃增力口下拉框选项monthChoice.addItemmonths[i]〃设置下拉框当前选择项monthChoice.selectsearchMonth;addmonthChoice;addbtUpdate;二;〃得至中的组件数量int componentCountthis.getComponentCount UAppletfor inti=0;icomponentCount;i-i-4-{设置所有组件的显示字体getComponenti.setFontsmallFont;//public voidpaintGraphics g{〃显示字体的对象FontMetrics fontMetric;FontMetricsint fontAscent;int dayPos;〃总的宽度,高度int totalWidth,totalHcight;〃行数int numRows;〃水平和垂直方向单元格数量int xNum,yNum;int numDays;〃显示天数字符串String dayStr;int margin;〃设置当前颜色g.setColorColor.lightGray;〃设置当前使用字体g.setFontbigFont;g.drawStringsearchYear+n^n,60,TOP+70;//^$iJ^^^g.drawStringsearchMonth+1+”月n,200,TOP+130;g.setColorColor.black;g.setFontsmallFont;〃获取变量初值fontMetric=g.getFontMetrics;fontAscent=fontMetric.getAscent;dayPos=TOP+fontAscent/2;〃得到总的表格宽度totalWidth=7*CELLWIDTH;for inti=0;i7;i++{g.drawStringdays[i],CELLWIDTH-fontMetric.stringWidthdays[i]/2+〃绘制表格标题栏i*CELLWIDTH,dayPos-20;}计算需要的行的数量numRows=getNumberRowssearchYear,searchMonth;//〃得至总的表格高度totalHeight=numRows*CELLHEIGHT;Ufor inti=0;i=totalWidth;i+=CELLWIDTH{〃绘制表格线g.drawLinei,TOP,i,TOP+totalHeight;for inti=0,j=TOP;i=numRows;i++,j+=CELLHEIGHT{g.drawLine0,j,〃绘制表格线totalWidth,j;xNum=getFirstDayOfMonthsearchYear,searchMonth+1*CELLWIDTH-MARGIN;yNum=TOP+MARGIN+fontAscent;numDays=daysInMonth[searchMonth]+calendar.isLeapYearsearchYear searchMonth=FEBRUARY1:0;for intday=1;day=numDays;day++{dayStr=Integer.toStringday;〃绘制字g.drawStringdayStr,xNum-fontMetric.stringWidthdayStr,yNum;符串xNum+=CELLWIDTH;if xNumtotalWidth{xNum=CELLWIDTH-MARGIN;yNum+=CELLHEIGHT;public booleanactionEvent e,Object o{int searchYearlnt;if e.target==btUpdate{〃得到查询月份searchMonth=monthChoice.getSelectedIndex;〃得到查询年份search Yearlnt=Integer.parseInttfYear.getText,10;if searchYearlnt1581{searchYear=search Yearlnt;〃重绘屏幕repaint;return true;return false;}{〃得至」行数量private intgetNumberRowsint year,int monthIint firstDay;int numCells;〃年份小于年,则返回if year1582{1582-1return-1;if month0||month11{return-1;}〃计算月份的第一天firstDay=getFirstDayOfMonthyear,month;if month==FEBRUARYfirstDay==0!calendar.isLeapYearyear{return4;numCells=firstDay+daysInMonth[month];if month==FEBRUARYcalendar.isLeapYearyear{numCells++;〃返回行数return numCells=355:6;〃得到每月的第一天private intgetFirstDayOfMonthint year,int month{int firstDay;inti;〃年份小于年,返回if year1582{1582-1return-1;}〃月份数错误,返回if month0||month11{-1return-1;〃得至」每年的第一天firstDay=getFirstDayOfYearyear;Ifor i=0;imonth;i++{。
个人认证
优秀文档
获得点赞 0