setIconImage 仅适用于 Eclipse,导出为可运行 jar 文件时不起作用 [英] setIconImage only works in eclipse, doesnt work when exported as runnable jar file

查看:61
本文介绍了setIconImage 仅适用于 Eclipse,导出为可运行 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)

从指定的文件.将使用 MediaTracker 预加载图像监控图像的加载状态.指定的字符串可以是文件名或文件路径.指定路径时,使用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")

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

参数:
文件名 - 一个字符串,指定一个文件名或路径

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 而不是 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,导出为可运行 jar 文件时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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