FXML加载异常没有指定资源

时间:2017-07-13 18:08:31

标签: java fxml scenebuilder

我开始使用SceneBuilder和JetBrains IDE构建一个程序,我想将程序国际化,所以我制作了一个属性文件。在SceneBuilder上一切正常,但是当我尝试用JetBrains运行程序时,我得到了这个异常

    /usr/lib/jvm/java-8-oracle/bin/java -Didea.launcher.port=7532 -Didea.launcher.bin.path=/home/paulo/idea-IC-163.12024.16/bin -Dfile.encoding=UTF-8 -classpath /usr/lib/jvm/java-8-oracle/jre/lib/charsets.jar:/usr/lib/jvm/java-8-oracle/jre/lib/deploy.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/cldrdata.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/jaccess.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/jfxrt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/nashorn.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunec.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-8-oracle/jre/lib/ext/zipfs.jar:/usr/lib/jvm/java-8-oracle/jre/lib/javaws.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jce.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jfr.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jfxswt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jsse.jar:/usr/lib/jvm/java-8-oracle/jre/lib/management-agent.jar:/usr/lib/jvm/java-8-oracle/jre/lib/plugin.jar:/usr/lib/jvm/java-8-oracle/jre/lib/resources.jar:/usr/lib/jvm/java-8-oracle/jre/lib/rt.jar:/home/paulo/mysql-connector-java-5.1.41/mysql-connector-java-5.1.41-bin.jar:/home/paulo/ProjetoOrcamentos/WorkManager/out/production/WorkManager:/home/paulo/idea-IC-163.12024.16/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain sample.Main
Exception in Application start method
Exception in thread "main" 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$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException: No resources specified.
/home/paulo/ProjetoOrcamentos/WorkManager/out/production/WorkManager/sample/sample.fxml:16

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
    at javafx.fxml.FXMLLoader.access$100(FXMLLoader.java:103)
    at javafx.fxml.FXMLLoader$Element.resolvePrefixedValue(FXMLLoader.java:421)
    at javafx.fxml.FXMLLoader$Element.processValue(FXMLLoader.java:363)
    at javafx.fxml.FXMLLoader$Element.processPropertyAttribute(FXMLLoader.java:325)
    at javafx.fxml.FXMLLoader$Element.processInstancePropertyAttributes(FXMLLoader.java:235)
    at javafx.fxml.FXMLLoader$ValueElement.processEndElement(FXMLLoader.java:767)
    at javafx.fxml.FXMLLoader.processEndElement(FXMLLoader.java:2823)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2532)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at sample.Main.start(Main.java:13)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$49(GtkApplication.java:139)
    ... 1 more

这是我到目前为止的代码

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setScene(new Scene(root, 1200, 1000));
        primaryStage.show();
    }


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

FXML文件:

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

<?import javafx.geometry.Insets?>
<?import javafx.scene.Cursor?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
   <top>
      <HBox spacing="20.0" BorderPane.alignment="CENTER">
         <children>
            <Button mnemonicParsing="false" text="%newWorkButton">
               <cursor>
                  <Cursor fx:constant="CLOSED_HAND" />
               </cursor></Button>
            <Button mnemonicParsing="false" text="%dbButton" />
         </children>
         <padding>
            <Insets bottom="15.0" left="15.0" right="15.0" top="15.0" />
         </padding>
         <BorderPane.margin>
            <Insets />
         </BorderPane.margin>
      </HBox>
   </top>
</BorderPane>

我知道在尝试加载属性文件时发生了问题,但为什么它在SceneBuilder中工作,当我尝试运行它时它不起作用?

PS:出现%newWorkButton%dbButton时会出现问题,因为这是使用属性文件的部分。

3 个答案:

答案 0 :(得分:4)

我正在回答自己的帖子,因为几天前我能够找到答案。 正如您在Main中看到的那样,我将此行加载fxml

Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"))

但是因为我正在使用属性文件,所以我也需要加载它,所以这就是我所做的:

 ResourceBundle resources = ResourceBundle.getBundle("Language/lang_pt");;

 Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"),resources);

答案 1 :(得分:0)

我有类似的问题。我刚刚发现这一点并意识到我忘记加载我的ResourceBundle并且我的应用程序正在爆炸i18n资源。我在这篇文章中找到了答案:

javaFX application error: No resources specified

答案 2 :(得分:0)

您必须将“/”替换为“。”像这样

~