資源描述:
《云南大學(xué)軟件學(xué)院JAVA實(shí)驗(yàn)九》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、云南大學(xué)軟件學(xué)院實(shí)驗(yàn)報(bào)告姓名:王增偉學(xué)號(hào):20111120083班級(jí):軟件工程日期:2014.11.18成績(jī):JAVA實(shí)驗(yàn)九一、實(shí)驗(yàn)?zāi)康模菏煜ava中的圖形化編程以及事件監(jiān)聽器的使用。二、實(shí)驗(yàn)要求:Program1CreateanapplicationChangeBallColorthatdisplaysapanel.?Thispanelwillcontaintwobuttons(oneforredandoneforblue).?Itwillalsodisplayafilledincircle.?Whentheuserclicksonabutton,thecircleshould
2、changetothatcolor.?So,ifauserclicksthebluebutton,thecirclewillchangetoblue.?Similarly,ifauserclickstheredbutton,thecirclewillturntored.Program2CreateanapplicationAddCirclesthatdisplaysapanel.?Thispanelwillcontaintwobuttons(oneforredandoneforblue).?Whentheuserclicksonabutton,changeavariablecall
3、edcurrentColortothatparticularcolor.Nowwhentheuserclicksonaspaceinthepanel(outsideabutton),drawacirclewiththecurrentcolor.?Haveyourapplicationjustaddnewcircleswhentheuserclicksthemouse.?Donoterasepreviouscircles.三、實(shí)驗(yàn)內(nèi)容:Program1:Balls.java代碼部分packageLab9;classBalls{//定義圓的顏色type,1表示藍(lán)色,2表示紅色intco
4、lor=0;//定義圓的位置intlocate=50;publicintgetColor(){returncolor;}publicvoidsetColor(intcolor){this.color=color;}publicintgetLocate(){returnlocate;}publicvoidsetLocate(intlocate){this.locate=locate;}//構(gòu)造函數(shù)publicBalls(intcolor,intlocate){this.color=color;this.locate=locate;}}ChangeBallColor.java代碼部分p
5、ackageLab9;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassChangeBallColorextendsJFrame{//定義組件JPaneljp;JButtonjb1,jb2;Panelmp=null;publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubChangeBallColormain=newChangeBallColor();}//構(gòu)造函數(shù)publicChangeBallColor(){mp=
6、newPanel();jp=newJPanel();jb1=newJButton("藍(lán)色");jb2=newJButton("紅色");jp.add(jb1);jp.add(jb2);//注冊(cè)監(jiān)聽jb1.addActionListener(mp);jb1.setActionCommand("藍(lán)色");jb2.addActionListener(mp);jb2.setActionCommand("紅色");this.add(jp,BorderLayout.NORTH);this.add(mp);this.setTitle("ChangeBallColor");this.setSize
7、(500,500);this.setLocation(300,150);this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}//定義我的面板classPanelextendsJPanelimplementsActionListener{Ballsball=null;//構(gòu)造函數(shù)publicPanel(){//初始化圓ball=newBalls(0,150);}publi