
19/12/2011, 12:22
|
 | | | Fecha de Ingreso: diciembre-2011 Ubicación: México
Mensajes: 2
Antigüedad: 13 años, 2 meses Puntos: 0 | |
Respuesta: Arrastrar y soltar un componente(Label, boton) con el mouse Hola mira yo encontre este ejemplo y me sirvio ya que estaba haciendo un proyecto similar a:
Código:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DragTest {
public static void main(String[] args) {
MyWindow window = new MyWindow();
window.setVisible(true);
window.setPreferredSize(new Dimension(400,400));
window.pack();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyWindow extends JFrame{
public MyWindow(){
super(”Drag test”);
Container container = getContentPane();
container.setLayout(null);
MyButton boton1 = new MyButton(”Move me”);
container.add(boton1);
boton1.setBounds(10, 10, 100, 30);
}
}
class MyButton extends JButton implements MouseMotionListener{
public MyButton(String text){
super.setText(text);
addMouseMotionListener(this);
}
public void mouseDragged(MouseEvent mme) {
setLocation(
this.getX() + mme.getX() - this.getWidth() / 2,
this.getY() + mme.getY() - this.getHeight() / 2
);
}
public void mouseMoved(MouseEvent mme) {}
}
checalo |