大家好,
    刚学java,碰到个问题,但高手难觅,始终未解决,于是来发个贴求教!
    本人的原意是运行下面代码后能将鼠标随意点击button的任何位置后将其拖曳到frame里任意地方。目的是达到了,但也出现了新的问题,每次拖曳完button后鼠标都出现在button的最左上角,而不是鼠标当初点击button时所在button上的相对位置(鼠标已经不在button上以前那个位置)。请高手帮忙修改下,代码如下:
import java.awt.*;
import java.awt.event.*;
public class test extends MouseAdapter implements MouseMotionListener 
{      Frame f;
       int x;
       int y;
       Button b;
       Point or=new Point();
       Point p=new Point();    
      
   public static void main(String[] args) 
{   new test();

}
     
 public test()
{    

         f=new Frame("testdrr2");
          f.setLayout(null);
          b=new Button("one");
          b.setSize(60,60);
          b.setLocation(80,80);
  
  
        b.addMouseMotionListener(this);

    f.add(b);
        
         
   f.setSize(200,200);
   f.setVisible(true);
}
      public void mousePressed(MouseEvent e)
          {   or.x=e.getX();
               or.y=e.getY();
   }
    
   public  void  mouseDragged(MouseEvent e) {    p=b.getLocation();
  

b.setLocation(p.x+e.getX()-or.x,p.y+e.getY()-or.y);

}
}

解决方案 »

  1.   

    b.addMouseMotionListener(this); 
    的后面再加一行
    b.addMouseListener(this);
    就好了
      

  2.   

    强烈建议你使用swing组件,我用swing组件重新实现了你的功能,逻辑稍微改了一下下,呵呵import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.UIManager;public class Test extends MouseAdapter implements MouseMotionListener
    {
        private JFrame frame;
        
        private JButton button;    private int x;    private int y;    public static void main(String[] args) throws Exception
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            new Test();
        }    public Test()
        {
            frame = new JFrame("TestDrr2");
            frame.setLayout(null);
            button = new JButton("One");
            button.setBounds(80, 80, 60, 60);
            button.addMouseMotionListener(this);
            button.addMouseListener(this);
            frame.add(button);
            frame.setSize(300, 200);
            frame.setLocationRelativeTo(null);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }    public void mousePressed(MouseEvent e)
        {
            x = e.getX();
            y = e.getY();
        }    public void mouseDragged(MouseEvent e)
        {
            button.setLocation(button.getX() + e.getX() - x, button.getY() + e.getY() - y);
        }
    }
      

  3.   

    为什么实现MouseMotionListener 后不需要把MouseMotionListener 的所有方法都实现也可以编译运行呢?
      

  4.   


    那里面就两个方法呀,全实现了,自己看源码
    /*
     * @(#)MouseMotionListener.java 1.17 06/04/13
     *
     * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
     * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
     */package java.awt.event;import java.util.EventListener;/**
     * The listener interface for receiving mouse motion events on a component.
     * (For clicks and other mouse events, use the <code>MouseListener</code>.)
     * <P>
     * The class that is interested in processing a mouse motion event
     * either implements this interface (and all the methods it
     * contains) or extends the abstract <code>MouseMotionAdapter</code> class
     * (overriding only the methods of interest).
     * <P>
     * The listener object created from that class is then registered with a
     * component using the component's <code>addMouseMotionListener</code> 
     * method. A mouse motion event is generated when the mouse is moved
     * or dragged. (Many such events will be generated). When a mouse motion event
     * occurs, the relevant method in the listener object is invoked, and 
     * the <code>MouseEvent</code> is passed to it.
     *
     * @author Amy Fowler
     * @version 1.17, 04/13/06
     *
     * @see MouseMotionAdapter
     * @see MouseEvent
     * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/mousemotionlistener.html">Tutorial: Writing a Mouse Motion Listener</a>
     *
     * @since 1.1
     */
    public interface MouseMotionListener extends EventListener {    /**
         * Invoked when a mouse button is pressed on a component and then 
         * dragged.  <code>MOUSE_DRAGGED</code> events will continue to be 
         * delivered to the component where the drag originated until the 
         * mouse button is released (regardless of whether the mouse position 
         * is within the bounds of the component).
         * <p> 
         * Due to platform-dependent Drag&Drop implementations, 
         * <code>MOUSE_DRAGGED</code> events may not be delivered during a native 
         * Drag&Drop operation.  
         */
        public void mouseDragged(MouseEvent e);    /**
         * Invoked when the mouse cursor has been moved onto a component
         * but no buttons have been pushed.
         */
        public void mouseMoved(MouseEvent e);}