java鼠标事件

packagetestmouse;importjava.awt.*;importjava.awt.event.*;importjava.util.*;importjava... package testmouse;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*;
import javax.swing.*;

public class mouse {
public static void main(String[] args)
{
mouseframe frame=new mouseframe();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class mouseframe extends JFrame
{
public mouseframe()
{
setTitle("mousetese");
setSize(300,200);
mousecom component=new mousecom();
add(component);
}
}
class mousecom extends JComponent
{
public mousecom()
{
squares=new ArrayList<Rectangle2D>();
current=null;
addMouseListener(new mousehandler());
addMouseMotionListener(new mousemotionhandler());
}
public void paintComponent(Graphics g)
{
Graphics2D g1=(Graphics2D) g;
for (Rectangle2D r:squares)
g1.draw(r);
System.out.println("paint");
}
public Rectangle2D find(Point2D p)
{
for (Rectangle2D r:squares)
{
if(r.contains(p)) return r;
}
return null;
}
public void add(Point2D p)
{
double x=p.getX();
double y=p.getY();
current=new Rectangle2D.Double(x-a/2,y-a/2,a,a);
squares.add(current);
repaint();
System.out.println("add");
}
public void remove(Rectangle2D s)
{
if(s==null) return;
if(s==current) current=null;
squares.remove(s);
repaint();
System.out.println("remove");
}
private int a=10;
ArrayList<Rectangle2D> squares;
private Rectangle2D current;
private class mousehandler extends MouseAdapter
{
public void mousePressed(MouseEvent event)
{
current=find(event.getPoint());
if(current==null) add(event.getPoint());
System.out.println("press");
}
public void mouseClicked(MouseEvent event)
{
current=find(event.getPoint());
if(current!=null&&event.getClickCount()>=2) remove(current);
System.out.println("click");
}
}
private class mousemotionhandler implements MouseMotionListener
{
public void mouseMoved(MouseEvent event)
{
if(find(event.getPoint())==null) setCursor(Cursor.getDefaultCursor());
else setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
//System.out.println("move");
}
public void mouseDragged(MouseEvent event)
{
System.out.println("undragged");
if(current==null)
{
int x=event.getX();
int y=event.getY();
current.setFrame(x-a/2, y-a/2, a, a);
repaint();
System.out.println("dragged");

}
}
}

}

请问这个程序如何操作才能实现打印出dragged,即让mouseDragged中的
if(current==null)
{
int x=event.getX();
int y=event.getY();
current.setFrame(x-a/2, y-a/2, a, a);
repaint();
System.out.println("dragged");

}运行?
能回答出来的话追加分数
展开
 我来答
czj880123
2012-07-22 · TA获得超过592个赞
知道小有建树答主
回答量:190
采纳率:0%
帮助的人:200万
展开全部
你的判断有问题。

if(current==null)
{
int x=event.getX();
int y=event.getY();
current.setFrame(x-a/2, y-a/2, a, a);
repaint();
System.out.println("dragged");

}
改成:
if(current!=null)
{
int x=event.getX();
int y=event.getY();
current.setFrame(x-a/2, y-a/2, a, a);
repaint();
System.out.println("dragged");

}
然后用鼠标抓取窗口就可以打印dragged。

理由:在mouseframe的构造器中,调用了add(),而add()方法执行过了那么current就不可能为空了。所以你的if(current==null)始终为false,故一直走不到判断里面的语句。就算current真的为null,那么判断中的current.setFrame(x-a/2, y-a/2, a, a)也会报空指针异常了。

希望能帮到你。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式