And*_*rdi 5 java javafx fxml scenebuilder
我真的很绝望。\n我不\xe2\x80\x99不知道为什么,但每次我尝试运行我的程序时都会出现此错误。\n我正在使用 Netbeans 和 Java 10.0.2,以便已经安装了 JavaFX。\ n我认为\xe2\x80\x99s是由于FXML文件的一些问题造成的。\n你能帮助我吗?
\njava.lang.reflect.InvocationTargetException\n at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\n at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n at java.base/java.lang.reflect.Method.invoke(Method.java:564)\n at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:473)\n at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:372)\n at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\n at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n at java.base/java.lang.reflect.Method.invoke(Method.java:564)\n at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:941)\nCaused by: java.lang.RuntimeException: Exception in Application start method\n at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:973)\n at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)\n at java.base/java.lang.Thread.run(Thread.java:844)\nCaused by: javafx.fxml.LoadException: Root hasn't been set. Use method setRoot() before load.\nfile:/C:/Users/User/Documents/NetBeansProjects/gruppo71/dist/run2124011701/gruppo71.jar!/gruppo71/FXMLDocument.fxml:13\n\n at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2621)\n at javafx.fxml/javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:105)\n at javafx.fxml/javafx.fxml.FXMLLoader$RootElement.constructValue(FXMLLoader.java:1338)\n at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:754)\n at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2722)\n at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2552)\n at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)\n at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3253)\n at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3210)\n at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)\n at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)\n at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3129)\n at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3122)\n at gruppo71.Gruppo71.start(Gruppo71.java:21)\n at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:919)\n at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$11(PlatformImpl.java:449)\n at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)\n at java.base/java.security.AccessController.doPrivileged(Native Method)\n at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)\n at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)\n at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)\n at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)\n ... 1 more\nException running application gruppo71.Gruppo71\nJava Result: 1\nRun Code Online (Sandbox Code Playgroud)\n我的主要是这样的:
\n\nimport javafx.application.Application;\nimport javafx.fxml.FXMLLoader;\nimport javafx.scene.Parent;\nimport javafx.scene.Scene;\nimport javafx.stage.Stage;\n\n/**\n *\n * @author User\n */\npublic class Gruppo71 extends Application {\n \n @Override\n public void start(Stage stage) throws Exception {\n Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));\n \n Scene scene = new Scene(root);\n \n stage.setScene(scene);\n stage.show();\n }\n\n /**\n * @param args the command line arguments\n */\n public static void main(String[] args) {\n launch(args);\n }\n} \nRun Code Online (Sandbox Code Playgroud)\n我的 FXML 代码是:
\n\n<?import javafx.scene.control.Button?>\n<?import javafx.scene.control.ContextMenu?>\n<?import javafx.scene.control.DatePicker?>\n<?import javafx.scene.control.MenuItem?>\n<?import javafx.scene.control.SplitPane?>\n<?import javafx.scene.control.TableColumn?>\n<?import javafx.scene.control.TableView?>\n<?import javafx.scene.control.TextField?>\n<?import javafx.scene.layout.AnchorPane?>\n\n<fx:root id="AnchorPane" prefHeight="300.0" prefWidth="450.0" type="AnchorPane" xmlns="http://javafx.com/javafx/10.0.2" xmlns:fx="http://javafx.com/fxml/1" fx:controller="gruppo71.FXMLDocumentController">\n <children>\n <SplitPane dividerPositions="0.13087248322147652" layoutX="28.0" orientation="VERTICAL" prefHeight="200.0" prefWidth="320.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">\n <items>\n <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">\n <children>\n <Button fx:id="aggiungiButton" layoutX="10.0" layoutY="5.0" mnemonicParsing="false" onAction="#addEvento" text="Aggiungi Evento" AnchorPane.bottomAnchor="5.0" AnchorPane.leftAnchor="10.0" AnchorPane.topAnchor="5.0" />\n <DatePicker fx:id="dataPicker" layoutX="114.0" layoutY="6.0" prefHeight="26.0" prefWidth="150.0" AnchorPane.bottomAnchor="5.0" AnchorPane.leftAnchor="124.0" AnchorPane.topAnchor="5.0" />\n <TextField fx:id="descriptionField" layoutX="285.0" layoutY="6.0" text="aggiungi descrizione..." AnchorPane.bottomAnchor="5.0" AnchorPane.leftAnchor="284.0" AnchorPane.rightAnchor="10.0" AnchorPane.topAnchor="5.0" />\n </children>\n </AnchorPane>\n <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">\n <children>\n <TableView fx:id="mainTab" layoutX="14.0" prefHeight="256.0" prefWidth="448.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">\n <columns>\n <TableColumn fx:id="dataColumn" prefWidth="136.0" text="Data" />\n <TableColumn fx:id="eventColumn" prefWidth="311.0" text="Evento" />\n </columns>\n <contextMenu>\n <ContextMenu>\n <items>\n <MenuItem fx:id="removeButton" mnemonicParsing="false" onAction="#removeEvent" text="Rimuovi Evento" />\n <MenuItem fx:id="importaButton" mnemonicParsing="false" onAction="#importCSV" text="Importa CSV" />\n <MenuItem fx:id="exportButton" mnemonicParsing="false" onAction="#exportCSV" text="Esporta CSV" />\n </items>\n </ContextMenu>\n </contextMenu>\n </TableView>\n </children>\n </AnchorPane>\n </items>\n </SplitPane>\n </children>\n</fx:root>\nRun Code Online (Sandbox Code Playgroud)\n所有帮助将不胜感激!
\n只是将其标记为已回答:请参阅@James_D'评论以获取解决方案。
<fx:root>在 FXML 文件中替换为或在调用之前<AnchorPane>设置根目录,例如如下所示:FXMLLoaderload()
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("FXMLDocument.fxml"));
fxmlLoader.setRoot(new AnchorPane());
Parent root = fxmlLoader.load();
Run Code Online (Sandbox Code Playgroud)
另请参阅: 设置 root 时的 JavaFx FXML 加载文件问题