MouseEvent.getY() : MouseEvent « java.awt.event « Java by API






MouseEvent.getY()

 
import java.awt.event.*;
import javax.swing.*;

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

  public MainClass() {
    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());
      }
    });
  }

  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 MainClass());
    frame.setVisible(true);
  }
}

           
         
  








Related examples in the same category

1.MouseEvent.MOUSE_CLICKED
2.MouseEvent.MOUSE_ENTERED
3.MouseEvent.MOUSE_EXITED
4.MouseEvent.MOUSE_PRESSED
5.MouseEvent.MOUSE_RELEASED
6.MouseEvent: getClickCount()
7.MouseEvent: getComponent()
8.MouseEvent: getModifiers()
9.MouseEvent: getX()