使用Swing Timer更新标签 [英] Update a Label with a Swing Timer

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

问题描述

我在使用这段代码时遇到了一些麻烦。

I'm having some trouble with this piece of code.

我正在启动一个带随机数的计时器,我想更新一个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();
    }
}


推荐答案

我我真的不明白你的问题,为什么你使用随机,但这里有一些观察:

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每秒触发一次。所以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.

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

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