![](/img/trans.png)
[英]In javafx loading FXML file from another package and error appear?
[英]Javafx location is required (loading FXML file from another folder/package)
我在將Java與FXML文件一起使用時遇到問題。
我現在搜索了幾個小時,卻找不到能解決我問題的任何東西,所以我的最后希望是針對我的具體情況提出問題(我知道像這樣的問題和其他問題,但在這方面沒有一個真正的幫助我。
簡單說明:我有一個Eclipse Java Project,並且我的(對於這個問題很重要)類位於軟件包[Project Name] /src/measurements.gui中。 我的FXML文件在[項目名稱] /資源文件夾中。
加載FXML文件ElementsProperties.java的類如下所示:
import java.io.IOException;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.Window;
public class ElementsProperties {
public static void show(Window parent, String title) {
ElementsProperties el = new ElementsProperties();
BorderPane root = el.loadFXMLFile("resources/TestWindow.fxml");
Stage dialog = new Stage();
dialog.initOwner(parent);
dialog.setTitle(title);
dialog.initModality(Modality.WINDOW_MODAL);
dialog.setScene(new Scene(root));
dialog.show();
}
@SuppressWarnings({ "finally", "static-access" })
private BorderPane loadFXMLFile(String filePath) {
BorderPane borderPane = null;
try {
borderPane = new BorderPane();
FXMLLoader fxmlLoader = new FXMLLoader();
Parent content = fxmlLoader.load(getClass().getResource(filePath));
borderPane.setCenter(content);
}
catch (IOException e) {
e.printStackTrace();
System.err.println("Couldn't find the specified file");
}
catch(Exception e){
e.printStackTrace();
}
finally {
return borderPane;
}
}}
FXML文件顯示為帶有以下簡單行的對話框: ElementsProperties.show(parent, "TestWindow");
我的FXML文件(使用JavaFXSceneBuilder 2.0創建)如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.effect.*?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>
<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<MenuBar VBox.vgrow="NEVER">
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="New" />
<MenuItem mnemonicParsing="false" text="Open…" />
<Menu mnemonicParsing="false" text="Open Recent" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Close" />
<MenuItem mnemonicParsing="false" text="Save" />
<MenuItem mnemonicParsing="false" text="Save As…" />
<MenuItem mnemonicParsing="false" text="Revert" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Preferences…" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Quit" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Undo" />
<MenuItem mnemonicParsing="false" text="Redo" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Cut" />
<MenuItem mnemonicParsing="false" text="Copy" />
<MenuItem mnemonicParsing="false" text="Paste" />
<MenuItem mnemonicParsing="false" text="Delete" />
<SeparatorMenuItem mnemonicParsing="false" />
<MenuItem mnemonicParsing="false" text="Select All" />
<MenuItem mnemonicParsing="false" text="Unselect All" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About MyHelloApp" />
</items>
</Menu>
</menus>
</MenuBar>
<AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
<children>
<Label alignment="CENTER" layoutX="155.0" layoutY="177.0" style=" " text="Drag components from Library here…" textAlignment="CENTER" textFill="#9f9f9f" wrapText="false">
<font>
<Font size="18.0" />
</font>
</Label>
<GridPane layoutX="-2.0" layoutY="-3.0" prefHeight="407.0" prefWidth="659.0">
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<HBox prefHeight="134.0" prefWidth="654.0" spacing="20.0" GridPane.rowIndex="1">
<children>
<Button mnemonicParsing="false" prefHeight="46.0" prefWidth="60.0" text="Click me" wrapText="true" />
<Button mnemonicParsing="false" prefHeight="47.0" prefWidth="73.0" text="No, click me" wrapText="true" />
<Button mnemonicParsing="false" prefHeight="46.0" prefWidth="120.0" text="Don't you dare click me" wrapText="true" />
</children>
<padding>
<Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
</padding>
</HBox>
<TextField promptText="Type something here..." GridPane.columnIndex="1" GridPane.rowIndex="1">
<GridPane.margin>
<Insets left="20.0" right="20.0" />
</GridPane.margin>
</TextField>
</children>
</GridPane>
</children>
</AnchorPane>
</children>
</VBox>
實際上,這就是所需要的。 如果我嘗試運行程序並顯示對話框,則異常
java.lang.NullPointerException: Location is required.
給出。 如果我將FXML文件移動到與主類相同的包中,即Measurements.gui,並將ELementsProperties.java類的show-method中的filePath更改為"TestWindow.fxml"
,那么一切正常,我看到創建的我的應用程序中的窗口。 但我想將fxml文件放在單獨的資源文件夾中,以便於插入其他fxml文件。
希望我能清楚地解釋我的問題,並且您可以幫助我解決這個問題。 關於如何從不同於主類的包中加載fxml文件的任何想法? 順便說一句,我已經嘗試過的事情是:
getClass().getClassLoader().getResource(filePath)
作為FXMLLoader的加載方法的參數 先感謝您。
感謝Roland提供的鏈接。 我試圖復制文件夾結構,並提出了以下建議(適用於可能存在相同問題的其他人):
我創建了一個測試項目JavaFXTest。 我在org.example.main包中有一個主類。 像往常一樣,該軟件包位於項目的src文件夾中。
MyJavaFXDialog.java:
package org.example.main;
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class MyJavaFXDialog extends Application{
public void start(Stage primaryStage) {
MyJavaFXDialog javaFx = new MyJavaFXDialog();
BorderPane root = javaFx.loadFXMLFile("/resources/TestWindow.fxml");
Stage dialog = new Stage();
dialog.initModality(Modality.WINDOW_MODAL);
dialog.setScene(new Scene(root));
dialog.show();
}
@SuppressWarnings({ "finally", "static-access" })
private BorderPane loadFXMLFile(String filePath) {
BorderPane borderPane = null;
try {
borderPane = new BorderPane();
FXMLLoader fxmlLoader = new FXMLLoader();
Parent content = fxmlLoader.load(getClass().getResource(filePath));
borderPane.setCenter(content);
}
catch (IOException e) {
e.printStackTrace();
System.err.println("Couldn't find the specified file");
}
catch(Exception e){
e.printStackTrace();
}
finally {
return borderPane;
}
}
public static void main(String[] args){
launch(args);
}
}
我在src文件夾中創建了一個普通文件夾“ resources”。 該資源文件夾僅包含我的TestWindow.fxml文件,該文件與上述問題完全相同。 現在,當我啟動應用程序時,找到了fxml文件並將其顯示為主窗口。
因此,解決方案實際上是將資源文件夾移至 src文件夾,對於我的情況而言,這仍然是可行的解決方案。 我只是不希望最終用戶不得不在其他軟件包的最深處四處摸索以保存自己的fxml文件。
再次感謝Roland向我指出該解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.