JavaFX动画在第一个循环后停止
我想要一个计数到3的计数器,然后它应该在0重新开始,无限期地重复计数直到3。 但是当我的计数器达到3时,我的程序所做的最后一件事就是设置文本JavaFX动画在第一个循环后停止,java,animation,javafx,Java,Animation,Javafx,我想要一个计数到3的计数器,然后它应该在0重新开始,无限期地重复计数直到3。 但是当我的计数器达到3时,我的程序所做的最后一件事就是设置文本“0”。 我想我没有正确使用动画。 我希望有人知道我做错了什么。 注意,这只是我真正问题的简化;也许有更简单的方法数到3 import javafx.animation.Animation; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.a
“0”
。
我想我没有正确使用动画。
我希望有人知道我做错了什么。
注意,这只是我真正问题的简化;也许有更简单的方法数到3
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;
public class GeneralTesting extends Application{
private Text text;
private int counter = 1;
public static void main(String[] args) {
launch();
}
@Override
public void start(Stage primaryStage) throws Exception {
Pane pane = new Pane();
text = new Text(500, 300, "0");
text.setFont(Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 100));
Animation animation = new Timeline(new KeyFrame(Duration.millis(1000), e -> {changeText(counter++);}));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
pane.getChildren().addAll(text);
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();
}
public void changeText(int counter){
if (counter > 5){
counter = 0;
}
text.setText(String.valueOf(counter));
}
}
一种方法是使用mod将范围保持在0到3之间
Pane pane = new Pane();
Text text = new Text(500, 300, "0");
int[] counter =
{
0
};
text.setFont(Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 100));
Animation animation = new Timeline(new KeyFrame(Duration.millis(1000), e
->
{
text.setText(Integer.toString(counter[0]++ % 4));
}));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
pane.getChildren().addAll(text);
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();
在
您正在将局部变量计数器
重置为0
。“实例”字段保持其以前的值。因此,在时间轴的下一次迭代中,它仍然大于5,并且您再次将局部变量设置为零
完全删除局部变量,只需更新字段:
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;
public class GeneralTesting extends Application{
private Text text;
private int counter = 1;
public static void main(String[] args) {
launch();
}
@Override
public void start(Stage primaryStage) throws Exception {
Pane pane = new Pane();
text = new Text(500, 300, "0");
text.setFont(Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 100));
Animation animation = new Timeline(new KeyFrame(Duration.millis(1000), e -> {
counter++;
changeText();
}));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
pane.getChildren().addAll(text);
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();
}
public void changeText(){
if (counter > 5){
counter = 0;
}
text.setText(String.valueOf(counter));
}
}
更好的方法是使用IntegerProperty
并将文本绑定到其值:
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;
public class GeneralTesting extends Application{
private Text text;
public static void main(String[] args) {
launch();
}
@Override
public void start(Stage primaryStage) throws Exception {
Pane pane = new Pane();
text = new Text(500, 300, "0");
text.setFont(Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 100));
IntegerProperty counter = new SimpleIntegerProperty(1);
text.textProperty().bind(counter.asString());
Animation animation = new Timeline(new KeyFrame(Duration.millis(1000),
e -> counter.set((counter.get()+1) % 5)));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
pane.getChildren().addAll(text);
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();
}
}
局部变量的第一件事正是我要找的!非常感谢你!
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;
public class GeneralTesting extends Application{
private Text text;
public static void main(String[] args) {
launch();
}
@Override
public void start(Stage primaryStage) throws Exception {
Pane pane = new Pane();
text = new Text(500, 300, "0");
text.setFont(Font.font("Comic Sans MS", FontWeight.BOLD, FontPosture.REGULAR, 100));
IntegerProperty counter = new SimpleIntegerProperty(1);
text.textProperty().bind(counter.asString());
Animation animation = new Timeline(new KeyFrame(Duration.millis(1000),
e -> counter.set((counter.get()+1) % 5)));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
pane.getChildren().addAll(text);
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();
}
}