JavaFX不会将我发送到下一个场景

时间:2016-03-08 13:13:08

标签: javafx

我希望在Timer之后移动到下一阶段,但是当我运行页面时,它会被冻结。

我想要一些帮助来解决这个问题,我是JavaFX的新手。

我想在我选择的特定时间之后转到下一个屏幕,因为此屏幕是加载屏幕

这是我的代码:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package chapitreapp;

import ressources.FadeInLeftTransition;
import ressources.FadeInRightTransition;
import ressources.FadeInTransition;
import ressources.config;
import ressources.config2;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.concurrent.TimeUnit;
import javafx.application.Platform;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import org.springframework.beans.*;
import org.springframework.context.*;


/**
 * FXML Controller class
 *
 * @author anas
 */
public class LoadController implements Initializable {
    @FXML
    private Text lblWelcome;
    @FXML
    private Text lblRudy;
    @FXML
    private VBox vboxBottom;
    @FXML
    private Label lblClose;
    Stage stage;
    @FXML
    private ImageView imgLoading;
    /**
     * Initializes the controller class.
     * @param url
     * @param rb
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        longStart();
        lblClose.setOnMouseClicked((MouseEvent event) -> {
            Platform.exit();
            System.exit(0);
        });
        // TODO

    }   

    private void longStart() {
        Service<ApplicationContext> service = new Service<ApplicationContext>() {
            @Override
            protected Task<ApplicationContext> createTask() {
                return new Task<ApplicationContext>() {           
                    @Override
                    protected ApplicationContext call() throws Exception {
                        ApplicationContext appContex = config.getInstance().getApplicationContext();
                        int max = appContex.getBeanDefinitionCount();
                        updateProgress(0, max);
                        for (int k = 0; k < max; k++) {
                          try
                          {
                                Thread.sleep(50);
                             updateProgress(k+1, max);

                           }
                           catch(InterruptedException e)
                           {
                                    System.exit(0);
                           }
                        }
                        return appContex;
                    }
                };
            }
        };

        service.start();
        service.setOnRunning((WorkerStateEvent event) -> {
            new FadeInLeftTransition(lblWelcome).play();
            new FadeInRightTransition(lblRudy).play();
            new FadeInTransition(vboxBottom).play();
        });
        service.setOnSucceeded((WorkerStateEvent event) -> {

            config2 config = new config2();
            config.newStage(stage, lblClose, "/chapitreapp/FXMLDocument.fxml", "Sample Apps", true, StageStyle.UNDECORATED, false);
        });
    } 
}

我哪里出错?

1 个答案:

答案 0 :(得分:0)

这些行中的内容,onSucceeded事件处理程序或它调用的方法......

    try {
        URL url = this.getClass().getResource("nextScene.fxml");
        if (url != null) {
            FXMLLoader loader = new FXMLLoader(url);
            Parent root = loader.load(url);
            Scene newScene = new Scene(root);
            this.getStage().setScene(newScene);
        } else {
            System.out.println("URL was null");
        }
    } catch (Exception e) {
        System.out.println(e);
    }

在你的控制器中,你需要有一些设置阶段的方法,可以通过接受Stage对象的构造函数或通过setStage()方法。我已经使用上面的getStage()方法来访问舞台并在其上设置新的Scene对象。

您还可以使用静态FXMLLoader加载方法:Parent root = FXMLLoader.load(url);

这也可能有益:https://blogs.oracle.com/acaicedo/entry/managing_multiple_screens_in_javafx1