带有JOptionPane的ImageIcon没有出现在可执行JAR中

问题描述 投票:0回答:1

正如标题所述,我一直在尝试将图像放入“ JOption消息”对话框中。当它们在eclipse中运行时,我已经显示了它们,但是当我将文件导出到可执行jar中时,图像不会加载。

In Eclipse

In jar executable

    ImageIcon normal = new ImageIcon("src/project/normal.png");
    ImageIcon happy = new ImageIcon("src/project/happy.png");
    ImageIcon jackpot = new ImageIcon("src/project/jackpot.png");
    ImageIcon fail = new ImageIcon("src/project/fail.png");
    ImageIcon error = new ImageIcon("src/project/error.png");

我只是在JOptionPane.showMessageDialogue中调用ImageIcon的名称

java swing embedded-resource imageicon getresource
1个回答
0
投票

您当前正在从文件中读取图像,并将其导出到可执行jar中时,该文件不再可用。相反,请尝试将图像作为资源:

ImageIcon imageIcon = new ImageIcon(getClass().getResource("/image.png"));

© www.soinside.com 2019 - 2024. All rights reserved.