2017-03-14 3 views
0

Ich möchte einen Zähler, der bis 3 zählt, und danach sollte es bei 0 neu starten und wiederholt bis 3 endlos. Aber wenn mein Zählerstand 3 erreicht, setzt mein Programm zuletzt den Text "0". Ich denke, ich verwende Animation nicht richtig. Ich hoffe jemand weiß was ich falsch mache. Beachten Sie, dass dies nur eine Vereinfachung meines wirklichen Problems ist; könnte es einfachere Wege bis 3 ...JavaFX Animation stoppt nach dem ersten Zyklus

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)); 
    } 

} 

Antwort

1

In

if (counter > 5) { 
    counter = 0 ; 
} 

halten Sie setzen die lokale Variable counter auf 0 zurück. Das Instanzfeld bleibt auf dem vorherigen Wert. Folglich ist es bei der nächsten Iteration der Zeitleiste immer noch größer als 5 und Sie setzen die lokale Variable wieder auf Null.

die lokalen Variable entfernen vollständig und aktualisieren Sie das Feld:

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)); 
    } 

} 

besser, noch ein IntegerProperty verwenden und den Text, um seinen Wert zu binden:

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(); 
    } 


} 
+0

Das erste, was mit den lokalen Variablen war genau das, was ich gesucht habe! Ich danke dir sehr! – jaaBeg16

0

Eine Möglichkeit ist, zu zählen sein mod zu verwenden, um den Bereich von 0 bis 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(); 
Verwandte Themen