Swing - 在 JAR 文件中包含图像

Swing - Include images in JAR file

我在 JAVA 中有一个项目,它有一个带屏幕的图形界面,是用 Swing 开发的。

我有 2 张图片,背景和图标,它们在包中:

br\com\drsolutions\monitorar\imagens

项目结构:

br\com\drsolutions\monitorar\imagens
    Fundo.jpg
    icone.png
br\com\drsolutions\monitorar\rede
    TestarIcmp.java
br\com\drsolutions\monitorar\ux
    InterfaceGrafica.java
br\com\drsolutions\monitorar
    Aplicativo.java

在InterfaceGrafica.java文件中,我在JFrame中添加图片如下:

...
jFrame = new JFrame("Monitorar");
jFrame.setSize(246, 410);
jFrame.setResizable(false);
jFrame.setLayout(null);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

/* Colocar a imagem no background e o ícone na janela */
try {
    jFrame.setContentPane(new JLabel(new ImageIcon(
            ImageIO.read(new File("src\br\com\drsolutions\monitorar\imagens\Fundo.jpg")))
    ));
    jFrame.setIconImage(new ImageIcon("src\br\com\drsolutions\monitorar\imagens\icone.png")
            .getImage()
    );
} catch (IOException e) {
    return false;
}
...

如何在不通过这种方式传递完整路径的情况下添加图像?

如何使图像在我生成的 JAR 文件中工作?

谢谢, 迭戈·罗德里格斯

在将文件放入 JAR 之前,您正在通过磁盘上的路径访问文件。您需要使用资源加载器,如下所述: How to read a file from jar in Java?