【发布时间】:2016-03-21 10:31:57
【问题描述】:
我对图像图标有疑问。我知道以前有人问过这个问题,但我不知道如何解决这个问题,因为我认为我的问题不同。当我使用 Eclipse 启动应用程序时,一切正常。但是当我把它做成可运行的 jar 时,它不会显示图像。
我的 ImagesHolder 类的一些代码:
package Clicker;
import javax.swing.ImageIcon;
public class ImagesHolder {
final public ImageIcon AccessoriesIcon = new ImageIcon("Images/Part_Accessories.png");
final public ImageIcon BodyIcon = new ImageIcon("Images/Part_Body.png");
final public ImageIcon BrakesIcon = new ImageIcon("Images/Part_Brakes.png");
final public ImageIcon CoolingIcon = new ImageIcon("Images/Part_Cooling.png");
final public ImageIcon ElectronicsIcon = new ImageIcon("Images/Part_Electronics.png");
final public ImageIcon EngineIcon = new ImageIcon("Images/Part_Engine.png");
final public ImageIcon ExaustIcon = new ImageIcon("Images/Part_Exaust.png");
final public ImageIcon FuelIcon = new ImageIcon("Images/Part_Fuel.png");
final public ImageIcon InteriorIcon = new ImageIcon("Images/Part_Interior.png");
final public ImageIcon SteeringIcon = new ImageIcon("Images/Part_Steering.png");
final public ImageIcon SuspensionIcon = new ImageIcon("Images/Part_Suspension.png");
final public ImageIcon TransmissionIcon = new ImageIcon("Images/Part_Transmission.png");
final public ImageIcon TiresIcon = new ImageIcon("Images/Part_Tires.png");
如果我将图像作为 URL,我无法重置图像图标,就像这里一样(我有标签,我想更改标签图标)
标签示例:
public JLabel AccessoriesLVL1Label = new JLabel(ImagesHolder.LockedIcon);
AccessoriesLVL1Label.setHorizontalTextPosition(JLabel.CENTER);
AccessoriesLVL1Label.setVerticalTextPosition(JLabel.BOTTOM);
AccessoriesLVL1Label.setText("<html>Accessories LVL 1<br>" + "Count: " + Part.parts[1]);
然后改变:
if(CarMain.main[5] >=1){
jbtnSellAccessoriesLv1.setEnabled(true);
Labels.AccessoriesLVL1Label.setIcon(ImagesHolder.AccessoriesIcon);
}
编辑: 如果我这样:
final public ImageIcon MoneyIcon = new ImageIcon("Images/Money.png");
制作为:
URL MoneyIcon = ImagesHolder.class.getResource("/Money.png");
我在这一行得到错误:
Labels.MoneyLabel.setIcon(ImagesHolder.MoneyIcon);
错误:
The method setIcon(Icon) in the type JLabel is not applicable for the arguments (URL)
【问题讨论】:
-
首先,你有没有检查过它们在罐子里吗?以防万一……
-
@user3272243 是的,我以 zip 格式打开 jar 文件,里面有所有图片。但它们是主要的,而不是图像文件夹。我尝试更改图像位置(在代码中,如果图像在主文件夹中,则可以工作),但仍然没有显示任何图像
-
如果您将它们移动到罐子内,就好像它是一个“正确位置”的拉链一样,它们会起作用吗?也尝试使用绝对路线将它们放置在该位置,看看罐子是否可以以这种方式丢弃,如果罐子真的不起作用或者它是否对您有问题。
-
@user3272243 对不起,我的英语不太好,我听不懂你的意思。也许你可以请解释一下,不知何故不同?这是图像,图像在 jar 文件中:imgur.com/LvBcvYN
-
Check this answer。它会帮助你。
标签: java swing embedded-resource