title: How to add a self-defined MouseMotionListener to a JPanel?
body: <p>I wrote a <code>MouseMotionListener</code> for <code>JPanel</code>, but I get confused on how to use it.</p>
  <p>The listener class works well when added to the frame using <code>aWindow.add(new MouseMotionEvents())</code>, but when I try to add it to the panel using <code>content.add(new MouseMotionEvents())</code>, it doesn't work. I am new to Swing and <code>JComponent</code>s, could someone give me some hints?</p>
  <pre class="lang-java prettyprint-override"><code>public class test {
  
    public static void main(String[] args) {
      JFrame aWindow = new JFrame();
      aWindow.setBounds(600, 600, 600, 600);
      aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
      JPanel content = new JPanel();
      content.add(new MouseMotionEvents());
      aWindow.add(content);
      
      aWindow.setVisible(true);
    }
  }
  
  class MouseMotionEvents extends JPanel implements MouseListener,
      MouseMotionListener {
    Point p;
    Point r;
  
    public MouseMotionEvents() {
      addMouseListener(this);
      addMouseMotionListener(this);
    }
  
    public void mouseClicked(MouseEvent me) {
      // p = me.getPoint();
      // repaint();
    }
  
    public void mouseEntered(MouseEvent me) {
    }
  
    public void mouseExited(MouseEvent me) {
    }
  
    public void mousePressed(MouseEvent me) {
      p = me.getPoint();
      // repaint();
    }
  
    public void mouseReleased(MouseEvent me) {
      r = me.getPoint();
      repaint();
    }
  
    public void mouseDragged(MouseEvent me) {
      r = me.getPoint();
      repaint();
    }
  
    public void mouseMoved(MouseEvent me) {
    }
  
    public void paint(Graphics g) {
      if (p != null &amp;&amp; r != null) {
        Dimension d = getSize();
        int xc = d.width / 2;
        int yc = d.height / 2;
        if(p.getX()-r.getX()&gt;0 &amp;&amp; p.getY()-r.getY()&gt;0){
          g.drawRect((int)p.getX(), (int)p.getY(), (int)(p.getX()-r.getX()), (int)(p.getY()-r.getY()));
        }
        if(p.getX()-r.getX()&gt;0 &amp;&amp; p.getY()-r.getY()&lt;0){
          g.drawRect((int)p.getX(), (int)p.getY(), (int)(p.getX()-r.getX()), (int)(-p.getY()+r.getY()));
        }
        if(p.getX()-r.getX()&lt;0 &amp;&amp; p.getY()-r.getY()&gt;0){
          g.drawRect((int)p.getX(), (int)p.getY(), (int)(-p.getX()+r.getX()), (int)(p.getY()-r.getY()));
        }
        if(p.getX()-r.getX()&lt;0 &amp;&amp; p.getY()-r.getY()&lt;0){
          g.drawRect((int)p.getX(), (int)p.getY(), (int)(-p.getX()+r.getX()), (int)(-p.getY()+r.getY()));
        }
        
      }
    }
  }
  </code></pre>