このデモの完全なコード ( Gistとしても入手可能):
public class ShakingStage extends Application {
public Stage primaryStage;
boolean xyState= true;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
primaryStage= stage;
Button shakeButton= ButtonBuilder.create()
.text("shake it")
.onAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
TimelineBuilder.create()
.keyFrames(new KeyFrame(Duration.seconds(0.2), new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
if (xyState) {
primaryStage.setX(primaryStage.getX() + 10);
primaryStage.setY(primaryStage.getY() + 10);
} else {
primaryStage.setX(primaryStage.getX() - 10);
primaryStage.setY(primaryStage.getY() - 10);
}
xyState= !xyState;
}
}))
.autoReverse(true)
.cycleCount(Timeline.INDEFINITE)
.build().play();
}
})
.build();
Scene scene= SceneBuilder.create()
.root(StackPaneBuilder.create()
.children(shakeButton)
.build()
)
.width(300)
.height(400)
.build();
stage.setScene(scene);
stage.show();
}
}