添加计时器并显示标签文本 [英] Adding a timer and displaying label text

查看:119
本文介绍了添加计时器并显示标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JLabel。最初我已经设置了一些文字。

I have a JLabel. Initially i have set some text to it.

JLabel j = new JLabel();
// set width height and position

j.setText("Hello");

我只希望文本Hello显示5秒钟。然后我希望显示文本Bye。

I only want the text Hello to display for 5 seconds. then i want the text Bye to be displayed.

我怎么能这样做。

我的工作;但我知道这是错的,因为它一次只执行1个if-else块。我想我需要一个计时器或一个计数器。让这个工作。帮助?

My workings; but i know it's wrong as it only executes 1 if-else block at a time. I think i will need a timer or a counter. to get this working. Help ?

long time = System.currentTimeMillis();

if ( time < (time+5000)) { // adding 5 sec to the time
    j.setText("Hello");

} else {
    j.setText("Bye");

}


推荐答案

Swing is一个事件驱动的环境,你需要掌握的最重要的概念之一就是你绝不能以任何方式阻止事件调度线程(包括但不限于循环,I / O或<$ c $) c>线程#sleep )

Swing is a event driven environment, one of the most important concepts you need to under stand is that you must never, ever, block the Event Dispatching Thread in any way (including, but not limited to, loops, I/O or Thread#sleep)

话虽如此,有很多方法可以实现你的目标。最简单的是通过 javax.swing.Timer 类。

Having said that, there are ways to achieve you goal. The simplest is through the javax.swing.Timer class.

public class TestBlinkingText {

    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new BlinkPane());
                frame.setSize(200, 200);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });

    }

    protected static class BlinkPane extends JLabel {

        private JLabel label;
        private boolean state;

        public BlinkPane() {
            label = new JLabel("Hello");
            setLayout(new GridBagLayout());

            add(label);
            Timer timer = new Timer(5000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    label.setText("Good-Bye");
                    repaint();
                }
            });
            timer.setRepeats(false);
            timer.setCoalesce(true);
            timer.start();
        }
    }
}

退房

  • The Event Dispatch Thread
  • How to Use Swing Timers

更多信息

这篇关于添加计时器并显示标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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