将项目导出到jar文件时,如何包含我的图标

时间:2021-11-23 10:57:33

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.

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

An example from my project:

我项目的一个例子:

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();
    }

}

3 个解决方案

#1


6  

Use Class#getResource() instead of passing a relative path to the ImageIcon constructor.

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

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

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

#2


5  

In order to make it work, follow these steps :

为了使其工作,请按照下列步骤操作:

  • Right-Click your Project in Project Explorer Tree.
  • 在项目浏览器树中右键单击您的项目。
  • Go to New -> Source Folder and then provide any Name to the Source Folder.
  • 转到新建 - >源文件夹,然后将任何名称提供给源文件夹。
  • Now manually add your stuff to this Source Folder so created by you, like if you want to add images then make a New Folder, by manually visiting this Source Folder through File System.
  • 现在手动将您的东西添加到您创建的源文件夹中,就像您要添加图像然后创建一个新文件夹一样,通过文件系统手动访问此源文件夹。
  • Name this New Folder as images and copy your images to this Folder.
  • 将此新文件夹命名为图像,并将图像复制到此文件夹。
  • Now go back to your Eclipse IDE and Refresh your Project from the Project Explorer, by Right Clicking your Project, here you be able to see your added content now after refreshing.
  • 现在返回Eclipse IDE并从Project Explorer中刷新项目,右键单击您的项目,此处您可以在刷新后立即查看添加的内容。
  • Now in order to access, say any image, you will use.

    现在,为了访问,比如任何图像,你将使用。

    getClass().getResource("/images/yourImageName.extension");
    

which will return one URL object. Do remember the first forward slash, in this case, since whatever is inside your Source Folder is accessed with the help of this, in simpler terms. Now when you will Run your project, the content of this Source Folder will be automatically added to the bin folder and when you will create a Runnable Jar, then the stuff inside your Source Folder can be accessed as it is.

这将返回一个URL对象。在这种情况下,请记住第一个正斜杠,因为在更简单的条件下,可以借助此访问源文件夹中的任何内容。现在,当您运行项目时,此源文件夹的内容将自动添加到bin文件夹中,当您创建Runnable Jar时,可以按原样访问源文件夹中的内容。

On this link, I had tried to explain that with images, how to add Images to your Eclipse Project

在这个链接上,我试图用图像解释,如何将图像添加到Eclipse项目中

#3


4  

You could either fix the layout of your JAR file, which would be to just arrange the folders accordingly as they are in Eclipse, or just do as Matt Ball said and use Class#getResource or Class#getResourceAsStream.

您可以修复JAR文件的布局,这可能只是像在Eclipse中那样相应地排列文件夹,或者像Matt Ball所说的那样,使用Class#getResource或Class#getResourceAsStream。

#1


6  

Use Class#getResource() instead of passing a relative path to the ImageIcon constructor.

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

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

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

#2


5  

In order to make it work, follow these steps :

为了使其工作,请按照下列步骤操作:

  • Right-Click your Project in Project Explorer Tree.
  • 在项目浏览器树中右键单击您的项目。
  • Go to New -> Source Folder and then provide any Name to the Source Folder.
  • 转到新建 - >源文件夹,然后将任何名称提供给源文件夹。
  • Now manually add your stuff to this Source Folder so created by you, like if you want to add images then make a New Folder, by manually visiting this Source Folder through File System.
  • 现在手动将您的东西添加到您创建的源文件夹中,就像您要添加图像然后创建一个新文件夹一样,通过文件系统手动访问此源文件夹。
  • Name this New Folder as images and copy your images to this Folder.
  • 将此新文件夹命名为图像,并将图像复制到此文件夹。
  • Now go back to your Eclipse IDE and Refresh your Project from the Project Explorer, by Right Clicking your Project, here you be able to see your added content now after refreshing.
  • 现在返回Eclipse IDE并从Project Explorer中刷新项目,右键单击您的项目,此处您可以在刷新后立即查看添加的内容。
  • Now in order to access, say any image, you will use.

    现在,为了访问,比如任何图像,你将使用。

    getClass().getResource("/images/yourImageName.extension");
    

which will return one URL object. Do remember the first forward slash, in this case, since whatever is inside your Source Folder is accessed with the help of this, in simpler terms. Now when you will Run your project, the content of this Source Folder will be automatically added to the bin folder and when you will create a Runnable Jar, then the stuff inside your Source Folder can be accessed as it is.

这将返回一个URL对象。在这种情况下,请记住第一个正斜杠,因为在更简单的条件下,可以借助此访问源文件夹中的任何内容。现在,当您运行项目时,此源文件夹的内容将自动添加到bin文件夹中,当您创建Runnable Jar时,可以按原样访问源文件夹中的内容。

On this link, I had tried to explain that with images, how to add Images to your Eclipse Project

在这个链接上,我试图用图像解释,如何将图像添加到Eclipse项目中

#3


4  

You could either fix the layout of your JAR file, which would be to just arrange the folders accordingly as they are in Eclipse, or just do as Matt Ball said and use Class#getResource or Class#getResourceAsStream.

您可以修复JAR文件的布局,这可能只是像在Eclipse中那样相应地排列文件夹,或者像Matt Ball所说的那样,使用Class#getResource或Class#getResourceAsStream。