2017-07-30 56 views
-1

我有一個媒體播放器,我在我的媒體播放器上爲滑塊值和時間值放置了一個listiner我的問題是如何格式化時間我在標籤上顯示爲0:00,第一個0表示秒,第二個表示秒,第三個表示每秒。我如何格式化將要在標籤中的字符串

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

回答

-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

'0.50分鐘'30秒?這不是你在問題中所要求的! – fabian

0

只需從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)); 
} 
相關問題