【发布时间】:2014-05-09 18:30:51
【问题描述】:
我想在对话框窗口中显示图像(保存在项目文件夹中),但是当我运行我的方法 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();
}
【问题讨论】: