鼠标拖动一个button按钮,是怎么通过改变坐标是鼠标拖动button按钮,代码如下:求解释~
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class java_1
{
public static void main(String args[]){
new Wins();
}
}
class Wins extends MouseAdapter implements MouseListener
{
JButton b1;
JFrame f;
int a,b,x0,y0,x,y;
Wins(){
f=new JFrame();
b1=new JButton("鼠标拖动我");
b1.addMouseListener(this);
b1.addMouseMotionListener(this);
f.setLayout(new FlowLayout());
f.add(b1);
JComponent lh=(JComponent)b1; int c=lh.getBounds().x;
int d=lh.getBounds().y;
f.setVisible(true);
f.setBounds(120,120,300,300);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
public void mousePressed(MouseEvent e){
JComponent com=null;
com=(JComponent)e.getSource();
a=(com).getBounds().x;
b=(com).getBounds().y;
x0=e.getX();  //鼠标点击在按钮中位置
y0=e.getY();
}
public void mouseDragged(MouseEvent e){
JComponent com=null;
if(e.getSource() instanceof JComponent){
com=(JComponent)e.getSource();
a=com.getBounds().x;
b=com.getBounds().y;
x=e.getX();
y=e.getY();
a=a+x;
b=b+y;
com.setLocation(a-x0,b-y0); }
}
public void mouseMoved(MouseEvent e){}
}

解决方案 »

  1.   

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class java_1
    {
    public static void main(String args[]){
    new Wins();
    }
    }
    class Wins extends MouseAdapter implements MouseListener
    {
    JButton b1;
    JFrame f;
    int a,b,x0,y0,x,y;
    Wins(){
    f=new JFrame();
    b1=new JButton("鼠标拖动我");
    b1.addMouseListener(this);
    b1.addMouseMotionListener(this);
    f.setLayout(new FlowLayout());
    f.add(b1);
    JComponent lh=(JComponent)b1;int c=lh.getBounds().x;
    int d=lh.getBounds().y;
    f.setVisible(true);
    f.setBounds(120,120,300,300);
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }
    public void mousePressed(MouseEvent e){
    JComponent com=null;
    com=(JComponent)e.getSource();//被鼠标点击的组件
    System.out.println("Pressed");
    a=(com).getBounds().x;//该组件左上角的x值
    System.out.print("a="+a+",");
    b=(com).getBounds().y;//该组件左上角的y值
    System.out.print("b="+b+",");
    x0=e.getX(); //鼠标点击在按钮中位置,若鼠标点在该组件左上角,x0=0
    System.out.print("x0="+x0);
    y0=e.getY();
    System.out.println("y0="+y0);
    }
    public void mouseDragged(MouseEvent e){
    JComponent com=null;
    if(e.getSource() instanceof JComponent){
    com=(JComponent)e.getSource();
    System.out.println("Dragged");
    a=com.getBounds().x;
    System.out.print("a="+a+",");
    b=com.getBounds().y;
    System.out.print("b="+b+",");
    x=e.getX();//新的鼠标在按钮中的位置x值,若鼠标在按钮左上方,则为0
    System.out.print("x="+x+",");
    y=e.getY();//新的鼠标点击在按钮中的位置y值
    System.out.println("y="+y);
    a=a+x;//组件位置x值+新的鼠标在按钮中的位置x值
    b=b+y;//组件位置y值+新的鼠标在按钮中的位置y值
    com.setLocation(a-x0,b-y0);//把鼠标的内部移动变为组件的外在移动}
    }
    public void mouseMoved(MouseEvent e){}
    }以上是我修改的代码,可以运行一下,这样将有助于理解。
    鼠标拖曳时保持内部相对静止的机理:
    1、鼠标点击,确定x0,y0,即鼠标在Button中的相对位置
    2、拖曳时,获得新的鼠标相对位置
    3、Button位置重置(新的位置=旧的位置+(新鼠标在Button中相对位置-旧鼠标在Button中相对位置))
       这样,虽然实际上是鼠标在移动,但是Button的移动就和鼠标的移动保持一致了。我运行并拖曳的显示如下:
    Pressed
    a=96,b=5,x0=7y0=23
    Dragged
    a=96,b=5,x=7,y=24
    Dragged
    a=96,b=6,x=7,y=24
    Dragged
    a=96,b=7,x=7,y=24
    Dragged
    a=96,b=8,x=7,y=24
    Dragged
    a=96,b=9,x=7,y=24
    Dragged
    a=96,b=10,x=7,y=24
    Dragged
    a=96,b=11,x=7,y=24
    Dragged
    a=96,b=12,x=7,y=24
    Dragged
    a=96,b=13,x=7,y=24
    Dragged
    a=96,b=14,x=7,y=24
    Dragged
    a=96,b=15,x=7,y=24
    Dragged
    a=96,b=16,x=7,y=22
    Dragged
    a=96,b=15,x=7,y=22可以看出,每次值的变化都非常小,因为Frame有实时监测事件发生的线程在后台运行,一有拖曳动作,就会触发拖曳方法。
      

  2.   

    x0=e.getX(); //鼠标点击在按钮中位置
    y0=e.getY();x0,y0记录的是鼠标相对于按钮左上角的距离其中
    a=com.getBounds().x;
    b=com.getBounds().y;
    x=e.getX();
    y=e.getY();
    a=a+x;
    b=b+y;
    com.setLocation(a-x0,b-y0);换成
    a=com.getBounds().x;
    b=com.getBounds().y;
    x=e.getX();
    y=e.getY();
    com.setLocation( (a+x)-x0,(b+y)-y0);
    在移动的事件中,getX(),getY()是相对于按钮左上角的偏移量如果说直接是
    com.setLocation(a+x,b+y);
    就是原始的左上角加上偏移量,那么实际就是鼠标的位置吧?移到新位置时,按钮的左上角移到了鼠标的位置但是鼠标的位置与左上角是有距离,距离是什么呢?很明显就是x0,y0.第一点击的时候,就是记录了鼠标的位置与按钮的左上角的距离.
    所以是:(a+x)-x0,(b+y)-y0