简体   繁体   English

如何从 JLabel ImageIcon 获取绝对路径?

[英]How to get the absolute path from an JLabel ImageIcon?

I am creating an Photo and Video Viewer.我正在创建一个照片和视频查看器。 Already made the video part, now I am making the photo part.已经制作了视频部分,现在我正在制作照片部分。

Trying to get the absloute path, for something like this:试图获得绝对路径,例如:

图片

And then it deletes.然后它会删除。 I'm using a JLabel for displaying.我正在使用 JLabel 进行显示。 What i tried was:我尝试的是:

System.out.println("Deleting file");

ImageIcon img = (ImageIcon) showDisplay.getIcon();
System.out.println(img.toString());

File file = new File(img.toString());
int response = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete the file " + file.getName() + "?");
if(response == JOptionPane.YES_OPTION) {
    deleteFile(file);
    //the thing that deleteFile has is file.delete();
}

But this returns javax.swing.ImageIcon@6aa93db7 in the console, not the absolute path.但这会在控制台中返回 javax.swing.ImageIcon@6aa93db7,而不是绝对路径。

Can someone help me here?有人可以在这里帮助我吗?

You can use the setDescription(...) and getDescription(...) methods for this.您可以为此使用setDescription(...)getDescription(...)方法。

Depending on your you load the Icon this may already contain the file path.根据您加载的图标,这可能已经包含文件路径。

If not, then you can manually set this value yourself when you create the image and create the Icon.如果没有,那么您可以在创建图像和创建图标时自己手动设置此值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM