关于鼠标事件的问题
代码如下:import java.awt.*;
import java.awt.event.*;
public class Mouse extends Frame
{
static int x,y,ox,oy,offx,offy,px,py;
static Mouse frm=new Mouse();
static Button bu=new Button("拖动我");
static TextField tf=new TextField(20);
public static void main(String args[])
{
frm.setLayout(null);
frm.setTitle("鼠标事件的处理");
frm.setBounds(100,100,400,300);
bu.setBounds(180,140,60,20);
tf.setBounds(50,200,260,50);
tf.setEditable(false);
frm.add(bu);
frm.add(tf);
frm.setVisible(true);
bu.addMouseListener(new myMouse());
bu.addMouseMotionListener(new myMouseM());
}
static class myMouse extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
px=e.getX();
py=e.getY();
ox=bu.getLocation().x;
oy=bu.getLocation().y;
}
}
static class myMouseM extends MouseMotionAdapter
{
public void mouseDragged(MouseEvent e)
{
offx=px-ox;
offy=py-oy;
x=e.getX()-offx;
y=e.getY()-offy;
tf.setText("命令按钮放置在("+x+","+y+")的位置");
bu.setLocation(x, y);
ox=x;// 如果不加这两句,拖动时会晃动,为什么?
oy=y;
}
}
}
运行后正常,但是觉得上述ox=x;oy=y;这两句多余,但是去掉后拖动时却晃动为什么?这两句有什么作用呢?