在ProgressBar加载到JavaFX后,如何打开新屏幕?

时间:2018-10-24 04:00:44

标签: java javafx progress-bar

这是来自Main.java的what gets launched

这是Main.java

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Main extends Application 
{
    public static Stage mainStage;
    ProgressBar pb;

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

    @Override
    public void start(Stage primaryStage) throws Exception 
    {
        VBox root = new VBox();
        root.getStyleClass().add("root");
        root.setAlignment(Pos.CENTER);
        root.setSpacing(20);
        Scene scene = new Scene(root,1024,768);

        ImageView appleIcon = new ImageView("apple.png");

        DropShadow ds = new DropShadow();
        ds.setRadius(5);
        ds.setOffsetX(8);
        ds.setOffsetY(8);
        ds.setColor(Color.BLACK);

        appleIcon.setEffect(ds);

        pb = new ProgressBar();
        pb.setMinSize(400, 20);

        //This line is for CSS Class selectors
        //This line sets a class name for progress bar which I used css file
        pb.getStyleClass().add("red-bar");
        pb.setProgress(0);

        Timeline timeline = new Timeline();
        KeyValue keyValue = new KeyValue(pb.progressProperty(),1);
        KeyFrame keyFrame = new KeyFrame(new Duration(3000),keyValue);
        timeline.getKeyFrames().add(keyFrame);
        timeline.play();

//      if(pb.getProgress() == 1)
//      {
//          mainStage = primaryStage;
//          mainStage.setScene(new loginScene());
//          mainStage.show();
//      }


        root.getChildren().addAll(appleIcon,pb);
        root.getStylesheets().add(getClass().getResource("progress.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

我要在进度条完全加载时启动另一个屏幕。

这是loginScene.java,它调用了loginPane的构造函数

import javafx.scene.Scene;

public class loginScene extends Scene 
{
    public loginScene()
    {
        super(new loginPane(),1000,700);
    }
}

这是loginPane.java。这是进度栏加载后要启动的屏幕。

import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class loginPane extends Pane
{
    public Stage mainStage;

    public loginPane()
    {
        VBox root = new VBox();
        Scene scene = new Scene(root,1024,768);

        Button bt = new Button("OK");

        root.getChildren().add(bt);
        mainStage = Main.mainStage;
        mainStage.setScene(scene);
        mainStage.show();

    }
}

我尝试了ProgressBar的bind()属性,但是没有用。

谢谢

1 个答案:

答案 0 :(得分:0)

向KeyFrame添加onFinished处理程序。

EventHandler<ActionEvent> onFinished = e->{
   mainStage = primaryStage;
   mainStage.setScene(new loginScene());
   mainStage.show();
};
KeyFrame keyFrame = new KeyFrame(new Duration(3000), onFinished ,keyValue);

需要对LoginPane类进行一些小的修改。

class LoginPane extends Pane    {
        public LoginPane()        {
            VBox root = new VBox();
            Button bt = new Button("OK");
            root.getChildren().add(bt);
            getChildren().add(root);
        }
    }