我正在使用Intelij Idea在JAVAFX中尝试一个简单的项目,所以我遇到了两个问题:
案例1:
当我将sample.fxml
和controller.java
以及main.java
放在同一文件夹src/main/java/FX/
中时:
1- SceneBuilder找到controller.java
2- Main.java找不到FXML文件
Parent root = FXMLLoader.load(getClass().getResource("/FX/sample.fxml"));
/opt/java/jdk1.8.0_20/bin/java -Didea.launcher.port=7537 -Didea.launcher.bin.path=/opt/idea-IU-143.381.42/bin -Dfile.encoding=UTF-8 -classpath /opt/java/jdk1.8.0_20/jre/lib/charsets.jar:/opt/java/jdk1.8.0_20/jre/lib/deploy.jar:/opt/java/jdk1.8.0_20/jre/lib/ext/cldrdata.jar:/opt/java/jdk1.8.0_20/jre/lib/ext/dnsns.jar:/opt/java/jdk1.8.0_20/jre/lib/ext/jfxrt.jar:/opt/java/jdk1.8.0_20/jre/lib/ext/localedata.jar:/opt/java/jdk1.8.0_20/jre/lib/ext/nashorn.jar:/opt/java/jdk1.8.0_20/jre/lib/ext/sunec.jar:/opt/java/jdk1.8.0_20/jre/lib/ext/sunjce_provider.jar:/opt/java/jdk1.8.0_20/jre/lib/ext/sunpkcs11.jar:/opt/java/jdk1.8.0_20/jre/lib/ext/zipfs.jar:/opt/java/jdk1.8.0_20/jre/lib/javaws.jar:/opt/java/jdk1.8.0_20/jre/lib/jce.jar:/opt/java/jdk1.8.0_20/jre/lib/jfr.jar:/opt/java/jdk1.8.0_20/jre/lib/jfxswt.jar:/opt/java/jdk1.8.0_20/jre/lib/jsse.jar:/opt/java/jdk1.8.0_20/jre/lib/management-agent.jar:/opt/java/jdk1.8.0_20/jre/lib/plugin.jar:/opt/java/jdk1.8.0_20/jre/lib/resources.jar:/opt/java/jdk1.8.0_20/jre/lib/rt.jar:/home/azb/IdeaProjects/TestMavenFX/target/classes:/opt/idea-IU-143.381.42/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain FX.Main
** (java:7837): WARNING **: Couldn't connect to accessibility bus: Failed to connect to socket /tmp/dbus-MBNrjIF7ES: Connection refused
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:875)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
at com.sun.javafx.application.LauncherImpl$$Lambda$1/424058530.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3201)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3169)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3142)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3118)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3098)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3091)
at FX.Main.start(Main.java:13)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$55/669200515.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$51/645218408.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$53/222734631.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$52/444279218.run(Unknown Source)
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$45(GtkApplication.java:126)
at com.sun.glass.ui.gtk.GtkApplication$$Lambda$43/1060865646.run(Unknown Source)
... 1 more
Process finished with exit code 1
案例2:
当我将sample.fxml
放入文件夹src/main/resources/FX/
中的文件夹controller.java
和main.java
以及src/main/java/FX/
时:
1- SceneBuilder找不到controller.java
2- Main.java找到FXML文件,程序运行
问题:这两种情况哪里出错了?巫婆cas是假的好人吗?请帮忙
由于
答案 0 :(得分:1)
在fxml文件中定义控制器类:
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" id="GridPane" prefHeight="400.0" prefWidth="600.0" styleClass="mainFxmlClass" fx:controller="yourpackage.yourControllerClass">
<children>
...
</children>
</AnchorPane>
答案 1 :(得分:1)
根据这个答案,唯一的问题是放在你的 FXML 文件所在的文件夹中,问题来自 scenebuilder 它有一个限制。
请点击以下链接了解更多详情