JavaFX每秒显示时间和刷新 [英] JavaFX Displaying Time and Refresh in Every Second

查看:2042
本文介绍了JavaFX每秒显示时间和刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想就此问题提出任何建议。

Hello I would like to ask for any advice on this problem.

我想用HH:MM:SS格式显示当前时间(每秒刷新一次)标签或任何有利于它的组件。

I would like to display current time in HH:MM:SS format (refreshing every second) in label or whatever component that is good for it.

任何建议?

编辑:有人要求代码..所以我把它放在这里以更好地描述问题。 我当时没有代码我想要实现的是简单的GUI日记,在其中一个标签中,我想显示剩余时间,直到最接近的事件,而在另一个标签中,我想显示像时钟一样刷新每个第二。我需要它来让剩下的时间工作。我能想到的只是创建新的线程来完成它并刷新时钟,但我在JavaFX中使用多线程并不是那么先进。所以我想知道是否有人可以建议我使用比多线程更复杂的东西(我不知道如何将该线程实现到JavaFX组件中)

Someone asked for a code.. so I put it here for better description of the problem. "I have no code for that time What I'm trying to achieve is simple GUI diary and in one of the labels I would like to display time remaining until the closest event and in the other label I want to display like clocks that refreshes every second. I need it to get remaining time working. All I can think of is creating new thread that will do it and refreshes the clock, but I am not that advanced to use multithreading in JavaFX . So I was wondering if anyone can advice me with something less complicated than multithreading (I dont know how to implement that thread into JavaFX components)"

推荐答案

带时间轴的版本:

long endTime = ...;
Label timeLabel = new Label();
DateFormat timeFormat = new SimpleDateFormat( "HH:mm:ss" );
final Timeline timeline = new Timeline(
    new KeyFrame(
        Duration.millis( 500 ),
        event -> {
            final long diff = endTime - System.currentTimeMillis();
            if ( diff < 0 ) {
            //  timeLabel.setText( "00:00:00" );
                timeLabel.setText( timeFormat.format( 0 ) );
            } else {
                timeLabel.setText( timeFormat.format( diff ) );
            }
        }
    )
);
timeline.setCycleCount( Animation.INDEFINITE );
timeline.play();

这篇关于JavaFX每秒显示时间和刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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