Swing Button anklicken und bewegen

Diskutiere Button anklicken und bewegen im AWT, Swing, JavaFX & SWT Bereich.
Z

Z3R0-23

Hallo, ich versuche grade einen Button zu bewegen das er der Maus folgt bekomme es aber nur so halb hin
Java:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;

    public class MoveButton extends JPanel {
      JButton button = new JButton("I Move");

      public MoveButton() {
        setLayout(null);
        add(button);
        button.setSize(button.getPreferredSize());
        button.setLocation(50, 50);
        addMouseListener(new MouseAdapter() {
          public void mousePressed(MouseEvent e) {
              button.setLocation(e.getX(), e.getY());
              //Test
              System.out.println("Geht 1");
              }
          public void mouseReleased(MouseEvent e) {
              button.setLocation(e.getX(), e.getY());
              //Test
              System.out.println("Geht 2");
              }
        });
      }
      //Erzeugen des Fensters
      public static void main(String[] args) {
        JFrame frame = new JFrame("MoveButton");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setSize(250, 200);
        frame.setLocation(200, 200);
        frame.setContentPane(new MoveButton());
        frame.setVisible(true);
      }
    }
 
MoxxiManagarm

MoxxiManagarm

Meinst du etwa so?

Java:
public class MoveButton extends JPanel {
    JButton button = new JButton("I Move");

    public MoveButton() {
        setLayout(null);
        add(button);
        button.setSize(button.getPreferredSize());
        button.setLocation(50, 50);

        // when initially clicking in the panel
        addMouseMotionListener(new MouseAdapter() {
            public void mouseDragged(MouseEvent e) {
                button.setLocation(e.getX(), e.getY());
            }
        });

        // when clicking initially on the button
        button.addMouseMotionListener(new MouseAdapter() {
            public void mouseDragged(MouseEvent e) {
                button.setLocation(button.getX() + e.getX(), button.getY() + e.getY());
            }
        });
    }
    //Erzeugen des Fensters
    public static void main(String[] args) {
        JFrame frame = new JFrame("MoveButton");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setSize(250, 200);
        frame.setLocation(200, 200);
        frame.setContentPane(new MoveButton());
        frame.setVisible(true);
    }
}
 
Z

Z3R0-23

Meinst du etwa so?

Java:
public class MoveButton extends JPanel {
    JButton button = new JButton("I Move");

    public MoveButton() {
        setLayout(null);
        add(button);
        button.setSize(button.getPreferredSize());
        button.setLocation(50, 50);

        // when initially clicking in the panel
        addMouseMotionListener(new MouseAdapter() {
            public void mouseDragged(MouseEvent e) {
                button.setLocation(e.getX(), e.getY());
            }
        });

        // when clicking initially on the button
        button.addMouseMotionListener(new MouseAdapter() {
            public void mouseDragged(MouseEvent e) {
                button.setLocation(button.getX() + e.getX(), button.getY() + e.getY());
            }
        });
    }
    //Erzeugen des Fensters
    public static void main(String[] args) {
        JFrame frame = new JFrame("MoveButton");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setSize(250, 200);
        frame.setLocation(200, 200);
        frame.setContentPane(new MoveButton());
        frame.setVisible(true);
    }
}
ja genau so danke
 
Thema: 

Button anklicken und bewegen

Passende Stellenanzeigen aus deiner Region:
Anzeige

Neue Themen

Anzeige

Anzeige
Oben