我有这个,它工作正常:
private Timeline heartbeat;
@Override
public void initialize(URL location, ResourceBundle resources) {
startHeartbeat();
}
private void startHeartbeat() {
heartbeat = new Timeline(new KeyFrame(Duration.seconds(5), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("beep");
}
}));
heartbeat.setCycleCount(Timeline.INDEFINITE);
heartbeat.play();
}
但是,当视图关闭时,线程仍然存在。我在哪里放置代码以终止线程?