Swing - Include images in JAR file

Asked

Viewed 94 times

-1

I have a project in JAVA, which has a graphical interface with a screen, developed with the Swing.

I have 2 images, background and favicon, which are in the package: [code]br with drsolutions monitor images[/code]

Structure of the Project: br with drsolutions monitor images Background. png icon. br with drsolutions monitor network Testaricmp.java br with drsolutions monitor ux Interfacegrafica.java br with drsolutions monitor Java application.

In the archive Interfacegrafica.java, add the images in Jframe as follows:

...
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;
}
...

How can I add the images without having to pass the full path this way?

How to make the images work in the JAR file I Gero?

Thank you, Diego M. Rodrigues

1 answer

0

Create a folder inside src/main by the name of Resources, then move the folder images into Resources and change the path to:

new File(getClass().getResources("imagens/Fundo.jpg"));
new ImageIcon(getClass().getResources("imagens/icone.jpg")).getImage();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.