要完成该任务,您可以在 100 毫秒后删除矩形。与 Flash 不同,JavaFX 不是基于关键帧构建的——它们是可选功能,仅在您需要真正的动画时使用,例如对象的缩放。有关更多信息,请参阅下一个教程:http: //docs.oracle.com/javafx/2/animations/jfxpub-animations.htm
并演示代码:
public void start(Stage primaryStage) {
final Rectangle rect1 = new Rectangle(10, 70, 50, 50);
final Rectangle rect2 = new Rectangle(10, 150, 50, 50);
Button btn = new Button("Play");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
// this "timeline" just call a handler after 500 ms which hides rectangle
TimelineBuilder.create().keyFrames(new KeyFrame(Duration.millis(500), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
rect1.setVisible(false);
}
})).build().play();
// this timeline hides rectangle 2 with animation
// changing scaleXProperty() from 1 (default) to 0
TimelineBuilder.create().keyFrames(
new KeyFrame(
Duration.millis(500),
new KeyValue(rect2.scaleXProperty(), 0))
).build().play();
}
});
Pane root = new Pane();
root.getChildren().addAll(rect1, rect2, btn);
Scene scene = new Scene(root, 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
}