我编写了以下代码,它起作用了。将fxml文件更改为resources文件夹后,它停止工作。我把它放回原来的地方,将无法使用。为什么?我这个问题几个月了。
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文件移动到另一个程序包,并且它停止工作。我将其移回原始位置,也无法使用。