JavaFX构建成功但GUI程序无显示问题求助
JavaFX FXML应用构建成功但GUI无法显示,FXML加载提示文件提前结束
在NetBeans上开发JavaFX FXML应用,使用Java 8和JavaFX SDK 20,构建输出显示BUILD SUCCESSFUL,但GUI程序无法在屏幕上显示。此前因Font Awesome相关报错添加了对应库,现在出现新问题,构建日志中抛出FXML加载异常,提示FXML文件提前结束。
构建日志
ant -f C:\Users\youse\hotelManagementProject jfxsa-runjfxsa-run:Buildfile: C:\Users\youse\hotelManagementProject\build.xml -mark-project-state-running: -check-jfxrt-in-fxsdk-jre: -set-jfxrt-in-fxsdk-jre-old: -set-jfxrt-in-fxsdk-jre-new: -check-jfxrt-in-fxsdk-rt: -set-jfxrt-in-fxsdk-rt-old: -set-jfxrt-in-fxsdk-rt-new: -check-jfxrt-in-platform-home-jre: -set-jfxrt-in-platform-home-jre-old: -set-jfxrt-in-platform-home-jre-new: -check-jfxrt-in-platform-home-rt: -set-jfxrt-in-platform-home-rt-old: -set-jfxrt-in-platform-home-rt-new: -check-jfxrt-modules-in-platform-home: -set-jfxrt-modules-in-platform-home: -check-jfxrt-in-jre: -set-jfxrt-in-jre-old: -set-jfxrt-in-jre-new: -set-jfxrt-in-jre-mods: -check-jfxrt-in-envjdk-jre: -set-jfxrt-in-envjdk-jre-old: -set-jfxrt-in-envjdk-jre-new: -check-jfxrt-modules-in-envjdk: -set-jfxrt-modules-in-envjdk: -pre-check-jfx-runtime: -check-jfx-runtime: -check-project: -warn-of-preloader: -post-init: -init-check: -init-ap-cmdline-properties: -init-macrodef-modulename: -init-source-module-properties: -init-macrodef-javac-with-module: -init-macrodef-javac-with-processors: -init-macrodef-javac-without-processors: -init-macrodef-javac: -init-macrodef-test-impl: -init-macrodef-junit-init: -init-test-properties: -init-macrodef-junit-prototype-with-module: -init-macrodef-junit-prototype-without-module: -init-macrodef-junit-single: -init-macrodef-junit-batch: -init-macrodef-junit: -init-macrodef-junit-impl: -init-macrodef-testng: -init-macrodef-testng-impl: -init-macrodef-test: -init-macrodef-junit-debug-impl: -init-macrodef-test-debug-junit: -init-macrodef-testng-debug: -init-macrodef-testng-debug-impl: -init-macrodef-test-debug-testng: -init-macrodef-test-debug: -init-debug-args: -init-macrodef-nbjpda: -init-macrodef-debug: -init-macrodef-java-with-module: -init-macrodef-java-with-unnamed-module: -init-macrodef-java-without-module: -init-macrodef-java: -init-presetdef-jar: -init-ap-cmdline-supported: -init-ap-cmdline: init: -deps-jar-init:[delete] Deleting: C:\Users\youse\hotelManagementProject\build\built-jar.properties deps-jar: -warn-already-built-jar:[propertyfile] Updating property file: C:\Users\youse\hotelManagementProject\build\built-jar.properties -check-automatic-build: -clean-after-automatic-build: -verify-automatic-build: -pre-pre-compile: -pre-compile: -copy-persistence-xml: -compile-depend: -do-compile: -post-compile: compile: -pre-jar: -check-module-main-class: -set-module-main-class: -pre-pre-jar: -do-jar-create-manifest: -do-jar-copy-manifest: -do-jar-set-mainclass: -do-jar-set-profile: -do-jar-set-splashscreen: -check-do-mkdist: -do-jar-jar: -init-macrodef-copylibs: -do-jar-copylibs: -do-jar-delete-manifest: -do-jar-without-libraries: -do-jar-with-libraries: -jfx-copylibs-warning: -jfx-copylibs:[delete] Deleting directory C:\Users\youse\hotelManagementProject\dist\lib[copy] Copying 1 file to C:\Users\youse\hotelManagementProject\dist\lib -check-rebase-libs: -rebase-libs-warning: -rebase-libs: -check-ant-javafx-in-fxsdk-lib: -set-ant-javafx-in-fxsdk-lib: -check-ant-javafx-in-fxsdk-tools: -set-ant-javafx-in-fxsdk-tools: -check-ant-javafx-in-platform-home-lib: -set-ant-javafx-in-platform-home-lib: -check-ant-javafx-in-platform-home-tools: -set-ant-javafx-in-platform-home-tools: -check-ant-javafx-in-probjdk-lib: -set-ant-javafx-in-probjdk-lib: -check-ant-javafx-in-probjdk-tools: -set-ant-javafx-in-probjdk-tools: -check-ant-javafx-in-macjdk-lib: -set-ant-javafx-in-macjdk-lib: -check-ant-javafx-in-envjdk-lib: -set-ant-javafx-in-envjdk-lib: -check-ant-javafx-in-envjdk-tools: -set-ant-javafx-in-envjdk-tools: -pre-check-ant-javafx-version: -check-endorsed-javafx-ant-classpath: -set-endorsed-javafx-ant-classpath: -set-javafx-ant-classpath: -check-ant-javafx-version: -check-jfx-sdk-version-old: -check-jfx-sdk-version-new: -check-jfx-sdk-version:[echo] Detected JavaFX Ant API version 1.3 -check-jfx-deployment: -check-dist-lib-exists: -check-jfx-deployment-jar-current-nolib: -check-jfx-deployment-jar-current-lib: -check-jfx-deployment-launch: -do-jfx-deployment-script: -do-jfx-deployment-noscript: jfx-deployment: -post-jar: -do-jar: -pre-deploy: -check-jlink: -do-deploy: -post-deploy: deploy: jar: -check-concurrent-runs: -create-temp-run-dir: [copy] Copying 4 files to C:\Users\youse\hotelManagementProject\dist\run218081809 jfx-project-run: [echo] Executing C:\Users\youse\hotelManagementProject\dist\run218081809\hotelManagementProject.jar using platform C:\Users\youse\Downloads\zulu8.68.0.21-ca-fx-jdk8.0.362-win_x64\zulu8.68.0.21-ca-fx-jdk8.0.362-win_x64/bin/java [java] Exception in Application start method [java] java.lang.reflect.InvocationTargetException [java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [java] at java.lang.reflect.Method.invoke(Method.java:498) [java] at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) [java] at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) [java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[java] at java.lang.reflect.Method.invoke(Method.java:498) [java] at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:873)[java] Caused by: java.lang.RuntimeException: Exception in Application start method [java] at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)[java] at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182) [java] at java.lang.Thread.run(Thread.java:750)[java] Caused by: javafx.fxml.LoadException:[java] file:/C:/Users/youse/hotelManagementProject/dist/run218081809/hotelManagementProject.jar!/hotelmanagementproject/FXMLDocument.fxml [java] [java] at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2608) [java] at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2550) [java] at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2448) [java] at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3251) [java] at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3212) [java] at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3185) [java] at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3161) [java] at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3141) [java] at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3134) [java] at hotelmanagementproject.HotelManagementProject.start(HotelManagementProject.java:21)[java] at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863) [java] at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)[java] at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295) [java] at java.security.AccessController.doPrivileged(Native Method)[java] at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294) [java] at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)[java] at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) [java] at com.sun.glass.ui.win.WinApplication.lambda$null$3(WinApplication.java:178) [java] ... 1 more[java] Caused by: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1][java] Message: Premature end of file.[java] at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:604)[java] at javax.xml.stream.util.StreamReaderDelegate.next(StreamReaderDelegate.java:88) [java] at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2520)[java] ... 17 more [java] Exception running application hotelmanagementproject.HotelManagementProject [java] Java Result: 1 -remove-temp-run-dir: [delete] Deleting directory C:\Users\youse\hotelManagementProject\dist\run218081809 jfxsa-run-no-another-jvm: BUILD SUCCESSFUL Total time: 2 seconds BUILD SUCCESSFUL (total time: 3 seconds)
主Java文件
package hotelmanagementproject; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; public class HotelManagementProject extends Application { @Override public void start(Stage stage) throws Exception { Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml")); Scene scene = new Scene(root); stage.setScene(scene); stage.show(); } /** * @param args 命令行参数 */ public static void main(String[] args) { launch(args); } }
CSS文件
.white-bg{-fx-background-color:#fff;} .left-form{-fx-background-color:linear-gradient(to bottom right, #686c89, #4f4c63);} .textfield{-fx-background-color:transparent;-fx-border-color:#4f4c63;-fx-border-width:0px 0px .8px 0px;-fx-font-size:13px;-fx-font-family:"Tahoma";} .textfield:focused{-fx-border-color:linear-gradient(to bottom right, #686c89, #4f4c63);-fx-border-width:0px 0px 1.5px 0px;} .login-btn{-fx-background-color:linear-gradient(to bottom right, ffffff, #565d91);-fx-text-fill:#fff;-fx-font-size:13px;-fx-cursor:hand;} .login-btn:hover{-fx-background-color:linear-gradient(to bottom right, ffffff, #2b346e);} .close{-fx-background-color:transparent;-fx-cursor:hand;} .close:hover{-fx-background-color:#da0404;}
FXMLDocumentController.java文件
/* * 点击 nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt 修改许可证 * 点击 nbfs://nbhost/SystemFileSystem/Templates/javafx/FXML2.java 编辑此模板 */ package hotelmanagementproject; import java.net.URL; import java.util.ResourceBundle; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Label; /** * @author youse */ public class FXMLDocumentController implements Initializable { @FXML private Label label; @FXML private void handleButtonAction(ActionEvent event) { System.out.println("你点击了我!"); label.setText("你好,世界!"); } @Override public void initialize(URL url, ResourceBundle rb) { // 待实现 } }
问题分析与解决方法
问题根源
报错信息javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1] Message: Premature end of file明确指向FXML文件为空或者格式损坏,导致XML解析器无法读取有效内容。
解决步骤
- 检查FXML文件完整性
- 找到项目中的
FXMLDocument.fxml文件,确认文件不为空,且XML结构完整(包含正确的根节点,比如<AnchorPane>,所有标签正常闭合)。 - 若文件为空,重新创建FXML文件或从备份恢复。
- 找到项目中的
- 验证FXML文件路径
- 确认
FXMLDocument.fxml与主类HotelManagementProject.java在同一包hotelmanagementproject下,NetBeans会自动将FXML文件打包到Jar包对应路径。 - 可临时添加日志输出资源路径,确认资源是否被正确找到:
System.out.println("FXML资源路径:" + getClass().getResource("FXMLDocument.fxml"));
- 确认
- 检查构建过程中的文件复制
- 打开生成的
hotelManagementProject.jar,检查hotelmanagementproject目录下是否存在FXMLDocument.fxml且内容正常。 - 查看NetBeans项目属性,确认FXML文件被设置为“复制到输出目录”。
- 打开生成的
- 版本兼容性优化
- Java 8与JavaFX 20存在跨版本兼容性风险,建议使用与Java 8匹配的JavaFX 8 SDK,避免API差异导致的隐性问题。
内容的提问来源于stack exchange,提问作者Yousef Khalil
相关产品推荐
相关产品推荐

