已阅读5页,还剩8页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
云南大学软件学院实 验 报 告姓名: 王增伟 学号: 20111120083 班级: 软件工程 日期: 2014.11.18 成绩: JAVA实验九一、 实验目的:熟悉Java中的图形化编程以及事件监听器的使用。二、实验要求:Program 1Create an application ChangeBallColor that displays a panel. This panel will contain two buttons (one for red and one for blue). It will also display a filled in circle. When the user clicks on a button, the circle should change to that color. So, if a user clicks the blue button, the circle will change to blue. Similarly, if a user clicks the red button, the circle will turn to red.Program 2Create an application AddCircles that displays a panel. This panel will contain two buttons (one for red and one for blue). When the user clicks on a button, change a variable called currentColor to that particular color.Now when the user clicks on a space in the panel (outside a button), draw a circle with the current color. Have your application just add new circles when the user clicks the mouse. Do not erase previous circles.3、 实验内容:Program 1:Balls.java代码部分package Lab9;class Balls /定义圆的颜色type,1表示蓝色,2表示红色int color=0;/定义圆的位置int locate=50;public int getColor() return color;public void setColor(int color) this.color = color;public int getLocate() return locate;public void setLocate(int locate) this.locate = locate;/构造函数public Balls(int color, int locate)this.color=color;this.locate=locate;ChangeBallColor.java代码部分package Lab9;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class ChangeBallColor extends JFrame /定义组件JPanel jp;JButton jb1,jb2;Panel mp=null;public static void main(String args) / TODO Auto-generated method stubChangeBallColor main=new ChangeBallColor();/构造函数public ChangeBallColor()mp=new Panel();jp=new JPanel();jb1=new JButton(蓝色);jb2=new JButton(红色);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);/定义我的面板class Panel extends JPanel implements ActionListenerBalls ball=null;/构造函数public Panel()/初始化圆ball=new Balls(0,150);public void paint(Graphics g)super.paint(g);/设置背景颜色g.setColor(Color.CYAN);g.fillRect(0, 0, 500, 500);/画出不同颜色的固定圆this.DrawBall(g, this.ball.getColor(), this.ball.getLocate();/定义一个画圆的函数public void DrawBall(Graphics g,int color, int locate)switch(color)case 0:g.setColor(Color.WHITE);break;case 1:g.setColor(Color.BLUE);break;case 2:g.setColor(Color.RED);break;g.fillOval(locate, locate, 150, 150);/对事件的处理函数public void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif(e.getActionCommand().equals(蓝色)this.ball.setColor(1);this.ball.setLocate(150);else if(e.getActionCommand().equals(红色)this.ball.setColor(2);this.ball.setLocate(150);/重绘界面this.repaint();运行结果显示:初始颜色为白色:点击蓝色按钮后变为蓝色:点击红色按钮后变为红色:Program 2:Balls.java代码部分package Lab9;class Balls /定义圆的颜色type,1表示蓝色,2表示红色int color=0;/定义圆的位置int locate=50;public int getColor() return color;public void setColor(int color) this.color = color;public int getLocate() return locate;public void setLocate(int locate) this.locate = locate;/构造函数public Balls(int color, int locate)this.color=color;this.locate=locate;/增加圆的类class AddBalls extends Ballspublic AddBalls(int color, int locate)super(color,locate);AddCircles.java代码部分package Lab9;import java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;public class AddCircles extends JFrame /定义组件JPanel jp;JButton jb1,jb2,jb3;MyPanel mp=null;public static void main(String args) / TODO Auto-generated method stubAddCircles main=new AddCircles();/构造函数public AddCircles()mp=new MyPanel();jp=new JPanel();jb1=new JButton(蓝色);jb2=new JButton(红色);jb3=new JButton(增加一个同颜色的圆);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);/定义我的面板class MyPanel extends JPanel implements ActionListenerBalls ball=null;/创建一个集合,用于保存增加的圆Vector abList=new Vector();int i=150;/表示增加圆的位置,初始为150/构造函数public MyPanel()ball=new Balls(0,100);public void paint(Graphics g)super.paint(g);/设置背景颜色g.setColor(Color.CYAN);g.fillRect(0, 0, 500, 500);/画出不同颜色的固定圆this.DrawBall(g, this.ball.getColor(), this.ball.getLocate();for(int i=0; iabList.size();i+)AddBalls ab=this.abList.get(i);this.DrawBall(g, ab.getColor(), ab.getLocate();/定义一个画圆的函数public void DrawBall(Graphics g,int color, int locate)switch(color)case 0:g.setColor(Color.WHITE);break;case 1:g.setColor(Color.BLUE);break;case 2:g.setColor(Color.RED);break;g.fillOval(locate, locate, 50, 50);/对事件的处理函数public void actionPerformed(ActionEvent e) / TODO Auto-generated method stubif(e.getActionCommand().equals(蓝色)this.ball.setColor(1);this.ball.setLocate(100);else if(e.getActionCommand().equals(红色)this.ball.setColor(2);this.ball.setLocate(100);else if(e.getActionCommand().equals(加圆)i+=20;/创建增加圆的对象AddBalls ab=new AddBalls(this.ball.getColor(),i);/将增加的圆加入到集合中abList.add(ab);/重绘界面this.repaint();运行结果显示:初始显示界面:当点击“蓝色”按钮后初始圆变为蓝色:当点击“增加一个同颜色的圆”按钮后的界面:再点击“红色”按钮后初始圆变为红色:在点击“增加一个同颜色的圆”后的界面:不断改变初始圆的颜色,不断增加多个圆的界面
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 紧急设备备件催货函(5篇)范文
- 网络信息安全专家在大型企业绩效指标表
- 工业工程师机械制造生产流程优化绩效指标表
- 软件研发项目经理项目管理与团队协作效能绩效考评表
- 税务申报材料确认函2026(4篇)
- 通知召开季度业务会议会议时间地点参会部门6篇范文
- 设备维护合同续签协商函(4篇范文)
- 税务处理存在问题汇报通知函(4篇范文)
- 文化艺术场馆战略规划效能评估表
- 2026年企业福利采购指南报告:工会视角下的采购策略
- 2025年甘肃(专升本)英语考试题库(含答案)
- DB45-T 2875-2024 矿山生态修复工程技术规范
- AI赋能小学语文整本书阅读实施方案
- 加气站变更管理制度(3篇)
- 《CATIA-V5R21基础与应用案例教程》(共9章)第七章钣金设计
- 手术部位感染的预防及控制措施
- 油漆店面应急预案
- 2025年四川省内江市专业技术继续教育公需科目考试及答案
- 工程合同外增加项补充协议模板
- 风电设备典型故障诊断处理指导手册第3册2025
- 2025年陕西陕煤集团榆林化学有限责任公司招聘笔试参考题库含答案解析
评论
0/150
提交评论