我正在尝试使用JavaFX和IntelliJ创建一个照片浏览器程序,并为一个按钮创建了一个actionEvent,该按钮可以在点击时更改照片。
如果我的Image构造函数如下所示只有一个字符串文件路径它可以正常工作(尝试#1),但是当我尝试使用目录和存储在字符串数组中的文件名的组合时(尝试#2) )或带有文件路径的字符串数组(尝试#3)它不起作用。当我尝试打印下面放在Image构造函数中的输出时,尝试#2打印构造函数中的文本#1(/Thumbnails/IMG_0004.jpg)并尝试#3打印完整文件路径image(/ Users / Amrit / Google Drive / Computer Science / Mini IA / PhotoBrowser / src / Imported / IMG_0004.jpg)
尝试#1
public void changeRight(ActionEvent actionEvent) {
image1.setImage(new Image("/Thumbnails/IMG_0004.jpg"));
}
尝试#2
public void changeRight(ActionEvent actionEvent) {
image1.setImage(new Image("/Thumbnails/" + fileNameArray[3]));
}
尝试#3
public void changeRight(ActionEvent actionEvent) {
image1.setImage(new Image(stringFilePathArray[3]));
}
此changeRight方法位于一个控制器类中,该控制器类链接到我使用JavaFX Scene Builder 2.0创建的名为sample.fxml的cxml文件。所有图像都存储在目录中的是项目文件夹(PhotoBrowser):" /PhotoBrowser/src/Thumbnails/ImageName.jpg"
对于所有这些我得到一个空指针异常,并不确定为什么或如何解决它。
下面是fxml文件中ImageView的代码,它位于GridPane的子代中:
<ImageView id="imgView1" fx:id="image1" fitHeight="195.0" fitWidth="300.0" onMouseClicked="#imgClick" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" GridPane.halignment="CENTER" GridPane.rowIndex="1" GridPane.valignment="CENTER">
<image>
<Image url="@../Thumbnails/IMG_0001.jpg" />
</image></ImageView>
答案 0 :(得分:1)
这是解决方案。对于您的路径,请使用file:Thumbnails/IMG_0004.jpg
。
实际上,您必须使用文件:前缀。