2016-08-04 3 views
1

타임 라인을 사용하여 1 분 길이를 기록하는 시스템을 만들려고합니다. 이 상황에서 타임 라인을 초당 증가시키고 Progressbar에 조치를 호출하고 타임 라인을 재설정하기 전까지 1 분 동안 기록하도록합니다.JavaFX 타임 라인을 초당 증가시키고 Progressbar에 바인딩하는 방법은 무엇입니까?

final Timeline timeline = new Timeline(); 
timeline.setCycleCount(Animation.INDEFINITE); 
timeline.getKeyFrames().add(new KeyFrame(Duration.seconds(1))); 
progress.progressProperty().bind(timeline.getTotalDuration()???); 
timeline.play(); 

타임 라인주기가 멈추지 않도록주기 횟수를 무기한으로 설정했습니다. 또한 타임 라인에 Progressbar 속성을 바인딩하려고했지만 Observablevalue가 필요합니다. timeline#getTotalDuration은 제공하지 않습니다. 나는 별도의 스레드에서 바인딩을하지 않고 프로그램을 실행하려고했습니다

는 :

new Thread(new Task<Void>() { 
      @Override 
      protected Void call() throws Exception { 
       while(true){ 
        System.out.printf("s:\t%f\n",timeline.getTotalDuration().toSeconds()); 
        Thread.sleep(1000); 
       } 
      } 
}).start(); 

그러나,이 초당 타이머를 증가하지 않지만, 실행하는 대신 한 번에 모든 사이클을 통해 간다. 설정 한 cycleCount가 INDEFINITE이므로 #totalDuration의 결과는 infinite입니다.

타임 라인을 2 초 단위로 작업하는 방법과 100 %에 도달 할 때까지 Progressbar에 지속 시간을 바인딩하는 방법은 어떻게됩니까? 당신은 그냥 각 분의 끝에 도달 할 때 진행률 표시 줄이 분의 과정을 통해 전체 0에서 반복적으로 증가 및 코드를 실행하려면

답변

4

, 당신이 필요로하는 모든입니다

ProgressBar progress = new ProgressBar(); 
    Timeline timeline = new Timeline(
     new KeyFrame(Duration.ZERO, new KeyValue(progress.progressProperty(), 0)), 
     new KeyFrame(Duration.minutes(1), e-> { 
      // do anything you need here on completion... 
      System.out.println("Minute over"); 
     }, new KeyValue(progress.progressProperty(), 1))  
    ); 
    timeline.setCycleCount(Animation.INDEFINITE); 
    timeline.play(); 

그 진행 막대에 대해 "아날로그"효과를 생성합니다. 즉, 매 초마다 점진적으로 증가하지는 않지만 매분마다 부드럽게 증가합니다. 당신이 정말로 점진적으로 각 초를 늘리려면

, (초)을 대표하는 IntegerProperty을 사용하고, 여기에 진행률 표시 줄의 진행 속성을 바인딩 :

ProgressBar progress = new ProgressBar(); 
    IntegerProperty seconds = new SimpleIntegerProperty(); 
    progress.progressProperty().bind(seconds.divide(60.0)); 
    Timeline timeline = new Timeline(
     new KeyFrame(Duration.ZERO, new KeyValue(seconds, 0)), 
     new KeyFrame(Duration.minutes(1), e-> { 
      // do anything you need here on completion... 
      System.out.println("Minute over"); 
     }, new KeyValue(seconds, 60)) 
    ); 
    timeline.setCycleCount(Animation.INDEFINITE); 
    timeline.play(); 

여기에서의 포인트 IntegerProperty이 보간 것입니다 0에서 60 사이이지만 정수 값만 허용합니다 (즉 보간 값을 int으로 자릅니다).

import javafx.animation.Animation; 
import javafx.animation.KeyFrame; 
import javafx.animation.KeyValue; 
import javafx.animation.Timeline; 
import javafx.application.Application; 
import javafx.beans.property.IntegerProperty; 
import javafx.beans.property.SimpleIntegerProperty; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.ProgressBar; 
import javafx.scene.layout.StackPane; 
import javafx.stage.Stage; 
import javafx.util.Duration; 

public class OneMinuteTimer extends Application { 

    @Override 
    public void start(Stage primaryStage) { 
     ProgressBar progress = new ProgressBar(); 
     progress.setMinWidth(200); 
     progress.setMaxWidth(Double.MAX_VALUE); 
     IntegerProperty seconds = new SimpleIntegerProperty(); 
     progress.progressProperty().bind(seconds.divide(60.0)); 
     Timeline timeline = new Timeline(
      new KeyFrame(Duration.ZERO, new KeyValue(seconds, 0)), 
      new KeyFrame(Duration.minutes(1), e-> { 
       // do anything you need here on completion... 
       System.out.println("Minute over"); 
      }, new KeyValue(seconds, 60)) 
     ); 
     timeline.setCycleCount(Animation.INDEFINITE); 
     timeline.play(); 

     StackPane root = new StackPane(progress); 
     root.setPadding(new Insets(20)); 
     primaryStage.setScene(new Scene(root)); 
     primaryStage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 
: 여기

번째 버전으로 SSCCE 인
관련 문제