[[oktatas:programozás:java:java_fx|< Java FX]]
====== JavaFX - Időzítés ======
* **Szerző:** Sallai András
* Copyright (c) 2025, Sallai András
* Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC BY-SA 4.0]]
* Web: https://szit.hu
===== Másodpercenként =====
package com.example;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.util.Duration;
public class MainController {
@FXML
void onClickStartButton(ActionEvent event) {
KeyFrame kf = new KeyFrame(Duration.seconds(1), e -> {
System.out.println("idő van");
});
Timeline tm = new Timeline();
tm.getKeyFrames().add(kf);
tm.setCycleCount(Timeline.INDEFINITE);
tm.play();
}
}
Megadható hányszor egész számként vagy végtelenszer:
tm.setCycleCount(Timeline.INDEFINITE);
===== Téglalap mozgatása =====
package com.example;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
public class MainController {
@FXML
private Pane panel;
Rectangle rec = new Rectangle(0, 0, 100, 100);
KeyValue xValue = new KeyValue(rec.xProperty(), 200);
KeyValue yValue = new KeyValue(rec.yProperty(), 200);
KeyFrame frame = new KeyFrame(javafx.util.Duration.millis(1000), xValue, yValue);
@FXML
void initialize() {
rec.setFill(Color.BLUE);
}
@FXML
void onClickStartButton(ActionEvent event) {
System.out.println("indul...");
panel.getChildren().add(rec);
Timeline line = new Timeline();
line.getKeyFrames().add(frame);
line.setCycleCount(3);
line.setAutoReverse(true);
line.play();
}
}