Código:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
public class DragTest extends JFrame{
JLabel label;
ImageIcon image;
public DragTest(){
setBounds(10, 10, 1000, 700);
setVisible(true);
setLayout(null);
image=new ImageIcon("oficina1.jpg");
label=new JLabel(image);
label.setBounds(10, 10, 100, 100);
add(label);
oyenteArrastre();
}
public void oyenteArrastre(){
Arrastre a=new Arrastre();
label.addMouseMotionListener(a);
}
class Arrastre implements MouseMotionListener{
@Override
public void mouseDragged(MouseEvent e) {
setLocation(label.getX() + e.getX() - label.getWidth()/2 , label.getY() + e.getY() - label.getHeight()/2);
}
@Override
public void mouseMoved(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
public static void main(String [] args) {
DragTest d=new DragTest();
}
}