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
Post a Comment