资源简介
1.编程实现以下功能:
界面如下图所示;
当点击不同的按钮时,圆的填充颜色会随之改变;
用鼠标点击窗体时,圆的 填充颜色会自动依照”面板背景色-红色-绿色-蓝色”循环改变;
鼠标移到圆内时,光标变成十字形;
代码片段和文件信息
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
public class CircleTest {
public static void main(String[]args){ //设置可见窗体
Buttonframe f = new Buttonframe();
f.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
f.setVisible(true);
}
}
class Buttonframe extends Jframe{
public Buttonframe(){ //面板加入窗体当中
this.settitle(“Button Window“);
this.setSize(DEFAULT_WIDTH DEFAULT_HEIGHT);
this.add(new ButtonPanel());
}
public static final int DEFAULT_WIDTH = 400;
public static final int DEFAULT_HEIGHT = 300;
}
class ButtonPanel extends JPanel{
private Ellipse2D yuan;
private Color ecolor=getBackground();
private Color[] colors={getBackground()Color.yellowColor.redColor.green};
public void init(){
new Thread().start();
}
public ButtonPanel(){
//用标签字符串构造按钮;
JButton b1 = new JButton(“Yellow“);
JButton b2 = new JButton(“Red“);
JButton b3 = new JButton(“Green“);
this.add(b1);//将按钮添加到面板上
this.add(b2);
this.add(b3);
//用适当颜色构造监听器对象
ColorAction a1 = new ColorAction(Color.YELLOW);
ColorAction a2 = new ColorAction(Color.RED);
ColorAction a3 = new ColorAction(Color.GREEN);
MouseHandler x = new MouseHandler();
MouseMotionHandler moh = new MouseMotionHandler();
/*eventSourceobject.addEventListener(eventListenerobject);
//把对象注册到事件源上*/
b1.addActionListener(a1);//把action与Button连接上 将监听器对象注册到组件
b2.addActionListener(a2);
b3.addActionListener(a3);
this.addMouseMotionListener(moh);//监听鼠标移动事件
this.addMouseListener(x);
}
public void paintComponent(Graphics g)
评论
共有 条评论