使用摆动计时器更新标签 [英] Update a Label with a Swing Timer

查看:26
本文介绍了使用摆动计时器更新标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码有点问题.

我正在用一个随机数启动一个计时器,我想每秒更新一个带有倒计时的 JLabel.但我还没有想出如何做到这一点,因为计时器触发的唯一侦听器是在它的末尾(我知道).

I'm starting a timer with a random number, and I want to update a JLabel with the countdown, every second. But I haven't figured out how to do so, since the only listener the timer triggers is at the end of it (that I know).

代码如下:

int i = getTimer(maxWait);
te1 = new Timer(i, this);
label.setText(i+"");
te1.start();

...

public int getTimer(int max){
    Random generator = new Random();
    int i = generator.nextInt(max);
    return i*1000;
}

...

public void actionPerformed(ActionEvent ev){
    if(ev.getSource() == te1){
        label.setText(i+"");
        te1.stop();
    }
}

推荐答案

我不太明白你为什么要使用 Random,但这里有一些观察:

I don't really understand your question why you are using the Random, but here are some observations:

我想每秒更新一个带有倒计时的 JLabel.

I want to update a JLabel with the countdown, every second.

然后您需要将计时器设置为每秒触发一次.所以 Timer 的参数是 1000,不是某个随机数.

Then you need to set the Timer to fire every second. So the parameter to the Timer is 1000, not some random number.

此外,在您的 actionPerformed() 方法中,您在 Timer 第一次触发时停止它.如果您正在进行某种倒计时,那么您只会在时间达到 0 时停止计时器.

Also, in your actionPerformed() method you stop the Timer the first time it fires. If you are doing a count down of some kind then you would only stop the Timer when the time reaches 0.

这是一个使用计时器的简单示例.它只是每秒更新一次时间:

Here is a simple example of using a Timer. It just updates the time every second:

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;

    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)
    {
        //System.out.println(e.getSource());
        timeLabel.setText( new Date().toString() );
    }

    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();
            }
        });
    }
}

如果您需要更多帮助,请使用适当的 SSCCE 来说明问题,从而更新您的问题.所有的问题都应该有一个适当的 SSCCE,而不仅仅是几行随机的代码,这样我们才能理解代码的上下文.

If you need more help then update your question with a proper SSCCE demonstrating the problem. All questions should have a proper SSCCE, not just a few random lines of code so we can understand the context of the code.

这篇关于使用摆动计时器更新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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