计时器不会停止.尝试点击 n 步 [英] Timer doesn't stop. Trying to do clicks for n steps

查看:34
本文介绍了计时器不会停止.尝试点击 n 步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个计时器,每 300 毫秒执行 15 次 mouseRelease 方法.为此,我写了这样的东西:

I want to do a timer that executes the method mouseRelease for 15 times every 300 ms. For this purpose I have written something like this:

import java.awt.*;
import java.awt.event.*;
import java.awt.SystemColor;
import java.io.*;
import java.lang.*;
import java.util.StringTokenizer;
import javax.swing.Timer;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

public class myapp extends PlugInFrame implements ActionListener, ItemListener, MouseListener {
     [...]
     int total = 0;
     public void mouseReleased(MouseEvent m) {
            [...]
            if (total == 0){
                timer.setRepeats(true);
                timer.setCoalesce(true);
                timer.start();          
            }else if (total==14){
                timer.stop;
                total =0;
            }     
        }

    Timer timer = new Timer(300, new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
             try {
                if (total < 15){
                    total++;
                    Robot r = new Robot();
                    r.mouseRelease(InputEvent.BUTTON1_MASK);    
                }
            } catch(AWTException e) {
                throw new RuntimeException("Failed to create java.awt.Robot for Robo instance", e);
            }
        }
    });
}

我的问题是,虽然它的工作做得很好,但最后它并没有停止计时器,所以在我完全结束程序之前,它不会停止点击.有什么问题吗?

My problem is that although it does well its work, at the end it doesn't stop the timer so until I end completely the program it doesn't stop to do clicks. What's wrong with it?

我希望我可以打印 var total 以查看它是否增加,但此代码用于 ImageJ 中的插件,我不知道如何打印,稍后查看日志.

I wish I could print the var total to see if it increases but this code is for a plugin in ImageJ and I don't know how to print and later see the log.

推荐答案

mouseReleased() 代码仅在您松开鼠标时执行一次.它不会执行 15 次.当 Timer 启动时,ActionListener 代码会接管并每 300 毫秒执行一次.

The mouseReleased() code is executed only once, when you release the mouse. It is not executed 15 times. When the Timer is started the ActionListener code takes over and is executed every 300ms.

在 ActionListener 代码本身中停止 Timer 更容易,因为这是 total 变量的值增加的地方.

It is easier to stop the Timer in the ActionListener code itself, since this is where the value of the total variable is incremented.

也许是这样的:

public void actionPerformed(ActionEvent e)
{
    if (total == 15)
    {
        Timer timer = (Timer)e.getSource();
        timer.stop();
    }
    else
    {
        // invoke your current code
    }
}

必须承认我不太理解你的逻辑,所以我无法提出更好的建议.那就是我不明白您为什么要尝试连续生成 15 个 mouseReleased() 事件.

Must admit I don't really understand your logic so I can't make any better suggestion. That is I don't understand why you are attempting to generate 15 mouseReleased() events in succession.

一个简单的例子:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;

public class TimerTime extends JPanel implements ActionListener
{
    private JLabel timeLabel;
    private int count = 0;

    public TimerTime()
    {
        timeLabel = new JLabel( new Date().toString() );
        add( timeLabel );

        Timer timer = new Timer(1000, this);
        timer.setInitialDelay(1);
        timer.start();
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        timeLabel.setText( new Date().toString() );
        count++;
        System.out.println(count);

        if (count == 10)
        {
            Timer timer = (Timer)e.getSource();
            timer.stop();
        }
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("TimerTime");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new TimerTime() );
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

这篇关于计时器不会停止.尝试点击 n 步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆