Swing中的MouseMoved计时器倒计时

时间:2017-04-08 14:31:24

标签: java swing timer

我想问一下,当第一次移动鼠标时,如何在定时器启动时进行60秒倒计时(并在标题中显示)。然后在最后我想显示一些jOptionPane.Message或其他东西。这是代码......

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 个答案:

答案 0 :(得分:0)

  1. 创建实现ActionListener的类的实例:示例EmilsListener list = new EmilsListener();
  2. 创建Timer类的实例,并传递对象及其重新启动的时间:Timer tym = new Timer(1000,list);
  3. 将侦听器添加到计时器,然后启动计时器:例如
  4. tym.addActionListener(list);
    tym.start();//But before this make sure you defined the EmilsListener class which must also contain your `jPanel2MouseMoved()` method.
    

    您可以在this stackoverflow page

    接受的答案中查看完整的计时器示例