简体   繁体   中英

MouseMoved Timer Countdown in Swing

i would like to ask how to make 60 sec countdown with Timer starting when first moving the mouse (and display it in the title). Then at the end i would like to display some jOptionPane.Message or something. Here's the code...

private void jPanel2MouseMoved(java.awt.event.MouseEvent evt)   {                                   
    jPanel1.setBounds(evt.getX(), evt.getY(), jPanel1.getWidth(), jPanel1.getHeight());
    int a = KEK.nextInt(jPanel2.getWidth()-15);
    int b = KEK.nextInt(jPanel2.getHeight()-15);
    if(jPanel1.getBounds().intersects(jPanel3.getBounds()) == true){
        rurin++;
        jPanel3.setBounds(a, b, jPanel3.getWidth(), jPanel3.getHeight());
        this.setTitle("Number of red dots touched: " +rurin+" ");
    } 
}
  1. Create an instance of the class that implements the ActionListener : Example EmilsListener list = new EmilsListener();
  2. Create an instance of the Timer class, and pass object and the time for it to restart to it: Timer tym = new Timer(1000,list);
  3. Add the listener to the timer and then start the timer: Eg
 tym.addActionListener(list); tym.start();//But before this make sure you defined the EmilsListener class which must also contain your `jPanel2MouseMoved()` method. 

You can check out a complete example of the timer in the accepted answer from this stackoverflow page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM