简体   繁体   English

javax.swing.timer减去我单击“开始”按钮后的数量

[英]javax.swing.timer subtracts as much as I click on start button

First of all hi! 首先嗨! This is my first post on stackoverflow! 这是我关于stackoverflow的第一篇文章! This is my second attempt to program something in Java and the first ever attempt with a gui. 这是我第二次尝试用Java编程,也是第一次使用gui。

I'm actually having 2 problems. 我实际上有2个问题。 The first being the program and the second understanding a part of the code. 第一个是程序,第二个是代码的一部分。

How the program should work: 该程序应如何工作:

When pressing start it counts down from 01:00 to 00:00 every minute (01:00 -> 00:59 -> 00:58). 按下开始键时,每分钟从01:00倒数到00:00(01:00-> 00:59-> 00:58)。 When you press stop, it stops counting down (duh) and when you press start again, it starts from 01:00 like the first time. 当您按停止时,它将停止倒计时(duh);当您再次按开始时,它将从01:00开始,就像第一次一样。

The program problem: 程序问题:

With that said. 照这样说。 This only works the first time I press start. 这仅在我第一次按开始时有效。 When I press start multiple times it subtracts that amount of times from the clock. 当我多次按启动键时,它将从时钟中减去该次数。 Pressed 2 times (01:00 -> 00:58 -> 00:56). 按下2次(01:00-> 00:58-> 00:56)。 Pressed 4 times (01:00 -> 00:56 -> 00:52). 按下4次(01:00-> 00:56-> 00:52)。 etc... This obviously should not be happening. 等等...这显然不应该发生。

The understanding problem: 理解问题:

I am having a hard time understanding why the timer requires an ActionListener and why it works when you use 'null'. 我很难理解为什么计时器需要一个ActionListener以及为什么在使用“空”时它可以工作。 In some cases it also works when using 'this' (which I also don't understand.). 在某些情况下,使用“ this”(我也不理解)也可以使用。

Java Swing Timer Documentation Java Swing计时器文档

import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CountdownClock extends JFrame
{

    private int oneSecond = 1000; //Milliseconds
    private Timer timer = new Timer(oneSecond * 60, null);
    private int timerCount = 59;

    public static void main(String args[])
    {
        new CountdownClock();
    }

    CountdownClock()
    {
        this.getContentPane().setLayout(null);
        this.setBounds(800, 450, 300, 125);

        final JLabel countdownLabel = new JLabel("01:00");
        countdownLabel.setBounds(110, 10, 125, 30);
        countdownLabel.setFont(new Font("Serif", Font.PLAIN, 30));

        JButton startButton = new JButton("Start");
        startButton.setBounds(10, 50, 125, 30);
        startButton.addActionListener(new ActionListener() 
        {
        public void actionPerformed(ActionEvent e)
        {
            timer.setRepeats(true);
            timer.stop();
            countdownLabel.setText("01:00");
            timerCount = 59;
            timer.start();
            timer.addActionListener(new ActionListener() 
            {
                public void actionPerformed(ActionEvent evt)
                {     
                    if (timerCount == 0)
                    {
                        timer.stop();
                        countdownLabel.setText("00:00");
                        timerCount = 59;
                    }
                    else if (timerCount <= 9)
                    {
                        countdownLabel.setText("00:0" + String.valueOf(timerCount));
                        timerCount = timerCount - 1;
                    }
                    else
                    {
                        countdownLabel.setText("00:" + String.valueOf(timerCount));
                        timerCount = timerCount - 1;
                    }
                }
            });
        }
    });

    JButton stopButton = new JButton("Stop");
    stopButton.setBounds(150, 50, 125, 30);
    stopButton.addActionListener(new ActionListener() 
    {
        public void actionPerformed(ActionEvent e)
        {
            timer.stop();
            countdownLabel.setText("01:00");
            timerCount = 59;
        }
    });

    add(countdownLabel);
    add(startButton);
    add(stopButton);

    setVisible(true);
    }
}

This happens because you are adding an ActionListener to the Timer every time that you press the button. 发生这种情况的原因是,每当您按下按钮时,都将ActionListener添加到Timer So, since a Timer allows multiple listeners, everyone of them gets notified when the timer ticks. 因此,由于Timer允许多个侦听器,因此当计时器计时时,每个人都会收到通知。

To solve the problems you could just instantiate a new Timer every time you press the start button ( timer = new Timer() ). 为了解决这些问题,您可以在每次按下启动按钮时实例化一个新的Timertimer = new Timer() )。 Or add the ActionListener just once in your JFrame constructor . 或仅在JFrame构造函数中添加一次ActionListener。 Or even remove the listener (but you should save a reference to it somewhere). 甚至删除监听器(但是您应该将对它的引用保存在某个地方)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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