java - How to set mouseClicked event? -


i'm trying write program shows me x- , y-coordinates when click left mouse button, doesn't work. console shows me x- , y-coordinates automatically if run program, want see coordination after mouse click.

import java.awt.*; import java.awt.event.*; import javax.swing.swingutilities;  public class simple {     public static void main(string[] args){         mouse maus = new mouse();         maus.mouseclicked(null);     } } 

second file

import java.awt.*; import java.awt.event.*;  public class mouse implements mouselistener {      public void mouseclicked(mouseevent e) {           double mousex = mouseinfo.getpointerinfo().getlocation().getx();          double mousey = mouseinfo.getpointerinfo().getlocation().gety();          system.out.println(mousex+"   "+mousey);     }     @override     public void mouseentered(mouseevent arg0) {}      @override     public void mouseexited(mouseevent arg0) {}      @override     public void mousepressed(mouseevent arg0) {}      @override     public void mousereleased(mouseevent arg0) {}    } 

what doing in code writing mouselistener, instead of adding gui components (so activated when mouse clicked), call yourself. why executed when start program.

what need use addmouselistener method on 1 of gui components, , register listener it.

there's guide here, examples.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -