我遇到的问题是eH只执行一次,我不确定是什么问题。我在时间轴上阅读了javadocs,但我没有看到我出错的地方。希望额外的一双眼睛可以看出出了什么问题。感谢。
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Demo extends Application {
GridPane gp = new GridPane();
Label label1 = new Label("Label 1");
Label label2 = new Label("Label 2");
Label label3 = new Label("Label 3");
Label label4 = new Label("Label 4");
Scene scene;
VBox vbox = new VBox();
VBox vbox2 = new VBox();
Image image = new Image("image.png");
ImageView iv = new ImageView();
Rectangle first = new Rectangle(0, 0, 50, 50);
Rectangle second = new Rectangle(0, 0, 50, 50);
Rectangle third = new Rectangle(0, 0, 50, 50);
Rectangle forth = new Rectangle(0, 0, 50, 50);
Button b1 = new Button("Enter1");
public void start(Stage primaryStage) {
vbox.setSpacing(10);
vbox.getChildren().addAll(label1, label2, label3, label4);
gp.add(vbox, 0, 0);
vbox2.getChildren().addAll(b1,new Button("Enter2"),
new Button("Enter3"),new Button("Enter4"),new Button("Enter5") );
gp.add(vbox2, 1, 0);
iv.setImage(image);
//iv.setFitWidth(700);
//iv.setFitHeight(300);
iv.setPreserveRatio(true);
iv.setSmooth(true);
iv.setCache(true);
gp.add(iv, 3, 0);
first.setFill(Color.BLACK);
second.setFill(Color.WHITE);
third.setFill(Color.WHITE);
forth.setFill(Color.BLACK);
gp.add(first, 4, 0);
gp.add(second, 5, 0);
gp.add(third, 4, 1);
gp.add(forth, 5, 1);
boolean x = false;
EventHandler<ActionEvent> eH = e -> {
if (x == false) {
first.setFill(Color.WHITE);
second.setFill(Color.BLACK);
third.setFill(Color.BLACK);
forth.setFill(Color.WHITE);
}
else {
first.setFill(Color.BLACK);
second.setFill(Color.WHITE);
third.setFill(Color.WHITE);
forth.setFill(Color.BLACK);
}
};
Timeline animation = new Timeline(new KeyFrame(Duration.millis(500), eH));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
b1.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
animation.stop();
animation.play();
}
});
scene = new Scene(gp, 700, 300);
primaryStage.setTitle("Lab 24");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
答案 0 :(得分:0)
动画继续播放......但它什么都不做。第一次x=false
执行一些更改,但x
始终为false,并且无需更改。
例如,如果您想创建一个后退并转发无限期效果,请执行以下操作:
boolean x = false;
@Override
public void start(Stage primaryStage) {
...
EventHandler<ActionEvent> eH = e -> {
if (x == false) {
first.setFill(Color.WHITE);
second.setFill(Color.BLACK);
third.setFill(Color.BLACK);
forth.setFill(Color.WHITE);
x=true;
}
else {
first.setFill(Color.BLACK);
second.setFill(Color.WHITE);
third.setFill(Color.WHITE);
forth.setFill(Color.BLACK);
x=false;
}
};
Timeline animation = new Timeline(new KeyFrame(Duration.millis(500), eH));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
或者,如果您只需要一个周期,请移除animation.setCycleCount(Timeline.INDEFINITE);
,每次点击按钮b1
时,您将获得一个周期。