有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

可执行JAR中未出现JOptionPane的java ImageIcon

正如标题所说,我一直在尝试将图像放入我的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");

我只是将ImageIcon的名称调用到JOptionPane中。showmessage对话


共 (1) 个答案

  1. # 1 楼答案

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

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