将项目导出到jar文件时,如何包含我的图标 [英] How can I include my icon when I export my project to a jar file

查看:146
本文介绍了将项目导出到jar文件时,如何包含我的图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个桌面应用程序。问题是当我将应用程序导出到jar文件时,应用程序中没有显示图标。当我从Eclipse运行它时,所有图标都显示在那里。

I have developed an desktop application. The problem is when I export the application to jar file the icon isn't shown in the app. When I run it from Eclipse all icons are shown there.

我项目的一个例子:

package net.ebank.gui;

import java.awt.*;
import javax.swing.*;


public class EBank extends JFrame {

    protected Start s;


    public EBank() {

        setTitle("Welcome To EBank");

        setBackground(Color.white);


        Image img = new ImageIcon("../EBank/res/bank.jpg").getImage();
        this.setIconImage(img);

        /*"com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"
        "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"*/

        setVisible(false);

        setSize(1350,700);

        setDefaultCloseOperation(this.EXIT_ON_CLOSE);

        try {

            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (ClassNotFoundException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();
        } catch (InstantiationException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        } catch (UnsupportedLookAndFeelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        s= new Start(this);

        s.setLocation(getWidth()/2, getHeight()/4);


    }

    public static void main(String[] args){
        new EBank();
    }

}


推荐答案

使用 Class#getResource() 而不是将相对路径传递给 ImageIcon 构造函数。

还要确保图像文件实际上是生成的JAR的一部分。

Also make sure the image file is actually part of the generated JAR.

这篇关于将项目导出到jar文件时,如何包含我的图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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