package event;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Ellipse2D;
public class MouseEventTest extends Frame{
/**
*
*/
private static final long serialVersionUID = -2790306218418130626L;
public void paint(Graphics g)
{
super.paint(g);
final Graphics2D g2 = (Graphics2D)g;
BasicStroke pen = new BasicStroke(3.0f);
g2.setStroke(pen);
g2.setColor(Color.BLUE);
this.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e)
{
System.out.println(e.getButton());
Ellipse2D.Float circle = new Ellipse2D.Float(140,140,15,15);
g2.draw(circle);
}
});
}
MouseEventTest()
{
this.setSize(400, 300);
this.setVisible(true);
this.setLocation(300, 300);
this.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
// System.out.println(e.getButton()+"Click");
// paint(e.ge );
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
});
}
public static void main(String[] args)
{
new MouseEventTest();
}
}
代码如上:
因为对JAVA的event不是很了解,这里我是希望我点击一下,在界面上就会画一个圆圈。
结果程序无法顺利进行,问题出在哪儿呢。
还有这里为什么g2需要设置成final,在鼠标响应事件里的类型,这个就需要设置成final么?为什么呢。