您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 资本运营 > 云南大学软件学院JAVA实验九
云南大学软件学院实验报告姓名:王增伟学号:20111120083班级:软件工程日期:2014.11.18成绩:JAVA实验九一、实验目的:熟悉Java中的图形化编程以及事件监听器的使用。二、实验要求:Program1CreateanapplicationChangeBallColorthatdisplaysapanel.Thispanelwillcontaintwobuttons(oneforredandoneforblue).Itwillalsodisplayafilledincircle.Whentheuserclicksonabutton,thecircleshouldchangetothatcolor.So,ifauserclicksthebluebutton,thecirclewillchangetoblue.Similarly,ifauserclickstheredbutton,thecirclewillturntored.Program2CreateanapplicationAddCirclesthatdisplaysapanel.Thispanelwillcontaintwobuttons(oneforredandoneforblue).Whentheuserclicksonabutton,changeavariablecalledcurrentColortothatparticularcolor.Nowwhentheuserclicksonaspaceinthepanel(outsideabutton),drawacirclewiththecurrentcolor.Haveyourapplicationjustaddnewcircleswhentheuserclicksthemouse.Donoterasepreviouscircles.三、实验内容:Program1:Balls.java代码部分packageLab9;classBalls{//定义圆的颜色type,1表示蓝色,2表示红色intcolor=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代码部分packageLab9;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassChangeBallColorextendsJFrame{//定义组件JPaneljp;JButtonjb1,jb2;Panelmp=null;publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubChangeBallColormain=newChangeBallColor();}//构造函数publicChangeBallColor(){mp=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(500,500);this.setLocation(300,150);this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}//定义我的面板classPanelextendsJPanelimplementsActionListener{Ballsball=null;//构造函数publicPanel(){//初始化圆ball=newBalls(0,150);}publicvoidpaint(Graphicsg){super.paint(g);//设置背景颜色g.setColor(Color.CYAN);g.fillRect(0,0,500,500);//画出不同颜色的固定圆this.DrawBall(g,this.ball.getColor(),this.ball.getLocate());}//定义一个画圆的函数publicvoidDrawBall(Graphicsg,intcolor,intlocate){switch(color){case0:g.setColor(Color.WHITE);break;case1:g.setColor(Color.BLUE);break;case2:g.setColor(Color.RED);break;}g.fillOval(locate,locate,150,150);}//对事件的处理函数publicvoidactionPerformed(ActionEvente){//TODOAuto-generatedmethodstubif(e.getActionCommand().equals(蓝色)){this.ball.setColor(1);this.ball.setLocate(150);}elseif(e.getActionCommand().equals(红色)){this.ball.setColor(2);this.ball.setLocate(150);}//重绘界面this.repaint();}}运行结果显示:初始颜色为白色:点击蓝色按钮后变为蓝色:点击红色按钮后变为红色:Program2:Balls.java代码部分packageLab9;classBalls{//定义圆的颜色type,1表示蓝色,2表示红色intcolor=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;}}//增加圆的类classAddBallsextendsBalls{publicAddBalls(intcolor,intlocate){super(color,locate);}}AddCircles.java代码部分packageLab9;importjava.awt.*;importjava.awt.event.*;importjava.util.*;importjavax.swing.*;publicclassAddCirclesextendsJFrame{//定义组件JPaneljp;JButtonjb1,jb2,jb3;MyPanelmp=null;publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubAddCirclesmain=newAddCircles();}//构造函数publicAddCircles(){mp=newMyPanel();jp=newJPanel();jb1=newJButton(蓝色);jb2=newJButton(红色);jb3=newJButton(增加一个同颜色的圆);jp.add(jb1);jp.add(jb2);jp.add(jb3);//注册监听jb1.addActionListener(mp);jb1.setActionCommand(蓝色);jb2.addActionListener(mp);jb2.setActionCommand(红色);jb3.addActionListener(mp);jb3.setActionCommand(加圆);this.add(jp,BorderLayout.NORTH);this.add(mp);this.setTitle(ChangeBallColor);this.setSize(500,500);this.setLocation(300,150);this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}//定义我的面板classMyPanelextendsJPanelimplementsActionListener{Ballsball=null;//创建一个集合,用于保存增加的圆VectorAddBallsabList=newVectorAddBalls();inti=150;//表示增加圆的位置,初始为150//构造函数publicMyPanel(){ball=newBalls(0,100);}publicvoidpaint(Graphicsg){super.paint(g);//设置背景颜色g.setColor(Color.CYAN);g.fillRect(0,0,500,500);//画出不同颜色的固定圆this.DrawBall(g,this.ball.getColor(),this.ball.getLocate());for(inti=0;iabList.size();i++){AddBallsab=this.abList.get(i);this.DrawBall(g,ab.getColor(),ab.getLocate());}}//定义一个画圆的函数publicvoidDrawBall(Graphicsg,intcolor,intlocate){switch(color){case0:g.setColor(Color.WHITE);break;case1:g.setColor(Color.BLUE);break;case2:g.setColor(Color.RED);break;}g.fillOval(locate,locate,50,50);}//对事件的处理函数publicvoidactionPerformed(ActionEvente){//TODOAuto-generatedmethodstubif(e.getActionCommand().equals(蓝色)){this.ball.setColor(1);this.ball.setLocate(100);}elseif(e.getActionCommand().equals(红色)){this.ball.setColor(2);this.ball.setLocate(100);}elseif(e.getActionCommand().equals(加圆)){i+=20;//创建增加圆的对象AddBallsab=newAddBalls(this.ball.getColor(),i);//将增加的圆加入到集合中abList.add(ab);}//重绘界
本文标题:云南大学软件学院JAVA实验九
链接地址:https://www.777doc.com/doc-5688703 .html