将图像添加到 JAR Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1096398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add image to JAR Java
提问by
I'm using some images in JFrame
, I have given a path to load image into panel of the frame, but when i made jar of the application its not displaying images.
Where should I place the image?
How should I specify the path?
我正在使用一些图像JFrame
,我已经给出了将图像加载到框架面板的路径,但是当我制作应用程序的 jar 时,它不显示图像。
我应该把图像放在哪里?
我应该如何指定路径?
setIconImage(Toolkit.getDefaultToolkit().getImage(
"D:\RFT\src\dailogBox\dump.jpg"));
like this i have done.
像这样我已经完成了。
回答by hhafez
There are many ways, for example make a folder called resources in your jar file and reference your images from there using a relative path....
有很多方法,例如在 jar 文件中创建一个名为 resources 的文件夹,然后使用相对路径从那里引用您的图像....
回答by joeslice
Add the image in your jar as a resource, then (assuming the jar is on your classpath), use the class loader to load it as a stream.
将图像作为资源添加到您的 jar 中,然后(假设 jar 在您的类路径上),使用类加载器将其加载为流。
回答by Mnementh
First step: Put your image in a defined location in your JAR-file. If you put it into the src-folder, maybe your IDE or your build-tool will package it to the JAR automatically. Otherwise check the documentation of your IDE/build-tool, in which location you have to put it.
第一步:将图像放在 JAR 文件中的定义位置。如果你把它放在 src-文件夹中,也许你的 IDE 或你的构建工具会自动将它打包到 JAR 中。否则,请检查您的 IDE/构建工具的文档,您必须将其放置在哪个位置。
Second step: Access the file from your program. I assume you put it in the package (the path in your JAR) package1/package2 and the file is called dump.jpg. In that case you call:
第二步:从您的程序访问文件。我假设您将它放在包(JAR 中的路径)package1/package2 中,并且该文件名为 dump.jpg。在这种情况下,您调用:
setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/package1/package2/dump.jpg")));
getClass().getResource(...) returns an URL for a resource on your classpath. You start the path in the classpath with a '/' and use the complete path/packages to your resource, separated by '/'.
getClass().getResource(...) 返回类路径上资源的 URL。类路径中的路径以“/”开头,并使用资源的完整路径/包,以“/”分隔。
回答by EasyAnswer
The easy way to do this is to have the folder your image is in parallel to the jar file. Example:
做到这一点的简单方法是让您的图像文件夹与 jar 文件平行。例子:
Say in the code you call the image like this:
在代码中说你像这样调用图像:
pic = new ImageIcon("Pics/buttonpic.jpg");
This means your picture would be called buttonpic and is located in a folder called Pics in your project directory. Now after you make your jar file, copy that Pics folder into the same folder your jar file is located.
这意味着您的图片将被称为 buttonpic 并位于项目目录中名为 Pics 的文件夹中。现在,在您制作 jar 文件后,将该 Pics 文件夹复制到您的 jar 文件所在的文件夹中。
Example: if your jar file is located on your desktop, then put the Pics folder on the desktop. Now when you run the jar file, your pictures will work correctly. Now to cover up the fact that you need to have your Pics folder next to your jar file, you can create a new folder and place in that folder your jar and the Pics folder. Then right click the jar and create a shortcut, then place that shortcut on the desktop or where you wanted to have just the jar file, and place the new folder you created wherever you want to hide it at.
示例:如果您的 jar 文件位于桌面上,则将 Pics 文件夹放在桌面上。现在,当您运行 jar 文件时,您的图片将正常工作。现在为了掩盖您需要在 jar 文件旁边放置 Pics 文件夹的事实,您可以创建一个新文件夹并将您的 jar 和 Pics 文件夹放在该文件夹中。然后右键单击该 jar 并创建一个快捷方式,然后将该快捷方式放在桌面上或您只想拥有 jar 文件的位置,然后将您创建的新文件夹放置在您想要隐藏它的任何位置。
回答by Joyal Augustine
First Create a package(Say images) under Source Packages
首先在Source Packages下创建一个包(比如图片)
copy all of your images to this package(when you create a package,a folder in the name of your package will be created inside your project src folder, so copy images to it)
将所有图像复制到此包中(创建包时,将在项目 src 文件夹中创建一个以包名称命名的文件夹,因此将图像复制到其中)
You can access your Images from your program as
您可以从您的程序访问您的图像
Example 1:
示例 1:
this.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/yourimagename")));
Example 2:
示例 2:
URL imageurl = getClass().getResource("/images/imagename");//assuming your package name is images
Image myPicture = Toolkit.getDefaultToolkit().getImage(imageurl);
JLabel piclabel = new JLabel(new ImageIcon( myPicture ));
piclabel.setBounds(0,0,myPicture.getWidth(null),myPicture.getHeight(null));
Now use this JLabel piclabel
现在使用这个 JLabel piclabel
回答by Martin Sansone - MiOEE
This IS the best way to handle all images and icons in a JAR App.
这是处理 JAR 应用程序中所有图像和图标的最佳方式。
Once you've zipped up all of your images and icons into its own JAR file - Configure your build path by adding the images JAR file into your libraries tab so that its now included in your classpath.
一旦您将所有图像和图标压缩到其自己的 JAR 文件中 - 通过将图像 JAR 文件添加到您的库选项卡中来配置您的构建路径,以便它现在包含在您的类路径中。
Then simply use the following 3x lines of codeat the start of your constuctor to access any image you need for anything including a SystemTray image which doesn't accept the simple ImageIcon's as its main icon (weird I know). The 3x lines are:
然后只需在构造函数的开头使用以下3x 行代码来访问您需要的任何图像,包括 SystemTray 图像,该图像不接受简单的 ImageIcon 作为其主图标(我知道很奇怪)。3x 行是:
URL iconUrl = this.getClass().getResource("/image-iconb.png");
Toolkit tk = this.getToolkit();
someimgicon = tk.getImage(iconUrl);
(someimgicon is just a constructor declared Image variable) Now you canset a window icon as simply as:
(someimgicon 只是一个声明 Image 变量的构造函数) 现在你可以简单地设置一个窗口图标:
setIconImage(someimgicon);
andat the same time use the same variable when setting the System TrayIcon by declaring:
同时在通过声明设置 System TrayIcon 时使用相同的变量:
trayIcon = new TrayIcon(someimgicon, "SystemTray Demo", popupMenu);
The above allows you to declare Images or ImageIcons easily and centrally without running the risk of not keeping image resources in the right place. It keeps it nice and tidy, with the JAR containing all your images automatically compiled at run time and distribution of your program.
以上允许您轻松集中地声明图像或图像图标,而不会冒没有将图像资源保存在正确位置的风险。它保持整洁,JAR 包含在运行时和程序分发时自动编译的所有图像。
As a bonus, once the JAR is registered in your classpath - you can keep adding any other images into the same JAR at any time without any fuss too - Everything just works and the added images are instantly available to your app.
作为奖励,一旦 JAR 在您的类路径中注册 - 您可以随时将任何其他图像添加到同一个 JAR 中而无需大惊小怪 - 一切正常,添加的图像立即可用于您的应用程序。
Much better in my view.
在我看来要好得多。
回答by Abdelsalam Shahlol
In netbeans 8.1 what I've done is to include the folder of icons and other images called Resourcesinside the srcfolder in the project file. So whenever i build Jarfile the folder is included there.The file tree should be like this:
在 netbeans 8.1 中,我所做的是在项目文件的src文件夹中包含名为Resources的图标和其他图像文件夹。所以每当我构建Jar文件时,文件夹都包含在那里。文件树应该是这样的:
- src (Java files in source packges are here)
** PACKAGE YOU NAMED IN PROJECT**
- file.java
Resources
- image.jpg
- src(源包中的Java文件在这里)
** 您在项目中命名的包**
- 文件.java
资源
- 图片.jpg
The code should be like:
代码应该是这样的:
jToggleButton1.setIcon(new javax.swing.ImageIcon(this.getClass().getResource("/resources/image.jpg")));