java - 为什么 fxml loader 无缘无故停止工作
问题描述
我编写了以下代码并且它有效。在我将 fxml 文件更改为资源文件夹后,它停止工作。我把它放回原来的地方,它不会工作。为什么?我有这个问题好几个月了。
package com.opensoftware.helptec.ui;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class HomepageApplication extends Application {
public static void main(String[] args) {
Application.launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("src/main/resources/fxml/homepage.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}
FXML 文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" prefHeight="400.0" prefWidth="600.0"
xmlns="http://javafx.com/javafx/8.0.172-ea" fx:controller="com.opensoftware.helptec.ui.HomeUiController">
<Button fx:id="button" layoutX="268.0" layoutY="300.0" mnemonicParsing="false" onAction="#onClick"
text="Click Me!"/>
<Label layoutX="274.0" layoutY="116.0" text="Welcome!"/>
<Label layoutX="206.0" layoutY="183.0" text="Press the button below to continue!"/>
</AnchorPane>
编辑:什么停止工作:应用程序将无法启动,因为 fxmlloader 推出了 NPE。
我尝试将 fxml 文件的路径更改为几种不同的格式:
"../resources/fxml/homepage.fxml"
"src/main/resources/fxml/homepage.fxml"
"../fxml/homepage.fxml"
"../homepage.fxml"
我没有得到的是,我现在使用的代码完全相同。然后我尝试将 fxml 文件移动到另一个包,它停止工作。我把它移回原来的地方,它也不起作用。