setLayout(null); add(button); button.setSize(button.getPreferredSize( )); button.setLocation(20, 20); addMouseListener(new MouseAdapter( ) { public void mousePressed(MouseEvent e) { button.setLocation(e.getX(), e.getY( )); } });
JFrame f = new JFrame("MoveButton"); f.addWindowListener(new WindowAdapter( ) { public void windowClosing(WindowEvent e) { System.exit(0); } }); f.setSize(250, 200); f.setLocation(200, 200); f.setContentPane(new MoveButton( )); f.setVisible(true);