欢迎来到天天文库
浏览记录
ID:38734798
大小:250.50 KB
页数:13页
时间:2019-06-18
《云南大学软件学院JAVA实验九》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、云南大学软件学院实验报告姓名:王增伟学号:20111120083班级:软件工程日期:2014.11.18成绩:JAVA实验九一、实验目的:熟悉Java中的图形化编程以及事件监听器的使用。二、实验要求: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.三、实验内容:Program1:Balls.java代码部分packageLab9;classBalls{//定义圆的颜色type,1表示蓝色,2表示红色intco
4、lor=0;//定义圆的位置intlocate=50;publicintgetColor(){returncolor;}publicvoidsetColor(intcolor){this.color=color;}publicintgetLocate(){returnlocate;}publicvoidsetLocate(intlocate){this.locate=locate;}//构造函数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();}//构造函数publicChangeBallColor(){mp=
6、newPanel();jp=newJPanel();jb1=newJButton("蓝色");jb2=newJButton("红色");jp.add(jb1);jp.add(jb2);//注册监听jb1.addActionListener(mp);jb1.setActionCommand("蓝色");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;//构造函数publicPanel(){//初始化圆ball=newBalls(0,150);}publi
此文档下载收益归作者所有