欢迎来到天天文库
浏览记录
ID:43091043
大小:257.57 KB
页数:40页
时间:2019-09-26
《java(教学0)韩建雷9练习题第九章》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、第九章JAVA多线程机制9-1编程设计弹跳球画面,即小球在一定区域内弹跳,有开始和关闭的相关控件。答案:程序①(Bounce)packagebounce;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;*Showsananimatedbouncingball.*@version1.332007-05-17*@authorCayHorstmann*/publicclassBounce{publicstaticvoidmain(String[]args){EventQueue.
2、invokeLater(newRunnable(){publicvoidrun(){JFrameframe=newBounceFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);});广**Theframewithballcomponentandbuttons.*/classBounceFrameextendsJFrame{privateBallComporientcomp;publicstaticfinalintSTEPS=10
3、00;publicstaticfinalintDELAY=3;*ConstructstheframewiththecomponentforshowingthebouncingballandStartandClose*buttons*/publicBounceFrame(){setTitle(”Bounce”);comp=newBallComponent();add(comp,BorderLayout.CENTER);JPanelbuttonPanel=newJPanel();addButton(buttonPanel,"Start”,newAc
4、tionListener(){publicvoidactionPerformed(ActionEventevent)addBall();});addButton(buttonPanel,"Close",newActionListener(){publicvoidactionPerformed(ActionEventevent){System.exit(O);}});add(buttonPanel,BorderLayout.SOUTH);pack();}*Addsabuttontoacontainer.*@paramcthecontainer*@
5、paramtitlethebuttontitle*@paramlistenertheactionlistenerforthebutton*/publicvoidaddButton(Containerc,Stringtitle,ActionListenerlistener){JButtonbutton=newJButton(title);c.add(button);button.addActionListener(listener);}*Addsabouncingballtothepanelandmakesitbounce1,000times.*
6、/publicvoidaddBall()try{Ballball=newBall();comp.add(ball);for(inti=1;i<=STEPS;i++){ball.move(comp.getBounds());comp.paint(comp.getGraphics());Thread.sleep(DELAY);}}catch(InterruptedExceptione){}}}程序②(Ball)packagebounce;importjava.awt.geom.*;Aballthatmovesandbouncesofftheedge
7、sofarectangle*@version1.332007-05-17*@authorCayHorstmann*/publicclassBallprivatestaticfinalintXSIZE=15;privatestaticfinalintYSIZE=15;privatedoublex=0;privatedoubley=0;privatedoubledx=1;privatedoubledy=1;*Movestheballtothenextposition,reversingdirectionifithitsoneoftheedges*/
8、publicvoidmove(Rectangle2Dbounds){x+=dx;y+=dy;if(xvbounds.getMinX()){x=boun
此文档下载收益归作者所有