日期:2014-05-20  浏览次数:20827 次

关于监听器的问题
import java.awt.*;
import java.awt.event.*;

public class Test{
  Frame frame = new Frame("内部类测试!");
  TextField tf = new TextField();

  Test(){
  frame.add(new Label("点击该框架。"),"North");
  frame.add(tf,"South");
  frame.addMouseMotionListener(new MouseMotionAdapter(){
  public void mouseDragged(MouseEvent e){
  String s = "该点坐标为( " + e.getX() +" . " + e.getY() + " )";
tf.setText(s);
  }
  });
frame.setSize(300,300);
frame.setVisible(true);
  }
  public static void main(String[] args){
  Test a = new Test();
  }
}

为什么我将上面的 frame.addMouseMotionListener(new MouseMotionAdapter(){
换成 frame.addMouseListener(new MouseAdapter(){生成的图形就无法获取鼠标的坐标,我看API文档里面
MouseMotionAdapter和MouseAdapter都继承了MouseMotionListener这个接口,都有 public void mouseDragged(MouseEvent e)这个方法,为什么就有一个是错误的啊。谢谢!

------解决方案--------------------
drag...拖动的意思 不是点的意思
按住鼠标拖下
------解决方案--------------------
可能是因为系统认为addMouseListener中添加的是MouseListener类,而MouseListener类中并没有mouseDragged这个方法,所以即使你拖拽鼠标它也不会调用该方法
------解决方案--------------------
好像是不支持mouseDragged,mousePressed就正常了
------解决方案--------------------
经过测试没有问题,都是拖动显示坐标的,我的是JDK1.6可能你的JDK太老了!