我偶然发现了一个非常奇怪的行为(在下面的代码中演示 - 只需复制粘贴,您就可以亲眼目睹它了!)。当按下播放按钮时..主菜单应该消失并且选项菜单应该出现。按下“返回”按钮,主菜单再次出现!现在的问题:再次按下播放按钮..菜单没有按预期消失,但它立即消失,然后选项菜单按预期出现..换句话说,第二次触发动画时它没有正确渲染! !
出于演示目的,我添加了一个“选项”按钮,它与播放按钮完全相同,但动画略有不同(它不是淡出主菜单而是缩小它)。此动画始终正常工作。
是否有任何 javaFX 2 专家能够解释这种异端行为?这是一个错误..还是我错过了什么?
先感谢您!这是代码..
package strangeFx;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application{
@Override
public void start(Stage stage) throws Exception {
Scene scene = new Scene(new StartScreen());
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
和主窗口类
package strangeFx;
import javafx.animation.*;
import javafx.event.*;
import javafx.scene.Group;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.util.Duration;
import cls.island.utils.TimeLineExt;
public class StartScreen extends Group {
private static final double ANIM_DURATION = 200D;
VBox mainBtns = new VBox();
VBox optionBtns = new VBox();
/**
* Initializes two group of buttons. the "main" group and "options" group.
* Initially only the menu group is displayed. By pressing the "options" button
* the menu group will be removed and replaced by the options with a small animation!
* When the "play" button is pressed the menu group will be removed and replaced by
* the options button, using a different animation
*
*/
public StartScreen() {
mainBtns.setFillWidth(true);
createOptionButtonGroup();
createStartButton(mainBtns);
createoptionsButton(mainBtns);
createQuitButton(mainBtns);
getChildren().add(mainBtns);
}
private void createOptionButtonGroup() {
Button button = new Button("Back");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
goToMain();
}
});
button.setMaxWidth(Double.MAX_VALUE);
optionBtns.getChildren().add(button);
}
private void createStartButton(Pane buttonGroup) {
Button button = new Button("Play");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
fadeAndGoToOptions();
}
});
button.setMaxWidth(Double.MAX_VALUE);
buttonGroup.getChildren().add(button);
}
private void createQuitButton(Pane buttonGroup) {
Button button = new Button("Quit");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.exit(0);
}
});
button.setMaxWidth(Double.MAX_VALUE);
buttonGroup.getChildren().add(button);
}
private void createoptionsButton(Pane buttonGroup) {
Button button = new Button("Options");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
goToOptions();
}
});
button.setMaxWidth(Double.MAX_VALUE);
buttonGroup.getChildren().add(button);
}
public void goToOptions() {
Timeline timeline = new Timeline();
timeline.getKeyFrames().add(new KeyFrame(new Duration(ANIM_DURATION), new KeyValue(mainBtns.scaleXProperty(), 0),
new KeyValue(mainBtns.scaleYProperty(),0)));
timeline.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
StartScreen.this.getChildren().remove(mainBtns);
mainBtns.scaleXProperty().set(1);
mainBtns.scaleYProperty().set(1);
Timeline timeline = new Timeline();
optionBtns.scaleXProperty().set(0);
optionBtns.scaleYProperty().set(0);
StartScreen.this.getChildren().add(optionBtns);
timeline.getKeyFrames().add(new KeyFrame(new Duration(ANIM_DURATION), new KeyValue(optionBtns.scaleXProperty(), 1),
new KeyValue(optionBtns.scaleYProperty(),1)));
timeline.play();
}
});
timeline.play();
}
public void goToMain() {
TimeLineExt timeline = new TimeLineExt();
timeline.getKeyFrames().add(new KeyFrame(new Duration(ANIM_DURATION), new KeyValue(optionBtns.scaleXProperty(), 0),
new KeyValue(optionBtns.scaleYProperty(),0)));
timeline.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
StartScreen.this.getChildren().remove(optionBtns);
optionBtns.scaleXProperty().set(1);
optionBtns.scaleYProperty().set(1);
Timeline timeline = new Timeline();
mainBtns.scaleXProperty().set(0);
mainBtns.scaleYProperty().set(0);
StartScreen.this.getChildren().add(mainBtns);
timeline.getKeyFrames().add(new KeyFrame(new Duration(ANIM_DURATION), new KeyValue(mainBtns.scaleXProperty(), 1),
new KeyValue(mainBtns.scaleYProperty(),1)));
timeline.play();
}
});
timeline.play();
}
/**
* This is where the problematic behavior occurs, but only when this animation is triggered
* for second time! The first time this animation runs and animates properly!
* The correct animation is to 1.
*/
public void fadeAndGoToOptions() {
Timeline timeline = new Timeline();
timeline.getKeyFrames().add(new KeyFrame(new Duration(1000), new KeyValue(this.opacityProperty(),0)));
timeline.setOnFinished(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
getChildren().remove(mainBtns);
StartScreen.this.opacityProperty().set(100);
Timeline timeline = new Timeline();
optionBtns.scaleXProperty().set(0);
optionBtns.scaleYProperty().set(0);
StartScreen.this.getChildren().add(optionBtns);
timeline.getKeyFrames().add(new KeyFrame(new Duration(ANIM_DURATION), new KeyValue(optionBtns.scaleXProperty(), 1),
new KeyValue(optionBtns.scaleYProperty(),1)));
timeline.play();
}
});
timeline.play();
}
}