Java定时器在第一轮后加速

Java定时器在第一轮后加速,java,timer,delay,Java,Timer,Delay,最近我创建了一个程序,测试你在五秒钟内点击一个按钮的速度,并告诉你每秒的点击率。当我只运行一次测试时,一切正常,但最终我决定添加一些东西,允许您在第一次运行后重新启动测试。然而,在这些后续运行中,它似乎不是从5递减1,而是递减2(从5、4、3、2、1变为5、3、1)。当我对控制台进行一些输出时,计时器的速度似乎是原来的两倍,每0.5秒刷新一次,而不是1秒。我已经用多种方法进行了测试,做了一些研究,但似乎没有弄明白 public class ClickSpeedTest extends Frame

最近我创建了一个程序,测试你在五秒钟内点击一个按钮的速度,并告诉你每秒的点击率。当我只运行一次测试时,一切正常,但最终我决定添加一些东西,允许您在第一次运行后重新启动测试。然而,在这些后续运行中,它似乎不是从5递减1,而是递减2(从5、4、3、2、1变为5、3、1)。当我对控制台进行一些输出时,计时器的速度似乎是原来的两倍,每0.5秒刷新一次,而不是1秒。我已经用多种方法进行了测试,做了一些研究,但似乎没有弄明白

public class ClickSpeedTest extends Frame implements ActionListener{

    JFrame frame = new JFrame();
    private boolean buttonDisabled = false;
    boolean testActive = false;
    boolean finished = false;
    Timer t;
    Label l;
    Label countdown;
    Button b;
    Button start;
    Font f = new Font("SansSerif", Font.ITALIC, 12);
    Font f1 = new Font("SansSerif", Font.ITALIC, 20);
    Font f2 = new Font("SansSerif", Font.BOLD, 15);
    int clicks = 0;
    static int interval;
    static Timer timer;
    public ClickSpeedTest() {
        timer = new Timer();
        l = new Label();
        b = new Button();
        countdown = new Label();
        l.setBounds(10,50,400,20);
        l.setName("Test");
        l.setEnabled(true);
        l.setText("Start clicking when ready. After 5 seconds your results will be shown.");
        l.setFont(f);
        countdown.setEnabled(true);
        countdown.setText("5");
        countdown.setBounds(200, 60, 400, 50);
        countdown.setFont(f1);
        b.setBounds(0, 100, 400, 350);
        b.addActionListener(this);
        b.setForeground(Color.MAGENTA);
        b.setBackground(Color.CYAN);
        b.setName("Click!");
        b.setActionCommand("Click!");
        b.setLabel("Click!");
        add(b);add(l);add(countdown);
        setName("Click Speed Test");
        setSize(400,400);  
        setLayout(null);  
        setVisible(true);
        setName("Click Speed Test");

    }

    public void actionPerformed(ActionEvent e) {
        if(buttonDisabled) {

            buttonDisabled = false;
            this.dispose();
            frame.setVisible(false);
            new ClickSpeedTest();
        }
        if(!testActive&&!finished) {
            clicks++;
            testActive = true;
            startTimer();
        }
        else if(testActive&&!finished) {
            clicks++;
        }
        else {
            buttonDisabled = true;
            //System.out.println("You clicked "+clicks+" times in 5 seconds. That is approximately "+(double)clicks/5+" clicks per second.");
            frame.setVisible(true);
            frame.getContentPane().setLayout(new FlowLayout());
            frame.getContentPane().add(new JLabel("You clicked "+clicks+" times in 5 seconds. That is approximately "+(double)clicks/5+" clicks per second."));

            frame.pack();
            frame.setVisible(true);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            testActive = false;
            finished = false;
        }
        //System.out.print("Click");
    }

    private void startTimer() {
        interval = 5;
        System.out.println(5);
        timer.scheduleAtFixedRate(new TimerTask() {

            public void run() {
                System.out.println(setInterval());

            }
        }, 1000, 1000);
    }

    private int setInterval() {
        if (interval == 0) {
            timer.cancel();
            timer.purge();
            testActive = false;
            finished = true;

        }
        int intervalCopy = interval - 1;
        if(intervalCopy == -1) {
            countdown.setBounds(35, 60, 400, 50);
            countdown.setFont(f2);
            countdown.setText("Test Finished. See other window for results.");

            b.setLabel("Click again to restart test.");
        }
        else {         
            countdown.setText(Integer.toString(intervalCopy));
        }
        return --interval;
    }
}
主要方法:

    public class Runner {
    public static void main(String[]args) {
        new ClickSpeedTest();
    }
}

在这方面我远不是专家,我还在学习。非常感谢您的帮助。

如果我想看到您描述的行为,如何运行您的代码?我没有看到任何
main
方法?在我的main方法中,我只创建了一个新的ClickSpeedTest对象。public static void main(String[]args){new ClickSpeedTest();}当我开始第二次单击时,我在线程“AWT-EventQueue-0”java.lang.IllegalStateException:计时器已取消。。我使用的是OracleJava9。