调用新场景

hmae6n7t  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(344)

我正试图在progressbar完成时调用一个新场景。我曾试图在不关闭旧场景的情况下调用它,但现在我希望它打开新场景并关闭当前场景。

task.setOnSucceeded(evt ->{
            Parent root = null;
            try {
                root = FXMLLoader.load(getClass().getResource("indexapp.fxml"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            Scene scene = new Scene(root);
            Stage window =(Stage) ((Node) evt.getSource()).getScene().getWindow();
            window.setScene(scene);
            window.show();
        });

输出是这样的

Exception in Application start methodjava.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:832)Caused by: javafx.fxml.LoadException: /C:/Users/Given/IdeaProjects/Garbage_Collect_Sytem_Client/out/production/Garbage_Collect_Sytem/resource/applicationStart.fxml:8

at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2625)
at javafx.fxml/javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:105)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:941)
at javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:980)
at javafx.fxml/javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:227)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:752)
at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at resource.Main.start(Main.java:29)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more Caused by: java.lang.InstantiationException: resource.StartApp
at java.base/java.lang.Class.newInstance(Class.java:598)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:936)
... 22 more Caused by: java.lang.NoSuchMethodException: resource.StartApp.<init>()
at java.base/java.lang.Class.getConstructor0(Class.java:3427)
at java.base/java.lang.Class.newInstance(Class.java:585)
... 23 more Exception running application resource.Main
rbpvctlc

rbpvctlc1#

我就是这么做的,而且成功了

@Override
public void start(Stage primaryStage) throws Exception {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("applicationStart.fxml"));
    Parent root = loader.load();
    StartApp mainController = loader.getController();
    primaryStage.setScene(new Scene(root));
    primaryStage.show();

    Task<Void> task = new Task<>() {
        @Override
        protected Void call() throws Exception {
            File file = new File("C:\\Users\\Given\\IdeaProjects\\App\\src\\resource");

            for (int i = 0; i < listOfFile.length; i++) {
                updateProgress(i, listOfFile.length);
                updateMessage(listOfFile[i].getName());
                Thread.sleep(150);
            }
            return null;
        }
    };

    //The below code updates the message
    task.messageProperty().addListener(new ChangeListener<String>() {
        @Override
        public void changed(ObservableValue<? extends String> observableValue, String s, String t1) {
            mainController.getField().setText(t1 + "Reading...");
        }
    });
    task.setOnSucceeded(ev ->{
        FXMLLoader innerLoader = new FXMLLoader(getClass().getResource("indexapp.fxml"));
        try
        {
            Stage stage = new Stage();
            Parent innerRoot = innerLoader.load();
            stage.setScene(new Scene(innerRoot));
            stage.show();
            primaryStage.close();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    });
    mainController.getBar().progressProperty().unbind();
    mainController.getBar().progressProperty().bind(task.progressProperty());
    Thread th = new Thread(task);
    th.setDaemon(true);
    th.start();
}

相关问题