JavaFX:带时间轴的ProgressBar;如果完成则重置两者

问题描述 投票:0回答:2

我正在构建一个考试应用程序。所有问题都显示在不同的屏幕上,所有问题都有一个进度条,显示用户离开的时间。当进度条已满时,将显示另一个问题的屏幕,并且应重置进度条和时间线。

当时间到了,显示下一个屏幕并且时间线重置,但进度条(在代码中显示为'PRGB')保持已满,并且它不会重置为空的进度条。这是我的代码:

public void timeBar() throws Exception{
    Timeline timeline = new Timeline(
            new KeyFrame(Duration.ONE, new KeyValue(PRGB.progressProperty(), 0)),
            new KeyFrame(Duration.seconds(Main.time), e-> {
            }, new KeyValue(PRGB.progressProperty(), 1))
    );
    timeline.setCycleCount(1);
    timeline.play();
    timeline.setOnFinished(event -> {
        Main.setPane(questionNumber);
        questionNumber++;
        timeline.play();
        //in here the progressbar has to be resetted
    });
}

更新:我已经从SceneBuilder中删除了我的ProgressBar,并创建了一个新的,然后我运行下面的代码(根据您的建议进行一些更改)。但现在进度条确实重置了它自己,但它没有显示进度:它保持空白。

public void timeBar() throws Exception{
    Timeline timeline = new Timeline(
            //I only use two keyframes instead of three
            new KeyFrame(Duration.seconds(Main.time), e-> {
            }, new KeyValue(PRGB.progressProperty(), 1))
    );
    timeline.setCycleCount(1);
    timeline.play();
    timeline.setOnFinished(event -> {
        Main.setPane(questionNumber);
        questionNumber++;
        //The setProgress is added
        PRGB.setProgress(0.0F);
        timeline.play();
    });
}
javafx progress-bar
2个回答
0
投票

你在timeline.play()电话中打电话给setOnFinished()。删除该调用,以便进度条不会重置为完整。

此外,您应该通过在progressBar.setProgress(0.0F);中执行setOnFinished()来重置进度条


0
投票

这可能会做得更好。在这个例子中,每个问题给出3秒,每个问题结束时1秒,以加载下一个问题。来自here的代码。

import java.util.concurrent.atomic.AtomicInteger;
import javafx.animation.KeyFrame;
import javafx.animation.PauseTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Main extends Application
{

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

    @Override
    public void start(Stage stage)
    {
        Label lblCurrentQuestion = new Label();
        ProgressBar prgb = new ProgressBar(0);

        int numberOfQuestions = 5;//Number of questions
        int timeForEachQuestion = 3;//3 second to do each questions
        AtomicInteger currentProgressCounter = new AtomicInteger(0);
        AtomicInteger currentQuestionCounter = new AtomicInteger(1);

        Timeline fiveSecondsWonder = new Timeline(new KeyFrame(Duration.seconds(1), (event) -> {
            if (currentProgressCounter.get() == timeForEachQuestion + 1) {
                //Go to next question!
                prgb.setProgress(0);
                currentProgressCounter.set(0);
                currentQuestionCounter.incrementAndGet();
            }
            lblCurrentQuestion.setText("Question " + currentQuestionCounter.get());
            prgb.setProgress(currentProgressCounter.getAndIncrement() / (double) timeForEachQuestion);
        }));

        fiveSecondsWonder.setCycleCount(numberOfQuestions * (timeForEachQuestion + 1));
        fiveSecondsWonder.play();
        fiveSecondsWonder.setOnFinished(event -> {
            PauseTransition wait = new PauseTransition(Duration.seconds(1));
            wait.setOnFinished((e) -> {
                /*YOUR METHOD*/
                lblCurrentQuestion.setText("Quiz done!");
                prgb.setProgress(0);
                wait.playFromStart();
            });
            wait.play();
        });

        Scene scene = new Scene(new StackPane(new VBox(lblCurrentQuestion, prgb)), 500, 500);

        stage.setScene(scene);
        stage.show();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.