我有一個應用程序具有帶有一些雙精度值的組合框。用戶可以選擇任何值。該應用程序附有一個「TimeLine」,它將在控制檯上打印語句。下面是sscce。應該發生的是,應該打印從組合框中選擇的選項的文本。請建議。JavaFX:將時間軸的持續時間綁定到屬性
package just.to.test;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.stage.Stage;
import javafx.util.Duration;
public class TimerSample extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Text Fonts");
Group g = new Group();
Scene scene = new Scene(g, 150, 100);
ObservableList<Double> data = FXCollections.observableArrayList();
data.add(5.0);
data.add(10.0);
data.add(15.0);
data.add(20.0);
ComboBox<Double> timeOptions = new ComboBox<Double>(data);
timeOptions.getSelectionModel().selectFirst();
g.getChildren().addAll(timeOptions);
primaryStage.setScene(scene);
primaryStage.show();
final double timerInterval = timeOptions.getSelectionModel().getSelectedItem();
KeyFrame keyFrame = new KeyFrame(Duration.seconds(timerInterval),
new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("This is called every "
+ timerInterval + " seconds");
}
});
Timeline timerThread = new Timeline(keyFrame);
timerThread.setCycleCount(Timeline.INDEFINITE);
timerThread.play();
}
}
的偉大工程。謝謝。但只是想知道性能是否明智,因爲隨着組合框值的每次變化都添加一個新的關鍵幀。 – Aspirant
根據其他問題更新了答案。 – jewelsea
'rateProperty()'不會更清潔嗎? – Mordechai