Auf Thema antworten

Der Vollständigkeit halber:


[CODE]import javax.swing.JButton;

import javax.swing.JFrame;


public class DragExample extends JFrame {

    public DragExample() {

        this.setLayout(null);

        JButton tb=new JButton("Drag&DropMe");

        tb.addMouseMotionListener(new MyButtonMotionListener());

        tb.setBounds(10,20,130,50);

        this.getContentPane().add(tb);

    }

   

    public static void main(String[] args) {

        JFrame frame = new DragExample();

        frame.setBounds(100, 100, 500, 500);

        frame.setVisible(true);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}

[/CODE]


Zudem solltest Du sicherstellen, dass die Maus mit dem Button nicht ausserhalb des Frames bewegt werden kann, ansonsten ist der Button beim Loslassen nicht mehr sichtbar.



Oben