如何在将 img 文件设置为 JLabel 之前验证它是否存在,以防清理并构建为 JAR 文件?

How to verify a img file existed before setIcon it to JLabel in case clean and build to JAR file?

我正在设计一个 Java 程序来在点击相应按钮时将图标设置为 JLabel。我想知道在调用之前检查 img 是否存在的方法:

ImageIcon myIcon = new ImageIcon(getClass().getClassLoader().getResource(path));

不允许我使用 try catch!

如果提供的路径中不存在图像,则从 getResource(..) 返回的 URL 将为 null