setIconImage仅在eclipse中有效,在导出为runnable jar文件时不起作用 [英] setIconImage only works in eclipse, doesnt work when exported as runnable jar file

查看:379
本文介绍了setIconImage仅在eclipse中有效,在导出为runnable jar文件时不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了一段时间来修复此问题。我的工作区中有一个项目文件夹(该文件夹基本上是我的根目录),其中有一个名为icon.gif的文件。在我的程序中,我有以下内容:

I have searched for a while now for a fix to this. I have a project folder in my workspace (that folder is basically my root), and in it there is a file called icon.gif. In my program, I have the following:

package com.mgflow58.Main;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class Game {

    public static void main(String[] args) {
        JFrame window = new JFrame("Guppy's Adventure");
        window.add(new GamePanel());
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setResizable(false);
        window.pack();
        window.setLocationRelativeTo(null);
        window.setVisible(true);
        window.setIconImage(new ImageIcon("icon.gif").getImage());
    }

}

这个图标在eclipse中运行正常我运行它,但导出的jar文件不会像在eclipse中那样显示图标。知道为什么吗?我疯了,试图解决这个问题。

The icon works fine in eclipse when I run it, but the exported jar file does not display the icon as it does in eclipse. Any idea why? I'm going crazy trying to figure this out.

推荐答案

jar文件包含其正确文件夹中所需的所有图像文件。

The jar file contains all the image files needed in their proper folders.

有问题... ImageIcon(String)正在寻找指定的
文件 JavaDocs ...

There is the problem...ImageIcon(String) is looking for the named file on the disk as described by the JavaDocs...


public ImageIcon(String filename)


创建指定文件的ImageIcon。将使用MediaTracker
预先加载图像以监控图像的加载状态。指定的String可以是
文件名或文件路径。指定路径时,请使用
Internet标准正斜杠(/)作为分隔符。 (该字符串是
转换为URL,因此正斜杠适用于所有系统。)对于
示例,请指定:

public ImageIcon(String filename)

Creates an ImageIcon from the specified file. The image will be preloaded by using MediaTracker to monitor the loading state of the image. The specified String can be a file name or a file path. When specifying a path, use the Internet-standard forward-slash ("/") as a separator. (The string is converted to an URL, so the forward-slash works on all systems.) For example, specify:

new ImageIcon(images / myImage.gif)

描述初始化为文件名字符串。


参数:
filename - 指定
文件名或路径的字符串

The description is initialized to the filename string.

Parameters:
filename - a String specifying a filename or path

资源不再驻留在磁盘上,但未嵌入到Jar文件中,因此无法再使用 File 访问它们方法。

The resources no longer reside on the disk, but are not embedded within the Jar file, so they can no longer be accessed by using File based methods.

相反,你需要使用类似 Class#getResource 的东西,例如...

Instead, you will need to use something like Class#getResource, for example...

window.setIconImage(new ImageIcon(Game.class.getResource("icon.gif")).getImage());

话虽如此,我还建议使用 ImageIO over ImageIcon 有很多原因,但主要是因为如果由于某种原因无法加载资源,它会实际抛出异常,而不是静默失败。 ..

Having said this, I would also recommend using ImageIO over ImageIcon for a number of reasons, but mostly, because it will actually throw an exception if the resource can't be loaded for some reason, rather than failing silently...

看看阅读/加载图片了解更多详情

这篇关于setIconImage仅在eclipse中有效,在导出为runnable jar文件时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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