我有一个应用程序具有带有一些双精度值的组合框。用户可以选择任何值。该应用程序附有一个“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