swing - Checking if the mouse is clicked in Java -


i writing gui in java. need know how check if user clicks mouse. know how check position of mouse, need check if it's clicked.

import java.awt.event.mouseevent; import java.awt.event.mouselistener; /*  * copyright (c) 1995, 2008, oracle and/or affiliates. rights reserved.  *  * redistribution , use in source , binary forms, or without  * modification, permitted provided following conditions  * met:  *  *   - redistributions of source code must retain above copyright  *     notice, list of conditions , following disclaimer.  *  *   - redistributions in binary form must reproduce above copyright  *     notice, list of conditions , following disclaimer in  *     documentation and/or other materials provided distribution.  *  *   - neither name of oracle or names of  *     contributors may used endorse or promote products derived  *     software without specific prior written permission.  *  * software provided copyright holders , contributors "as  * is" , express or implied warranties, including, not limited to,  * implied warranties of merchantability , fitness particular  * purpose disclaimed.  in no event shall copyright owner or  * contributors liable direct, indirect, incidental, special,  * exemplary, or consequential damages (including, not limited to,  * procurement of substitute goods or services; loss of use, data, or  * profits; or business interruption) caused , on theory of  * liability, whether in contract, strict liability, or tort (including  * negligence or otherwise) arising in way out of use of  * software, if advised of possibility of such damage.  */    import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.*;  import javax.imageio.*; import javax.swing.*;  /**  * class demonstrates how load image external file  */ public class loadimageapp extends component {      bufferedimage img;      public void paint(graphics g) {         g.drawimage(img, 50, 70, null);     }      public loadimageapp() {        try {            img = imageio.read(new file("strawberry.jpg"));        } catch (ioexception e) {        }      }      public dimension getpreferredsize() {         if (img == null) {              return new dimension(100,100);         } else {            return new dimension(img.getwidth(null), img.getheight(null));        }     }      public static void main(string[] args) throws interruptedexception {          jframe f = new jframe("load image sample");          f.addwindowlistener(new windowadapter(){                 public void windowclosing(windowevent e) {                     system.exit(0);                 }             });          f.add(new loadimageapp());         f.pack();         f.setvisible(true);         f.setresizable(true);         while(true){             thread.sleep(1000);             pointerinfo = mouseinfo.getpointerinfo();             point b = a.getlocation();             int x = (int) b.getx();             int y = (int) b.gety();              system.out.println(x);             system.out.println(y);         }         }     public void mouseclicked(mouseevent e) {         system.out.println("mouseclicked");           } } 

this code, please explain how impliment 1 mouse checker thing. thanks!

implement mouselistener interface , put code inside mouseclicked(mouseevent e) method. add instance of class listener component.

basically, create class this:

class mymouselistener implements mouselistener {  @override public void mouseclicked(mouseevent arg0) {      //put code want here     //...     //... }  @override public void mouseentered(mouseevent arg0) { }  @override public void mouseexited(mouseevent arg0) { }  @override public void mousepressed(mouseevent arg0) { }  @override public void mousereleased(mouseevent arg0) {  }  } 

then simple create new instance of new class mymouselistener

mymouselistener mml = new mymouselistener(); 

and add listener component. like:

mypanel.addmouselistener(mml); 

all information here: http://docs.oracle.com/javase/7/docs/api/java/awt/event/mouselistener.html


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -