javafx时间轴行为怪异

jdgnovmf  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(215)

我正在尝试制作一个javafx应用程序,在这个应用程序中,文本将显示在屏幕上(使用动画),就像有人在键入文本一样,然后您必须在给定的字段中键入文本,它将计算您的错误。但是当我在使用停止按钮停止时间轴动画后运行它时,它一次打印两个字母,而不是一个。当我重新启动它时,它一次打印3个字母,以此类推。有人能调查一下吗?
这是代码(非常混乱,所以,请不要生气)

private int counter = 0;
    private Scanner scanner;
    private String str = "DaVinci made detailed drawings of human anatomy," +
            " which are still highly regarded today. DaVinci wrote notebook entries in mirror" +
            " (backwards) script, a trick that kept many of his observations from being widely" +
            " known until decades after his death. It is believed that he was hiding his scientific" +
            " ideas from the powerful Roman Catholic Church, whose teachings sometimes disagreed with" +
            " what Leonardo observed.";
    private String expectedStr;
    private Timeline timeline = new Timeline();
    private TextArea textArea = new TextArea();
    private HBox hBox = new HBox();
    private final IntegerProperty i = new SimpleIntegerProperty(0);
    private final Button button = new Button("Start");
    private final Button button1 = new Button("Stop");
    private final Button button2 = new Button("Increase");
    private final Button button3 = new Button("Decrease");
    private BorderPane borderPane = new BorderPane();
    private HBox hBox1 = new HBox(10);
    private HBox hBox2 = new HBox();
    private TextField textField = new TextField();

    void startAnimation() {
        expectedStr = scanner.nextLine();
        KeyFrame keyFrame = new KeyFrame(
                Duration.millis(380),
                event -> {
                    if (i.get() > str.length()) {
                        stopAnimation();
                    } else {
                        textArea.insertText(i.get(), (String.valueOf(str.charAt(i.get()))));
                        i.set(i.get() + 1);
                    }
                });
        timeline.getKeyFrames().add(keyFrame);
        timeline.setCycleCount(Animation.INDEFINITE);
        timeline.play();
    }

    void stopAnimation() {
        i.set(0);
        textArea.setText("");
        timeline.stop();
    }

    @Override
    public void start(Stage primaryStage) {
        i.set(0);
        textField.setPrefWidth(320);
        hBox.getChildren().add(textArea);
        hBox.setAlignment(Pos.TOP_LEFT);
        hBox2.getChildren().add(textField);
        hBox2.setAlignment(Pos.CENTER);
        textArea.setWrapText(true);
        hBox1.setAlignment(Pos.CENTER);
        hBox1.getChildren().addAll(button2, button, button1, button3);
        borderPane.setTop(hBox);
        borderPane.setBottom(hBox1);
        borderPane.setCenter(hBox2);
        button.setOnAction(actionEvent -> {
            if (timeline.getStatus() != Animation.Status.RUNNING) {
                startAnimation();
            }
        });
        button1.setOnAction(actionEvent -> {
            if (timeline.getStatus() == Animation.Status.RUNNING) {
                stopAnimation();
            }
        });
        button2.setOnAction(actionEvent -> timeline.setRate(timeline.getRate() + .1));
        button3.setOnAction(actionEvent -> timeline.setRate(timeline.getRate() - .1));
        textField.textProperty().addListener((observableValue) -> {
            if (timeline.getStatus() != Animation.Status.PAUSED &&
                    textField.getText().length() != 0 && textField.getText().length() < str.length()) {
                System.out.println(mistakeCounter());
            }
        });
        Scene scene = new Scene(borderPane, 350, 350);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public int mistakeCounter() {
        char s1 = textField.getText().charAt(textField.getText().length() - 1);
        char[] s2 = str.toCharArray();
        if (s1 != s2[textField.getText().length() - 1]) {
            counter++;
        }
        return counter;
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}```

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题