从 FXML 加载图像和原生打包
Image loading from FXML and native packaging
当导出为 osx.
的本机包时,我在从 fxml 文件加载选项卡面板(按钮等)的图像时遇到一些问题
此代码在从 eclipse 启动时工作正常,但在将应用程序导出为本机包时所有图像都消失了。
<BorderPane stylesheets="@../my.css" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.view.MainController">
<center>
<TabPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab fx:id="projectsTab" closable="false">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0">
<children>
<fx:include source="main/ProjectView.fxml" />
</children>
</AnchorPane>
</content>
<graphic>
<ImageView fitHeight="30.0" fitWidth="30.0" opacity="0.5" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../resources/images/ic_list_black_48dp_1x.png" />
</image>
</ImageView>
</graphic>
</Tab>
使用此代码,在导出为本机包时也可以工作,但我想以 fxml 方式进行。
private void initialize() {
projectsTab.setGraphic(createTabImage("/images/ic_list_black_48dp_2x.png"));
}
private ImageView createTabImage(String pathToImage) {
ImageView imgView = new ImageView(new Image(this.getClass().getResource(pathToImage).toExternalForm()));
imgView.setFitWidth(30);
imgView.setFitHeight(30);
imgView.setOpacity(0.5);
return imgView;
}
有什么建议吗?
在大多数 IDE 设置中,resources
文件夹本身不会在项目编译时导出,但其内容会部署到类路径的根目录中。您没有在问题中显示 FXML 文件与图像的关系,但您可能需要
url="@../../images/ic_list_black_48dp_1x.png"
当导出为 osx.
的本机包时,我在从 fxml 文件加载选项卡面板(按钮等)的图像时遇到一些问题此代码在从 eclipse 启动时工作正常,但在将应用程序导出为本机包时所有图像都消失了。
<BorderPane stylesheets="@../my.css" xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1" fx:controller="app.view.MainController">
<center>
<TabPane maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab fx:id="projectsTab" closable="false">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0">
<children>
<fx:include source="main/ProjectView.fxml" />
</children>
</AnchorPane>
</content>
<graphic>
<ImageView fitHeight="30.0" fitWidth="30.0" opacity="0.5" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../../resources/images/ic_list_black_48dp_1x.png" />
</image>
</ImageView>
</graphic>
</Tab>
使用此代码,在导出为本机包时也可以工作,但我想以 fxml 方式进行。
private void initialize() {
projectsTab.setGraphic(createTabImage("/images/ic_list_black_48dp_2x.png"));
}
private ImageView createTabImage(String pathToImage) {
ImageView imgView = new ImageView(new Image(this.getClass().getResource(pathToImage).toExternalForm()));
imgView.setFitWidth(30);
imgView.setFitHeight(30);
imgView.setOpacity(0.5);
return imgView;
}
有什么建议吗?
在大多数 IDE 设置中,resources
文件夹本身不会在项目编译时导出,但其内容会部署到类路径的根目录中。您没有在问题中显示 FXML 文件与图像的关系,但您可能需要
url="@../../images/ic_list_black_48dp_1x.png"