javafx 找到正确的路径 [英] javafx find the right path
问题描述
我得到以下结构:
<块引用>项目|---+构建|---+距离|---+nbproject|----src|-------客户|-----------东西|---------控制器|----------------DefaultController.java|------文件|-------------fxml|----------------默认FXML.fxml|------img|-------------lib|---------------ContentManager.java|---------根|---------------StartClass.Java|---+build.xml|---manifest.mf
我在 ContentManager 中设置了 fxml 文件,例如:
public static final StringDEFAULT_SCREEN_FXML = "../fxml/Default.fxml";
但我总是得到一个 nullPointerException.而且我不想使用绝对路径.那么如何找出相对路径呢?是否正确设置 fxml 文件的控制器:
fx:id="client.stuff.controller.DefaultController"
??
部署应用程序时,通常会创建一个 jar 文件,其中包含类文件和所有资源.您需要从 jar 文件内部加载 fxml 文件,即作为资源.在这种情况下,路径元素.."实际上没有任何意义.
使用
public static final StringDEFAULT_SCREEN_FXML = "/client/stuff/fxml/Default.fxml";
(路径是相对于类路径的).
当然加载它
FXMLLoader loader = new FXMLLoader(getClass().getResource(DEFAULT_SCREEN_FXML));
或类似的东西.
I get following structure:
project |---+build |---+dist |---+nbproject |----src |-------client |----------stuff |-------------controller |----------------DefaultController.java |-------------files |-------------fxml |----------------DefaultFXML.fxml |-------------img |-------------lib |---------------ContentManager.java |-------------root |---------------StartClass.Java |---+build.xml |---manifest.mf
I'm setting in the ContentManager the fxml-Files like:
public static final String
DEFAULT_SCREEN_FXML = "../fxml/Default.fxml";
But i get allways a nullPointerException. And i dont want to work with absolute Path. So how to finde out the relative path? And is it right to set the controller of the fxml-file with:
fx:id="client.stuff.controller.DefaultController"
??
When you deploy your application, you typically create a jar file which includes the class files and all the resources. You need to load the fxml file from inside the jar file, i.e. as a resource. In this context, the path element ".." doesn't really mean anything.
Use
public static final String
DEFAULT_SCREEN_FXML = "/client/stuff/fxml/Default.fxml";
(the path is relative to the classpath).
and of course load it with
FXMLLoader loader = new FXMLLoader(getClass().getResource(DEFAULT_SCREEN_FXML));
or something similar.
这篇关于javafx 找到正确的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!