【问题标题】:my image loads properly on running on eclipse , but when exporting as jar and opening it there is no image at all我的图像在 eclipse 上运行时正确加载,但是当导出为 jar 并打开它时,根本没有图像
【发布时间】:2020-08-06 21:12:44
【问题描述】:

当我在 eclipse 中运行主类时图像出现,但当我导出为可执行 jar 文件时没有出现。即使一切正常。

icon =new ImageIcon(new ImageIcon("robot.png").getImage().getScaledInstance(150, 150, Image.SCALE_DEFAULT));
label=new JLabel(icon);

【问题讨论】:

    标签: java eclipse image jar executable-jar


    【解决方案1】:

    当您的图像被打包到一个 jar 中时,它不再是一个“文件”,因此以这种方式加载它是行不通的。您可以使用 #getResource() 向您的班级询问图像内容的可用 URL,并使用生成的 URL 来构建您的 ImageIcon

    【讨论】:

    • 伙计,我试过这样做它给了我找不到类的错误,这里的类名“GUI”是我写代码的类pastebin.com/LxFf7MiM
    • 如果您的代码在 GUI 类中,请使用 getClass()。如果没有,请使用 GUI.class。您不必使用其名称查找您自己的类之一,如果它未在包中声明,则可能无法正常工作。
    猜你喜欢
    • 2021-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多