import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public class Mover {
public static void main(String[] args){
JFrame mv = new MiVentana();
mv.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mv.pack();
mv.setVisible(true);
}
}
class MiVentana extends JFrame{
MiPan mp = new MiPan();
public MiVentana(){
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(mp);
}
}
class MiPan extends JPanel{
MiBoton b1 = new MiBoton("pepe",100,100);
MiLinea l1 = new MiLinea();
public MiPan(){
setLayout(null);
setBackground(Color.WHITE);
setPreferredSize(new Dimension(400,400));
add(l1);
l1.setBounds(200,100,100,4);
l1.setEnabled(false);
add(b1);
b1.setBounds(100,100,100,20);
b1.addMouseMotionListener(new MouseMotionListener(){
public void mouseDragged(MouseEvent arg0){
b1.setBounds(arg0.getX(),arg0.getY(),100,20);
b1.setText(arg0.getX()+" : "+arg0.getY());
}
public void mouseMoved(MouseEvent arg0) {}
});
}
}
class MiBoton extends JButton{
String cont;
int x;
int y;
public MiBoton(String cont, int x, int y){
this.cont = cont;
this.x = x;
this.y = y;
setBackground(Color.GREEN);
setPreferredSize(new Dimension(100,20));
setBorderPainted(true);
setEnabled(false);
setText(cont);
}
}
class MiLinea extends JButton{
public MiLinea(){
setPreferredSize(new Dimension(100,4));
setBackground(Color.BLACK);
setBorder(null);
}
}