【问题标题】:how do i format a string that is going to be in a label [duplicate]我如何格式化将在标签中的字符串[重复]
【发布时间】:2018-01-05 19:21:41
【问题描述】:

我有一个媒体播放器,我在我的媒体播放器上为滑块值和时间值添加了一个监听器我的问题是如何格式化我放入标签中的时间以显示为 0:00第一个 0 是分钟,第二个是 10 秒,第三个是每秒。

public void setUpsongDurationSlider()
{
    musicMedia.getMediaPlayer().currentTimeProperty().addListener((obs, oldTme, newTime)->
    {
        homeView.getSongDurationSlider().setValue(newTime.toSeconds());
        homeView.getSongDurationSliderLabel().setText(Double.toString((newTime.toMinutes())));
    });
}

【问题讨论】:

    标签: java javafx


    【解决方案1】:

    所以其他帖子有所帮助,但它是一个需要转换的双精度值,所以我需要使用“%f”而不是“%d”。

    musicMedia.getMediaPlayer().currentTimeProperty().addListener((obs, oldTme, newTime)->
            {
                homeView.getSongDurationSlider().setValue(newTime.toSeconds());
                homeView.getSongDurationSliderLabel().setText(String.format("%.2f min",(newTime.toMinutes())));
    

    【讨论】:

    • 0.50 min 30 秒?这不是您在问题中要求的!
    【解决方案2】:

    只需从Duration 对象中检索分钟/秒,然后使用String.format 填充秒...

    以下示例仅打印到 System.out 并使用代码中创建的属性,但您应该能够根据您的目的对其进行调整:

    ObjectProperty<Duration> duration = new SimpleObjectProperty(Duration.ZERO);
    
    duration.addListener((observable, oldValue, newValue) -> {
        System.out.println(String.format("%d:%02d",
                (long)newValue.toMinutes(),
                ((long) newValue.toSeconds()) % 60));
    });
    
    // test for 1-99 seconds
    for (int i = 1; i < 100; i++) {
        duration.set(Duration.seconds(i));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-10
      • 2017-06-21
      • 2018-08-10
      • 2017-10-27
      • 1970-01-01
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多