我有一个应用鼠标输入动作监听器和一个鼠标退出监听器按钮。当鼠标悬停在按钮上时,它旋转并退出它假设停止并返回其原始位置但不幸的是我不喜欢#39;知道如何实现这个......任何想法?这就是我所拥有的。
finishButton.setOnMouseEntered(event -> {
buttonRotateAnimation(finishButton);
play();
});
finishButton.setOnMouseExited(event ->{
stop();
});
创建方法
public RotateTransition buttonRotateAnimation(Button button){
rotateTransition = new RotateTransition(Duration.seconds(2),button);
rotateTransition.setFromAngle(0);
rotateTransition.setToAngle(720);
rotateTransition.setCycleCount(Timeline.INDEFINITE);
rotateTransition.setAutoReverse(true);
return rotateTransition;
}
public void play() {
rotateTransition.play();
}
public void stop() {
rotateTransition.stop();
}
答案 0 :(得分:3)
您可以将ChangeListener添加到statusProperty,当状态为Status.STOPPED时,您只需添加另一个RotateTransition,如下所示:
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
StackPane root = new StackPane();
Button button = new Button( "I'm rotating!");
RotateTransition rotateTransition = createRotateTransition(button);
button.addEventFilter(MouseEvent.MOUSE_ENTERED, e -> {
rotateTransition.play();
});
button.addEventFilter(MouseEvent.MOUSE_EXITED, e -> {
rotateTransition.stop();
});
root.getChildren().add( button);
Scene scene = new Scene(root,400,400);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public RotateTransition createRotateTransition(Button button){
RotateTransition rotateTransition = new RotateTransition(Duration.seconds(2),button);
rotateTransition.setFromAngle(0);
rotateTransition.setToAngle(720);
rotateTransition.setCycleCount(Timeline.INDEFINITE);
rotateTransition.setAutoReverse(true);
rotateTransition.statusProperty().addListener(new ChangeListener<Status>() {
@Override
public void changed(ObservableValue<? extends Status> observable, Status oldValue, Status newValue) {
if( newValue == Status.STOPPED) {
RotateTransition transition = new RotateTransition(Duration.seconds(2),button);
transition.setFromAngle(button.getRotate());
transition.setToAngle(0);
transition.setCycleCount(1);
transition.setAutoReverse(true);
transition.play();
}
}
});
return rotateTransition;
}
public static void main(String[] args) {
launch(args);
}
}