java - How to move button when you put the mouse over it -


i have jframe 2 buttons: "yes" , "no". , move "yes" button every time user places mouse on it, way user "chasing" it.

here's code:

import java.awt.*; import java.awt.borderlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.mouselistener; import java.awt.event.mousemotionlistener; import java.awt.event.mouseevent; import javax.swing.*; // import javax.swing.jlabel; // import javax.swing.jpanel;  public class beachpartyprimerframe extends jframe  {    private jpanel mousepanel; // panel in mouse events occur    private jlabel statusbar; // label displays event information    private jlabel party, study;    private jbutton yesbutton, yes2button, nobutton;    private int locx []= {200, 100, 500, 200};    private int locy [] = {20, 200, 200, 300};    private int count = 0;    private int mod = 4;      private boolean nobuttonhit = false;    // mousetrackerframe constructor sets gui ,     // registers mouse event handlers    public beachpartyprimerframe()    {       super( "demonstrating mouse events" );        mousepanel = new jpanel(); // create panel       mousepanel.setbackground( color.white ); // set background color       mousepanel.setlayout(null);       add( mousepanel, borderlayout.center ); // add panel jframe       statusbar = new jlabel( "mouse outside jpanel" );        add( statusbar, borderlayout.south ); // add label jframe       party = new jlabel ("do want beach party right now?");       add (party, borderlayout.north);       study = new jlabel ("great! keep studying hard!!");       study.setvisible(false);       study.setfont(new font("serif",font.plain, 24));       study.setforeground(color.red);       study.setsize(400,80);       study.setlocation(100,200);       yesbutton = new jbutton ("yes");       yesbutton.setsize (60,40);       yesbutton.setlocation(200, 20);         nobutton = new jbutton ("no");       nobutton.setsize(60,40);       nobutton.setlocation (320, 20);       buttonhandler bhandler = new buttonhandler();       yesbutton.addactionlistener (bhandler);       nobutton.addactionlistener(bhandler);        mousepanel.add(yesbutton);       mousepanel.add(nobutton);       mousepanel.add(study);        // create , register listener mouse , mouse motion events       mousehandler handler = new mousehandler();        mousepanel.addmouselistener( handler );        mousepanel.addmousemotionlistener( handler );         } // end mousetrackerframe constructor    public void hidebuttons () {        system.out.println ("try hide buttons");        yesbutton.setvisible(false);    }    private class buttonhandler implements actionlistener     {       // handle button event       public void actionperformed( actionevent event )       {           if (event.getactioncommand() == "no") {              system.out.println ("no button hit");           }           else {              system.out.println ("yes button pressed");              yesbutton.setlocation(yesbutton.getx(),                       yesbutton.gety() + 60);          }       } // end method actionperformed    } // end private inner class buttonhandler    private class mousehandler implements mouselistener,        mousemotionlistener     {       // mouselistener event handlers       // handle event when mouse released after press       public void mouseclicked( mouseevent event )       {          statusbar.settext( string.format( "clicked @ [%d, %d]",              event.getx(), event.gety() ) );          if (event.ismetadown())              system.out.println("right button clicked");          else              system.out.println("left button clicked");        } // end method mouseclicked        // handle event when mouse pressed       public void mousepressed( mouseevent event )       {          statusbar.settext( string.format( "pressed @ [%d, %d]",              event.getx(), event.gety() ) );       } // end method mousepressed        // handle event when mouse released after dragging       public void mousereleased( mouseevent event )       {          statusbar.settext( string.format( "released @ [%d, %d]",              event.getx(), event.gety() ) );       } // end method mousereleased        // handle event when mouse enters area       public void mouseentered( mouseevent event )       {          statusbar.settext( string.format( "mouse entered @ [%d, %d]",              event.getx(), event.gety() ) );          mousepanel.setbackground( new color (255, 255,0));       } // end method mouseentered        // handle event when mouse exits area       public void mouseexited( mouseevent event )       {          statusbar.settext( "mouse outside jpanel" );          mousepanel.setbackground( color.white );       } // end method mouseexited        // mousemotionlistener event handlers       // handle event when user drags mouse button pressed       public void mousedragged( mouseevent event )       {          statusbar.settext( string.format( "dragged @ [%d, %d]",              event.getx(), event.gety() ) );       } // end method mousedragged        // handle event when user moves mouse       public void mousemoved( mouseevent event )       {            statusbar.settext( string.format( "moved @ [%d, %d]",              event.getx(), event.gety() ) );        } // end method mousemoved    } // end inner class mousehandler } // end class mousetrackerframe 

and here's driver class:

import javax.swing.jframe; public class beachpartyprimer  {    public static void main( string args[] )    {        beachpartyprimerframe mousetrackerframe = new beachpartyprimerframe();        mousetrackerframe.setdefaultcloseoperation( jframe.exit_on_close );       mousetrackerframe.setsize( 600, 400 ); // set frame size       mousetrackerframe.setvisible( true ); // display frame    } // end main } // end class mousetracker 

here's screenshot of how looks like: screenshot of program

currently, if click "yes", button moves down little bit: enter image description here

but said beofre, change , make user chasing it.

you add mouselistener yesbutton. override mouseentered method , set random position button :

  yesbutton.addmouselistener(new mouseadapter() {         public void mouseentered(java.awt.event.mouseevent evt) {             yesbutton.setlocation(new random().nextint(600), new random().nextint(400));         }     }); 

i created random position x , y in range 600 , 400 (your window size), can of course adjust it.


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -