You need to drag the button while holding ctrl. I am writing such a code, the button moves with blinking and duality (that is, the same button appears at a certain moment, but in a different place), and the mouse moves far ahead of the button, and the button moves as if behind it. I can not understand what the error is, on many sites they write that when you move a button it is necessary to take away the current from its previous coordinates, but then if I write it this way does not move at all.

public class Mouse extends JFrame{ private JPanel mainPanel; private JButton button1; private JPanel showPanel; private JTextField textField1; private static int xPosition,lastX; private static int yPosition,lastY; private static boolean flag=false,flag1=false; public Mouse(){ super("Mouse Application"); setLocation(450,145); setSize(300, 300); setContentPane(mainPanel); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); showPanel.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { super.mouseClicked(e); button1.setLocation(e.getX(),e.getY()); repaint(); } }); showPanel.addMouseMotionListener(new MouseMotionAdapter() { @Override public void mouseMoved(MouseEvent e) { super.mouseMoved(e); xPosition=e.getX(); yPosition=e.getY(); textField1.setText("x: "+xPosition+" y: "+yPosition); repaint(); } }); setVisible(true); button1.addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { super.keyPressed(e); flag=e.isControlDown(); } }); button1.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { super.mousePressed(e); if(flag) { flag1 = true; lastX=e.getX(); lastY=e.getY(); } } }); button1.addMouseListener(new MouseAdapter() { @Override public void mouseReleased(MouseEvent e) { super.mouseReleased(e); flag=false; flag1=false; } }); /*button1.addMouseListener(new MouseAdapter() { @Override public void mouseDragged(MouseEvent e) { super.mouseDragged(e); if(flag1){ button1.setLocation(lastX-e.getX(),lastY-e.getY()); lastX=e.getX(); lastY=e.getY(); } } });*/ button1.addMouseMotionListener(new MouseMotionAdapter() { @Override public void mouseDragged(MouseEvent e) { super.mouseDragged(e); if(flag1){ button1.setLocation(e.getX(),e.getY()); //lastX=e.getX(); //lastY=e.getY(); repaint(); } } }); } public static void main(String[] args) { new Mouse(); } } 

    1 answer 1

    The coordinates in MouseEvent are given in the system of the component for which the event was created (i.e. dragging (0, 0) - the upper left corner of the button). You need to calculate the offset of the mouse relative to the point of depression and move the button to this offset. After that the point of pressing will again appear under the cursor.

    You can replace your mouse listeners for a button with this adapter:

     MouseAdapter mouseDrag = new MouseAdapter() { // Π·Π°ΠΏΠΎΠΌΠΈΠ½Π°Π΅ΠΌ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Ρ‹ наТатия Π² Π°Π΄Π°ΠΏΡ‚Π΅Ρ€Π΅ int clickX, clickY; // сюда ΠΆΠ΅ ΠΌΠΎΠΆΠ½ΠΎ пСрСнСсти flag1 public void mousePressed(MouseEvent e) { // вмСсто KeyListener ΠΌΠΎΠΆΠ½ΠΎ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΈΡ‚ΡŒ Π½Π°ΠΆΠ°Ρ‚ΠΈΠ΅ CTRL, ALT ΠΈΠ»ΠΈ SHIFT прямо Ρ‚ΡƒΡ‚ if ( (e.getModifiersEx() & InputEvent.CTRL_DOWN_MASK) != 0 ) { flag1 = true; clickX=e.getX(); clickY=e.getY(); } } @Override public void mouseReleased(MouseEvent e) { flag1=false; } @Override public void mouseDragged(MouseEvent e) { if(flag1){ // сдвиг ΠΌΡ‹ΡˆΠΈ ΠΎΡ‚Π½ΠΎΡΠΈΡ‚Π΅Π»ΡŒΠ½ΠΎ Ρ‚ΠΎΡ‡ΠΊΠΈ наТатия int dx = e.getX() - clickX; int dy = e.getY() - clickY; // послС пСрСмСщСния курсор окаТСтся Π½Π°Π΄ Ρ‚ΠΎΠΉ ΠΆΠ΅ Ρ‚ΠΎΡ‡ΠΊΠΎΠΉ ΠΊΠ½ΠΎΠΏΠΊΠΈ, // Π² ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΉ Π±Ρ‹Π» Π΄ΠΎ сдвига ΠΌΡ‹ΡˆΠΈ button1.setLocation( button1.getX() + dx, button1.getY() + dy ); // repaint Π½Π΅ Π½ΡƒΠΆΠ΅Π½, setLocation Π΅Π³ΠΎ сам Π²Ρ‹Π·Ρ‹Π²Π°Π΅Ρ‚ } } }; button1.addMouseListener( mouseDrag ); button1.addMouseMotionListener( mouseDrag );