运行带控制器和FXML的JAVAFX gui时出错

时间:2017-12-27 18:10:35

标签: java javafx fxml

当我尝试运行GUI时,我从Eclipse中收到错误说明:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: Location is not set.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
    at ui.MainController.<init>(MainController.java:22)
    at ui.Main.start(Main.java:18)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$5(GtkApplication.java:139)
    ... 1 more
Exception running application ui.Main    

尝试了很多变化和不同的实现(主要变化与FXML文件中的fx:root标签有关)但似乎没有任何效果,主要是:

package ui;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        // just load fxml file and display it in the stage:
        Parent root = new MainController();
        //FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
        //Parent root = loader.load();
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

控制器:

package ui;

import java.io.IOException;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.VBox;

public class MainController extends VBox {
    @FXML private IOTabController IOTabController;
    /*
    @FXML private void initialize() {
        IOTabController.injectMainController(this);
    }
     */

    public MainController() {
        try {
            FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("main.fxml"));
            fxmlLoader.setRoot(this);
            fxmlLoader.setController(this);
            fxmlLoader.load();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

和FXML文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>


<fx:root type="VBox" fx:id="Main" xmlns="http://javafx.com/javafx/9"
    xmlns:fx="http://javafx.com/fxml/1" fx:controller="ui.MainController">
    <children>
        <TabPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
            minWidth="-Infinity" prefHeight="527.0" prefWidth="651.0"
            tabClosingPolicy="UNAVAILABLE">
            <tabs>
                <Tab text="Information">
                    <content>
                        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0"
                            prefWidth="200.0">
                            <children>
                                <TextArea layoutX="14.0" layoutY="21.0" prefHeight="200.0"
                                    prefWidth="624.0" text="&#10;Instructions:" />
                            </children>
                        </AnchorPane>
                    </content>
                </Tab>
                <Tab text="Input / Output">
                    <content>
                        <fx:include fx:id="iOTab" source="IOTab.fxml" />
                    </content>
                </Tab>
                <Tab text="Filters">
                    <content>
                        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0"
                            prefWidth="200.0" />
                    </content>
                </Tab>
                <Tab text="Algorithms">
                    <content>
                        <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0"
                            prefWidth="200.0" />
                    </content>
                </Tab>
            </tabs>
        </TabPane>
    </children>
</fx:root>

我真的无法理解我在这里做错了什么。

0 个答案:

没有答案