JavaFX:带有时间轴的ProgressBar;完成后将两者重置

时间:2018-06-22 14:43:42

标签: javafx progress-bar

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

时间到时,将显示下一个屏幕,并且时间轴将重置,但是进度条(在代码中显示为“ 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,并制作了一个新的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();
    });
}

2 个答案:

答案 0 :(得分:0)

您在With ActiveSheet.PageSetup .Orientation = xlPortrait .PrintArea = "$A$1:$L$47" 'Change this to your print area. .Zoom = False .FitToPagesTall = False .FitToPagesWide = 1 End With ActiveSheet.ExportAsFixedFormat _ Type:=xlTypePDF, _ Filename:=your_file_name_here, _ Quality:=xlQualityStandard, _ IncludeDocProperties:=False, _ IgnorePrintAreas:=False, _ From:=1, _ To:=3, _ 'This line is optional if you have multiple pages OpenAfterPublish:=False 通话中打了一个timeline.play()的电话。删除该呼叫,以使进度条不会重置为完整状态。

此外,您应该通过执行setOnFinished()

中的progressBar.setProgress(0.0F);来重置进度栏

答案 1 :(得分: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();
    }
}