Java映像未出现在JAR文件中 [英] Java images not appearing in JAR file

查看:78
本文介绍了Java映像未出现在JAR文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我怎么称呼图像

Icon logoOne = new ImageIcon("logo.png");
// LOGO
JLabel imageLogo = new JLabel(logoOne); 
imageLogo.setPreferredSize(new Dimension(200, 50));

它只是添加到JFrame的面板中,在Eclipse中运行时工作正常,但在Eclipse中运行时却不显示

Its just added to a panel in a JFrame and works fine when running in eclipse but doesnt show up when exported to JAR.

任何人都可以帮忙吗?

推荐答案

导出时您的JAR文件中,您必须确保还将图像文件包含在JAR文件中。例如:如果您有一个名为 res的文件夹,其中包含图像,则必须选中此文件夹之前的复选框,以确保此文件夹和图像都包括在内。

When exporting your JAR file, you've to make sure you're also including the image files in your JAR file. For example: If you have a folder called 'res' containing the images, you have to check the checkbox before this folder in order to make sure this folder, and the images are being included.

您可能想添加一些故障排除代码。例如:如果此图像文件存在,请添加一些在控制台中打印的代码。导出您的JAR文件,通过控制台/终端运行您的JAR并检查结果。如果显示的图像不存在,则说明导出的JAR文件存在问题。

You might want to add some trouble shooting code. For example: Add some code that prints in the console if this image file exists. Export your JAR file, run your JAR via the console/terminal and check the result. If this shows you the image doesn't exists, you know it's a problem with your exported JAR file.

您可能希望尝试将其作为资源流到达该图像,尝试以下代码示例:

You might want to try reaching the image as a resource steam, give the following code sample a try:

java.net.URL logoOneUrl = getClass().getResource("logo.png");
Icon logoOne = new ImageIcon(logoOneUrl );

这篇关于Java映像未出现在JAR文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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