我想在对话窗口中显示项目文件夹中保存的图像,但是当我运行showDialogWithImage方法时,出现了FileNotFoundExcpetion: imgs\pic1.jpg(系统找不到指定的文件),尽管图像位于那里。
我尝试过这种方式来加载图片:
Image image = new Image(getClass().getResourceAsStream(path));,但是遇到了同样的问题。
还有其他可能的方法来将图像加载到ImageView吗?
谢谢您的帮助!
我的Java代码位于项目文件夹中的src\myProject\gui中。
path="imgs\pic1.jpg" // imgs位于项目文件夹中
public void showDialogWithImage(String path) {
final Stage dialogStage = new Stage();
logger.info(path);
InputStream is = null;
try {
is = new FileInputStream(path); // here I get FileNotFoundException
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Image image = new Image(is);
ImageView view = new ImageView();
view.setImage(image);
Button btnOK = new Button("OK");
btnOK.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
dialogStage.close();
}
});
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.setScene(new Scene(VBoxBuilder.create()
.children(view, btnOK).alignment(Pos.CENTER)
.padding(new Insets(35)).build()));
dialogStage.show();
}