2 u013451048 u013451048 于 2014.11.01 11:15 提问

java mousePressed事件无响应不知道什么问题

import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class HE extends JFrame
{
public HE()
{
showJPanel panel =new showJPanel();
//setLayout(new BordeLaout());
add(panel);
}
public static void main(String[] args)
{
HE frame = new HE();
frame.setTitle("showJFrame");
frame.setSize(310,410);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
//面板类
class showJPanel extends JPanel
{

private int currentX = 1 ,currentY = 1,rX = 10,rY = 10;
private int cr  = 255,cg = 0,cb = 0;
int count = 0;
public showJPanel()
{

    addMouseMotionListener(new MouseAdapter()
    {
        public void mousePressed(MouseEvent e)
        {
            currentX = e.getX();
            currentY = e.getY();

            double L = (currentX-rX)*(currentX-rX)+(currentY-rY)*(currentY-rY);

            if(Math.sqrt(L)<10)
            {
                rX = (int)(Math.random()*300)+10;   //随机显示圆
                rY = (int)(Math.random()*400)+10;


                cr = (int)(Math.random()*255);  //随机确定颜色
                cg = (int)(Math.random()*255);
                cb = (int)(Math.random()*255);
                repaint();
            }   
            L = 0;
        }
    });
}
//覆盖父类方法
protected void paintComponent(Graphics g)
{
    super.paintComponent(g);


    g.drawOval(rX,rY ,10,10);           


    Color c = new Color(cr,cg,cb);      

    g.setColor(c);
    g.fillOval(rX,rY,10,10);
}   

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!