title: dragging a jlabel around the screen
body: <p>So I am trying to click and drag a JLabel around a JFrame. The following code allows a JLabel to be moved around the screen when the mouse is pressed / dragged at any point on the screen, but I am not sure how to add a second ActionListener to check if the mouse is clicking on the label, assuming that is the solution.</p>
  
  <p>I would like to have multiple JLabels on the screen so that the only label being moved is the one that the mouse has clicked and is now dragging.</p>
  
  <p>Thanks.     </p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;
  
  @SuppressWarnings("serial")
  public class test extends JFrame implements MouseMotionListener {
  
  private JPanel panel = new JPanel(null);    
  private JLabel dragLabel = new JLabel("drag test");
  private int mouseX = 200;
  private int mouseY = 200;
  
  public test() {
      this.add(panel);
      panel.setBackground(Color.WHITE);
      panel.add(dragLabel);
      dragLabel.setForeground(Color.RED);
      dragLabel.setBounds(mouseX, mouseY, 100, 50);
      panel.addMouseMotionListener(this);
  }
  
  @Override
  public void mouseDragged(MouseEvent e) {
      mouseX = e.getX();
      mouseY = e.getY();
      dragLabel.setBounds(mouseX, mouseY, 100, 50);
  }
  
  @Override
  public void mouseMoved(MouseEvent e) {}
  
  public static void main(String[] args) {
      test frame = new test();
      frame.setVisible(true);
      frame.setSize(600, 400);
      frame.setResizable(false);
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  }
  </code></pre>