创建可运行的JAR文件后,ImageIcon丢失 [英] ImageIcon lost after creating a runnable JAR file

查看:223
本文介绍了创建可运行的JAR文件后,ImageIcon丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

项目当我在Eclipse中单击播放时,运行正常,但在创建了可运行的JAR文件之后,按钮的 ImageIcon

图像存储在src文件夹中,uder一个子文件夹图像。

我创建了图像图标为

  • 图标playIcon =( Icon)new ImageIcon(src / images / play.png);
  • The project runs fine when I click play in Eclipse but after I created the runnable JAR file, The ImageIcon for the button is gone.
    The images are stored within the src folder, uder a subfolder images.
    I created the image icon as

  • Icon playIcon = (Icon) new ImageIcon("src/images/play.png");




  • 我最终为屏幕录像机创建了一个非常非结构化的单片代码,现在实现他所说的话很难实现。

    我想知道是否有一种创建类或将包含所有这些资源的界面。

  • 例如:
  • I ended up creating a very unstructured monolithic code for screen recorder and now it is slightly difficult to implement what he said.
    I was wondering if there is a way like creating a class or interface that will contain all these resources.

  • For example:
  • public class embeddedResources {
        public static Icon blackCursor;
        public static Icon whiteCursor;
        ...
        ...
    }  
    

    然后所有我要做的是导入这些静态和我的主要 ScreenRecorder 类,

    Then all I have to do is import these statics and in my main ScreenRecorder class,

    this.blackCursor = embeddedResources.blackCorsor;
    


    推荐答案

    我使用这种方法将图像读入 BufferedImage 其中IconManager是定义它的类。

    I am using this method to read image into BufferedImage where IconManager is class where it is defined.

    private static BufferedImage readBufferedImage (String imagePath) {
        try {
            InputStream is = IconManager.class.getClassLoader().getResourceAsStream(imagePath);
            BufferedImage bimage = ImageIO.read(is);
            is.close();
            return bimage;
        } catch (Exception e) {
            return null;
        }
    }
    

    这篇关于创建可运行的JAR文件后,ImageIcon丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆