按下按钮圆圈改变颜色
2015-12-15 23:55
204 查看
import sun.jvm.hotspot.types.JBooleanField; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TestDemo implements ActionListener { JFrame frame; public static void main(String args[]){ TestDemo gui = new TestDemo(); gui.go(); } public void go(){ frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton button = new JButton("Change colors"); button.addActionListener(this); MyDrawPane drawPane = new MyDrawPane(); frame.getContentPane().add(BorderLayout.SOUTH,button); frame.getContentPane().add(BorderLayout.CENTER,drawPane); frame.setSize(300,300); frame.setVisible(true); } public void actionPerformed(ActionEvent event){ frame.repaint();//当按下按钮时要求frame重新绘制 } } class MyDrawPane extends JPanel{ public void paintComponent(Graphics g){ Graphics2D g2d = (Graphics2D) g; int red = (int)(Math.random()*255); int green = (int)(Math.random()*255); int blue = (int)(Math.random()*255); Color startColor = new Color(red ,green,blue); red = (int)(Math.random()*255); green = (int)(Math.random()*255); blue = (int)(Math.random()*255); Color endColor = new Color(red , green, blue ); GradientPaint gradientPaint = new GradientPaint(70,70,startColor,150,150,endColor); g2d.setPaint(gradientPaint); g2d.fillOval(70,70,100,100);//用目前的笔刷设定来填满椭圆型区域 } }
相关文章推荐
- button作为asp.net服务器控件时在IE提交两次
- 关于Hello Minecraft! Launcher源代码的简介
- Unity3D中的系统调用方法
- 框架SSH学习(Struts)
- JavaDoc的使用
- 斯坦福机器学习2:监督学习应用
- http协议笔记
- vb常用函数一览表
- 打怪路线专用
- ABP理论学习之发布说明
- 通过表达式、函数给React组件属性赋值
- UVa 11082 - 最大流 基础建模
- android的interpolator用法
- php中instanceof的作用
- Hexo 主题使用进阶
- 【菜鸟初长成】之人生第一个JAVA程序
- SpringMVC接收参数的注解笔记
- jquery学习(五)-jquery中的事件
- [How to]使用自定义cell进行tableview的创建,适用于cell样式不发生变化的情况。
- storm 入门原理介绍_AboutYUN