课件课后练习代码经典案例_第1页
课件课后练习代码经典案例_第2页
课件课后练习代码经典案例_第3页
课件课后练习代码经典案例_第4页
课件课后练习代码经典案例_第5页
已阅读5页,还剩48页未读 继续免费阅读

下载本文档

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

文档简介

JavaSE01万年历工扫雷游戏——完成Field类及Minesfield类的设计及除getAround方法外的编图-图-privateCalendarcalendar=publicvoidincMonth()calendar.add(Calendar.MONTH1);}在PerpetualCalendar类中添加decMonthpublicvoiddecMonth()calendar.add(Calendar.MONTH1);}publicclassPerpetualCalendarprivateCalendarFrameprivateCalendarcalendar=}publicPerpetualCalendar()view=new}publicvoidincMonth()calendar.add(Calendar.MONTH,}publicvoiddecMonth()calendar.add(Calendar.MONTH,-}publicvoidshow()}publicstaticvoidmain(String[]args)throwsExceptionfor(LookAndFeelInfoinfo:UIManager.getInstalledLookAndFeels()){if}}PerpetualCalendarcalendar=newPerpetualCalendar();}publicclassCalendarFrameextendsJFrameprivatePerpetualCalendarcalendarprivateJLabelyearAndMonth;//显示年月的privateJLabelmonthInc;//显示上一月(<<)的privateJLabelmonthDec;//显示下一月(>>)的publicvoidsetYearAndMonth(StringyearAndMonth)}publicvoidsetCalendar(intbegin,intmaxDay,int{for(inti=0;i<days.length;i++)}for(inti=1;i<=maxDay;i++)StringdateStr=i<=9?""+i:""+i;days[begin+i-2].setText(dateStr);if(currentDay==i)days[begin+i-} }publicCalendarFrame(PerpetualCalendarcalendar)this.calendar=calendar;setSize(400,350);DimensiondsetLocation((d.width-getWidth())/2,(d.height-getHeight())/2);}privatevoidinit()ContainercontentPane=getContentPane();for(inti=0;i<weeks.length;{weeks[i].setBounds(40+i*50,50,50,30);} for(inti=0;i<days.length;i++)days[i]=newdays[i].setBounds(40+(i7)*+/7)*50,30); monthInc=newmonthInc.setBounds(250,20,monthDec=newmonthDec.setBounds(130,20,yearAndMonth=newyearAndMonth.setBounds(165,80,}在CalendarFrame类的init方法添加为上一月(<<)Label设置的代码monthDec.addMouseListener(newMouseAdapter()publicvoidmouseEntered(MouseEvent{}publicvoidmouseExited(MouseEvent{}publicvoidmouseClicked(MouseEvent{}在CalendarFrame类的init方法添加为下一月(>>)Label设置的代码monthInc.addMouseListener(newMouseAdapter()publicvoidmouseEntered(MouseEvent{}publicvoidmouseExited(MouseEvent{}publicvoidmouseClicked(MouseEvent{}importimportimportimportpublicclass{privateCalendarFrameprivateCalendarcalendar=Calendar.getInstance();privateSimpleDateFormatsdf=newSimpleDateFormat("yyyy年MMpublicvoid{ publicvoiddecMonth()calendar.add(Calendar.MONTH,- publicPerpetualCalendar()view=new publicvoidshow() publicstaticvoidmain(String[]args)throwsExceptionfor(LookAndFeelInfoinfoUIManager.getInstalledLookAndFeels()){}}PerpetualCalendarcalendar=new}}importimportjava.awt.Container;importimportimportclassCalendarFrameextendsJFrameprivatestaticfinallongserialVersionUID=privatePerpetualCalendarcalendar;privateJLabelprivateJLabelprivateJLabelprivateJLabel[]weeks={newJLabel("newJLabel("newJLabel("newJLabel("newJLabel("newJLabel("newJLabel("六"),};privateJLabel[]days=newpublicvoidsetYearAndMonth(StringyearAndMonth) publicvoidsetCalendar(intbegin,intmaxDay,int for(inti=0;i<days.length;i++) for(inti=1;i<=maxDay;i++)StringdateStr=i<=9?""+i:""+days[begin+i-2].setText(dateStr); if(currentDay==i)}}days[begin+i-}}publicCalendarFrame(PerpetualCalendar{this.calendar=setSize(400,DimensiondsetLocation((d.width-getWidth())/(d.height-getHeight())/}privatevoidinit()ContainercontentPane=for(inti=0;i<weeks.length;i++)weeks[i].setBounds(40+i*50,50,}for(inti=0;i<days.length;i++)days[i]=newdays[i].setBounds(40+(i%7)*50,+(i/7)*50,30);}monthInc=newmonthInc.setBounds(250,20,20,monthDec=newmonthDec.setBounds(130,20,20,yearAndMonth=newyearAndMonth.setBounds(165,20,80,monthInc.addMouseListener(newMouseAdapter()publicvoidmouseEntered(MouseEvente) publicvoidmouseExited(MouseEvente) publicvoidmouseClicked(MouseEvente) monthDec.addMouseListener(newMouseAdapter()publicvoidmouseEntered(MouseEvente) publicvoidmouseExited(MouseEvente) publicvoidmouseClicked(MouseEvente) }}}privateSimpleDateFormatsdfnewSimpleDateFormat("yyyy年MMprivatevoidupdateCalendar()intmaxDayintcurrentDay=-Calendarc=if(c.get(Calendar.YEAR)==&&c.get(Calendar.MONTH)calendar.get(Calendar.MONTH))currentDay= calendar.get(Calendar.MONTH),intbegin=view.setCalendar(begin,maxDay,yyyy年MM}publicvoidincMonth()calendar.add(Calendar.MONTH,}publicvoiddecMonth()calendar.add(Calendar.MONTH,-1);}publicPerpetualCalendar()view=newCalendarFrame(this);}importimportimportimportpublicclassPerpetualCalendarprivateCalendarFrameprivateCalendarcalendar=privateSimpleDateFormatsdfnewSimpleDateFormat("yyyy年MMpublicvoidincMonth()calendar.add(Calendar.MONTH, publicvoiddecMonth()calendar.add(Calendar.MONTH,- publicPerpetualCalendar()view=new privatevoidupdateCalendar()intmaxDayintcurrentDay=-Calendarc=if(c.get(Calendar.YEAR)==&&c.get(Calendar.MONTH)calendar.get(Calendar.MONTH))currentDay= c.set(calendar.get(Calar.get(Calendar.MONTH),intbegin=view.setCalendar(begin,maxDay, publicvoidshow()}publicstaticvoidmain(String[]args)throwsExceptionfor(LookAndFeelInfoinfo:UIManager.getInstalledLookAndFeels()){if}}PerpetualCalendarcalendar=newPerpetualCalendar();}}classCalendarFrameJFrameprivateJLabel[]weeks={newJLabel("newJLabel("newJLabel("newJLabel("newJLabel("newJLabel("newJLabel("六"),};privateJLabel[]days=newJLabel[42];publicvoidsetYearAndMonth(StringyearAndMonth)}publicvoidsetCalendar(intbegin,intmaxDay,int{for(inti=0;i<days.length;i++)}for(inti=1;i<=maxDay;i++)StringdateStr=i<=9?""+i:""+days[begin+i-if(currentDay==i)days[begin+i-}}}publicCalendarFrame(PerpetualCalendarcalendar)this.calendar=setSize(400,DimensiondsetLocation((d.width-getWidth())/(d.height-getHeight())/}privatevoidinit()ContainercontentPane=for(inti=0;i<weeks.length;i++)weeks[i].setBounds(40+i*50,50,}for(inti=0;i<days.length;i++)days[i]=newdays[i].setBounds(40+(i%7)*+/*}monthInc=newmonthInc.setBounds(250,20,20,monthDec=newmonthDec.setBounds(130,20,20,yearAndMonth=newyearAndMonth.setBounds(165,20,80,monthInc.addMouseListener(new{publicvoidmouseEntered(MouseEvente)}publicvoidmouseExited(MouseEvente)}publicvoidmouseClicked(MouseEvente)}monthDec.addMouseListener(newMouseAdapter()publicvoidmouseEntered(MouseEvente)}publicvoidmouseExited(MouseEvente)}publicvoidmouseClicked(MouseEvente)}}}图-点击其中一个小方块(就是一个il),如果该il不是,但是它周围有,该ild上显示的是周围的数目如图-所示。如果点击的这个ild为那么所有il将部打如图-所示:图-图-图-图-图-publicstaticfinalintSIZE=16;//设置雷的区域是16行16publicstaticfinalintMINES_NUM=40;//设置雷的总数目为privateList<Field>publicFieldgetField(intx,inty)intindex=fields.indexOf(newField(x,y));if(index>=0){return}elsereturn}}publicintgetFieldStyle(intx,inty)returngetField(x,}publicvoidsetFieldStyle(intx,inty,intstyle)getField(x,}publicintgetMineValue(intx,inty)returngetField(x,}publicbooleanisMine(intx,inty)returngetField(x,}publicbooleanisCovered(intx,inty)returngetField(x,y).getStyle()==}publicbooleanisOpended(intx,inty)returngetField(x,y).getStyle()==}publicbooleanisMarked(intx,inty)returngetField(x,y).getStyle()==}publicvoidsetCovered(intx,inty)getField(x,}publicvoidsetOpended(intx,inty)getField(x,}publicvoidsetMarked(intx,inty)getField(x,}{fields.add(newField(x,}publicvoidinitMinefield()fields=newArrayList<Field>();Randomr=newRandom();for(intx=0;x<SIZE;{for(inty=0;y<SIZE;}//随机生成40for(inti=0;i<MINES_NUM;{intx,{x=r.nextInt(SIZE);y=}while(isMine(x,}publicclassFieldpublicstaticfinalintSTYLE_COVERD=publicstaticfinalintSTYLE_OPENED=2;publicstaticfinalintSTYLE_MARKED=3;publicbooleanequals(Objectprivateintintintstyle=intmineValue=publicField(intx,inty)this.x=x;this.y=}publicvoidsetMine()this.mineValue=-}publicboolean{returnmineValue==-{if(objinstanceofField)Fieldfield=(Field)returnx==field.x&&y==}elsereturn }publicStringtoString()if(STYLE_COVERD==style)return}elseif(STYLE_MARKED==style)return}elseif(STYLE_OPENED==style)if(isMine())return}elsereturn""}} returnpublicclassMinesfieldpublicstaticfinalintSIZE=publicstaticfinalintMINES_NUM=privateList<Field>publicFieldgetField(intx,inty)intindex=fields.indexOf(newField(x,if(index>=0)return}elsereturn}}publicintgetFieldStyle(intx,inty)returngetField(x,}publicvoidsetFieldStyle(intx,inty,intstyle)getField(x,}publicintgetMineValue(intx,inty)returngetField(x,}publicbooleanisMine(intx,inty)returngetField(x,}publicbooleanisCovered(intx,inty)returngetField(x,y).getStyle()==}publicbooleanisOpended(intx,inty)returngetField(x,y).getStyle()==}publicbooleanisMarked(intx,inty)returngetField(x,y).getStyle()==}publicvoidsetCovered(intx,inty)getField(x,}publicvoidsetOpended(intx,inty)getField(x,}publicvoidsetMarked(intx,inty)getField(x,}publicvoidinitMinefield()fields=newArrayList<Field>();Randomr=newRandom();for(intx=0;x<SIZE;{for(inty=0;y<SIZE;{fields.add(newField(xy));}}//随机生成40for(inti=0;i<MINES_NUM;{intx,{x=r.nextInt(SIZE);y=}while(isMine(x,}publicMinesfield()}publicclassTestField}} }

图-publicclassFieldintSTYLE_COVERD=intSTYLE_OPENED=2;intSTYLE_OPENED=intintintstyle=privateintmineValuepublicField(intx,{this.x=this.y=}publicintgetX()return}publicintgetY()return}publicvoidsetStyle(intstyle)this.style=}publicintgetStyle()return}publicvoidsetMineValue(int{this.mineValue=}publicintgetMineValue()return}publicvoidsetMine()this.mineValue=-}publicbooleanisMine()returnmineValue==- publicbooleanequals(Objectobj)if(objinstanceofField)Fieldfield=(Field)returnx==field.x&&y==}elsereturn}}publicStringtoString()if(STYLE_COVERD==style)return}elseif(STYLE_MARKED==style)return}elseif(STYLE_OPENED==style)if(isMine())return}elsereturn""+}}return}}importimportjava.util.List;importjava.util.Random;publicclassMinesfieldpublicstaticfinalintSIZE=16;publicstaticfinalintMINES_NUM=40;privateListpublicvoidinitMinefield()fields=newRandomr=newfor(intx=0;x<SIZE;x++)for(inty=0;y<SIZE;y++)fields.add(newField(x,}}for(inti=0;i<MINES_NUM;i++)intx,dox=y=}while(isMine(x,getField(x,}}publicFieldgetField(intx,inty)intindex=fields.indexOf(newif(index>=0)return(Field)}elsereturn}}publ

温馨提示

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

评论

0/150

提交评论