Moving the Panel

Hi all…

Can someone tell me why how come when I move the panel it does not move with the cursor?
The sample code is below


import javax.swing.*;

public class testDrag extends JFrame {

	private JPanel jContentPane = null;
	private JLayeredPane lp;

	private JPanel movePanel = null;
	public static void main(String []args){
	    new testDrag();
	}


	public testDrag() {
		super();
		initialize();
	}

	private void initialize() {
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setSize(1000,1000);
		this.setContentPane(getJContentPane());
		this.setTitle("Test Drag");
		this.setVisible(true);
		lp = getLayeredPane();
		lp.add(getMovePanel(),JLayeredPane.MODAL_LAYER);
	}

	private javax.swing.JPanel getJContentPane() {
		if(jContentPane == null) {
			jContentPane = new javax.swing.JPanel();
			jContentPane.setLayout(new java.awt.BorderLayout());
		}
		return jContentPane;
	}
  
	private JPanel getMovePanel() {
		if (movePanel == null) {
			movePanel = new JPanel();
			movePanel.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.red,5));
			movePanel.setSize(100, 100);
			movePanel.setLocation(10, 10);
			movePanel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() { 
				public void mouseDragged(java.awt.event.MouseEvent e) {    
				    //int pX = getX();
				    //int pY = getY();
				    //int newX = e.getPoint().x;
				    //int newY = e.getPoint().y;
				    //int diffX = newX - pX;
				    //int diffY = newY - pY;
				    movePanel.setLocation(e.getPoint().x,e.getPoint().y);
				    e.consume();
				}
			});
		}
		return movePanel;
	}
 }


The panel always moves around else where… so I really dont understand why…

Can someone one explain why? ??? ???

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MButton extends JFrame{
	JButton b=new JButton("bar");
	int x=20,y=20,xo=0,yo=0;
	public MButton(){
		JPanel p=new JPanel();
		p.setLayout(null);
		b.setBounds(x,y,60,20);
		p.add(b);
		getContentPane().add(p);
		setSize(500,500);
		setVisible(true);
		MBInputAdapter m=new MBInputAdapter();
		b.addMouseMotionListener(m);
		b.addMouseListener(m);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}
	public static void main(String[]args){
		new MButton();
	}
	class MBInputAdapter extends MouseInputAdapter{
		public void mouseDragged(MouseEvent e){
			b.setLocation(x+=e.getX()-xo,y+=e.getY()-yo);
		}
		public void mousePressed(MouseEvent e){
			xo=e.getX();
			yo=e.getY();
		}
	}
}

Thanks!! ;D

Needed sometime to understand the code
lolz

Cheers!