Add picture to .jar

2

I have several programs that run perfectly on my machine, .jar runs smoothly and I use them as I wish, without problems. However, when I run them on another machine, the images do not appear. The program runs perfectly, everything the way it should be, but the images simply do not appear.

Here's one of the programs you'd like the most, a calculator. I changed the JOpTionPane.showMessageDialog icon, which is in the menu, and the icon does not appear.

import java.awt.Color;
import java.awt.event.*;
import javax.swing.*;

public class Calculadora extends JFrame {
    private static final long serialVersionUID = 1L;

private JButton n1;
private JButton n2;
private JButton n3;
private JButton n4;
private JButton n5;
private JButton n6;
private JButton n7;
private JButton n8;
private JButton n9;
private JButton n0;
private JButton botaoMais;
private JButton botaoMenos;
private JButton botaoVezes;
private JButton botaoDividi;
private JButton botaoIgual;
private JButton botaoC;
private JButton botaoCE;
private JButton botaoPonto;
private char operacao;
private int inteiro;
private int decimal;
private double memoria;
private boolean ponto;
private JLabel creditos;
private JTextField campo;

public Calculadora(){
    setTitle("Calculadora do Ozzy");
    setBounds(0,0,280,400);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(false);
    getContentPane().setBackground(new Color(190,194,233));
    setLayout(null);

    JMenuBar mBar = new JMenuBar();

    JMenu opcoes = new JMenu("Opções");
    JMenuItem sobreCalc = new JMenuItem("Sobre");
    JMenuItem sobreMim = new JMenuItem("Sobre mim");

    opcoes.add(sobreCalc);
    opcoes.add(sobreMim);

    JMenu sair = new JMenu("Sair");
    JMenuItem exit = new JMenuItem ("Sair");

    sair.add(exit);

    mBar.add(opcoes);
    mBar.add(sair);
    setJMenuBar(mBar);

    inteiro = 0;
    decimal = 0;
    memoria = 0;

    n1 = new JButton ();
    n1.setText("1");
    n1.setBounds(25,115,45,45);
    n1.setBackground(new Color(134,142,215));
    n1.setForeground(Color.white);
    this.add(n1);

    n2 = new JButton ();
    n2.setText("2");
    n2.setBounds(85,115,45,45);
    n2.setBackground(new Color(134,142,215));
    n2.setForeground(Color.white);
    this.add(n2);

    n3 = new JButton();
    n3.setText("3");
    n3.setBounds(140,115,45,45);
    n3.setBackground(new Color(134,142,215));
    n3.setForeground(Color.white);
    this.add(n3);

    n4 = new JButton();
    n4.setText ("4");
    n4.setBounds (25,170,45,45);
    n4.setBackground(new Color(134,142,215));
    n4.setForeground(Color.white);
    this.add(n4);

    n5 = new JButton ();
    n5.setText("5");
    n5.setBounds(85,170,45,45);
    n5.setBackground(new Color(134,142,215));
    n5.setForeground(Color.white);
    this.add (n5);

    n6 = new JButton();
    n6.setText("6");
    n6.setBounds(140,170,45,45);
    n6.setBackground(new Color(134,142,215));
    n6.setForeground(Color.white);
    this.add(n6);

    n7 = new JButton ();
    n7.setText("7");
    n7.setBounds(25,225,45,45);
    n7.setBackground(new Color(134,142,215));
    n7.setForeground(Color.white);
    this.add(n7);

    n8 = new JButton();
    n8.setText("8");
    n8.setBounds (85,225,45,45);
    n8.setBackground(new Color(134,142,215));
    n8.setForeground(Color.white);
    this.add (n8);

    n9 = new JButton ();
    n9.setText("9");
    n9.setBounds (140,225,45,45);
    n9.setBackground(new Color(134,142,215));
    n9.setForeground(Color.white);
    this.add (n9);

    n0 = new JButton();
    n0.setText ("0");
    n0.setBounds (140,280,45,45);
    n0.setBackground(new Color(134,142,215));
    n0.setForeground(Color.white);
    this.add (n0);

    botaoMais = new JButton();
    botaoMais.setText("+");
    botaoMais.setBounds(195,115,45,45);
    botaoMais.setBackground(new Color(133,197,235));
    botaoMais.setForeground(Color.white);
    this.add(botaoMais);

    botaoMenos = new JButton ();
    botaoMenos.setText ("-");
    botaoMenos.setBounds(195,170,45,45);
    botaoMenos.setBackground(new Color(133,197,235));
    botaoMenos.setForeground(Color.white);
    this.add (botaoMenos);

    botaoVezes = new JButton();
    botaoVezes.setText ("x");
    botaoVezes.setBounds(195,225,45,45);
    botaoVezes.setBackground(new Color(133,197,235));
    botaoVezes.setForeground(Color.white);
    this.add(botaoVezes);

    botaoDividi = new JButton();
    botaoDividi.setText("÷");
    botaoDividi.setBounds(195,280,45,45);
    botaoDividi.setBackground(new Color(133,197,235));
    botaoDividi.setForeground(Color.white);
    this.add(botaoDividi);

    botaoIgual = new JButton();
    botaoIgual.setText("=");
    botaoIgual.setBounds(25,280,45,45);
    botaoIgual.setBackground(new Color(133,197,235));
    botaoIgual.setForeground(Color.white);
    this.add(botaoIgual);

    botaoPonto = new JButton ();
    botaoPonto.setText(".");
    botaoPonto.setBounds(85,280,45,45);
    botaoPonto.setBackground(new Color(133,197,235));
    botaoPonto.setForeground(Color.white);
    this.add (botaoPonto);

    botaoC = new JButton ();
    botaoC.setText("C");
    botaoC.setBounds(25,65,105,40);
    botaoC.setBackground(new Color (157,182,210));
    botaoC.setForeground(Color.white);
    this.add(botaoC);

    botaoCE = new JButton ();
    botaoCE.setText("CE");
    botaoCE.setBounds(140,65,100,40);
    botaoCE.setBackground(new Color(157,182,210));
    botaoCE.setForeground(Color.white);
    this.add(botaoCE);

    campo = new JTextField();
    campo.setBounds(25,25,216,30);
    this.add(campo);

    creditos = new JLabel();
    creditos.setBounds(78,287,200,100);
    creditos.setText("Gabriel Ozzy Santos");
    this.add(creditos);

    n1.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            if(ponto){
                decimal +=1;
            } else{
            inteiro *= 10;
            inteiro +=1;

            }
            campo.setText(campo.getText()+ "1");
        }
    });
    n2.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            if (ponto){
                decimal +=2;
            }else{
            inteiro *=10;
            inteiro+= 2;

            }
            campo.setText (campo.getText()+"2");
        }
    });
    n3.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent event){
            if (ponto){
                decimal+=3;
            }else {
            inteiro *= 10;
            inteiro += 3;
            }
            campo.setText(campo.getText()+"3");

        }
    });
    n4.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            if (ponto){
                decimal+=4;
            }else {
            inteiro *= 10;
            inteiro +=4;
            }
            campo.setText (campo.getText ()+"4");

        }
    });
    n5.addActionListener(new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            if (ponto){
                decimal +=5;
            }else{
            inteiro*= 10;
            inteiro +=5;
            }
            campo.setText (campo.getText ()+ "5");
        }
    });
    n6.addActionListener (new ActionListener (){
        public void actionPerformed (ActionEvent evt){
            if (ponto){
                decimal +=6;
            }else {
            inteiro*= 10;
            inteiro +=6;
            }
            campo.setText (campo.getText ()+ "6");
        }
    });
    n7.addActionListener(new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            if(ponto){
                decimal +=7;
            }else {
            inteiro*= 10;
            inteiro +=7;
            }
            campo.setText (campo.getText()+"7");
        }
    });
    n8.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            if(ponto){
                decimal +=8;
            }else{
            inteiro *=10;
            inteiro +=8;
            }
            campo.setText(campo.getText()+"8");
        }
    });
    n9.addActionListener (new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            if (ponto){
                decimal +=9;
            }else{
            inteiro *=10;
            inteiro +=9;
            }
            campo.setText(campo.getText()+"9");

        }
    });
    n0.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            if(ponto){
                decimal +=0;
            }else{
            inteiro*=10;
            inteiro +=0;
            }
            campo.setText(campo.getText()+"0");
        }
    });
    botaoMais.addActionListener (new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            double numero =Double.parseDouble(String.format("%s.%s",inteiro,decimal));

            String t1 = campo.getText();
            if(!"".equals(t1))
            numero = Double.parseDouble(t1);

            operacao = '+';
            if (memoria > 0){
                memoria += numero;
            }else{
                    memoria = numero;
                }
            inteiro = 0;
            numero = 0;
            decimal = 0;
            ponto = false;
            campo.setText("");
            }
        });
    botaoMenos.addActionListener (new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            double numero = Double.parseDouble (String.format("%s.%s",inteiro,decimal));

            String t1 = campo.getText();
            if(!"".equals(t1))
            numero = Double.parseDouble(t1);

            operacao = '-';
            if (memoria > 0){
                memoria -= numero;
            }else {
                memoria = numero;
            }
            numero = 0;
            inteiro = 0;
            decimal = 0;
            ponto = false;
            campo.setText("");
        }
    });
    botaoVezes.addActionListener (new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            double numero = Double.parseDouble (String.format("%s.%s",inteiro,decimal));

            String t1 = campo.getText();
            if(!"".equals(t1))
            numero = Double.parseDouble(t1);

            operacao = '*';
            if (memoria > 0){
                memoria *= numero;
            }else {
                memoria = numero;
            }
            numero = 0;
            inteiro =0;
            decimal =0;
            ponto = false;      
            campo.setText("");
        }
    });
    botaoDividi.addActionListener(new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            double numero = Double.parseDouble(String.format("%s.%s",inteiro,decimal));

            String t1 = campo.getText();
            if(!"".equals(t1))
            numero = Double.parseDouble(t1);

            operacao = '/';
            if(memoria > 0){
                memoria /=numero;
            }else {
                memoria = numero;
            }
            numero=0;
            inteiro =0;
            decimal = 0;
            ponto = false;
            campo.setText("");
        }
    });
    botaoC.addActionListener (new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            inteiro = 0;
            decimal = 0;
            memoria = 0;
            ponto = false;
            campo.setText("");
        }
    });
    botaoCE.addActionListener(new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            inteiro = 0;
            decimal = 0;
            ponto = false;
            campo.setText("");
        }
    });
    botaoPonto.addActionListener(new ActionListener(){
        public void actionPerformed (ActionEvent evt){
            if (!ponto){
                campo.setText(inteiro +".");
                ponto = (true);
            }
        }
    });
    botaoIgual.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            double numero = Double.parseDouble(String.format("%s.%s", inteiro, decimal));

            String t1 = campo.getText();
            if(!"".equals(t1))
            numero = Double.parseDouble(t1);

            switch (operacao){
            case '+':{
                memoria += numero;
                break;
            }
            case '-':{
                memoria -=numero;
                break;
            }
            case '*':{
                memoria *=numero;
                break;
            }
            case '/':{
                memoria /=numero;
                break;
            }
            }
            numero = 0;
            campo.setText(""+ memoria);
        }
    });

    sobreCalc.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            Icon about = new ImageIcon("C:/Users/Gabriel Ozzy/Downloads/Java2.jpg");  
            JOptionPane.showMessageDialog(null,"Programado em Java 7 - Plataforma Eclipse", "Sobre o software",0,about);
        }
    });
    sobreMim.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            Icon about2 = new ImageIcon("C:/Users/Gabriel Ozzy/Downloads/Java2.jpg");
            JOptionPane.showMessageDialog(null,"Programador: Gabriel Ozzy Santos","Sobre o desenvolvedor",0,about2);
        }
    });
    exit.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent evt){
                System.exit(0);
            }
        });
}

public static void main (String [] args){
    Calculadora cCalculadora = new Calculadora();
        cCalculadora.setVisible(true);
}
}

Here is the error:

    
asked by anonymous 12.05.2014 / 18:57

3 answers

3

I always put all the images that I'm going to use inside a new package, so when I generate a .jar I do not have to worry about them because they'll be in there.

I created a new package called assets, and inside it I put the images that I need and in another package I created a class to perform the load test of the image. According to the following structure:

Howdidtheclassloadtheimagesanddidatestiftheimagewasuploadedsuccessfully.

You can use various shapes to load the image, I used a simple one as needed. And the console output indicating that the image was uploaded successfully.

    
13.05.2014 / 21:59
2

Create a .zip file with all your images and add it to your Build Path.

  

Right click on your project folder > Build Path > Configure Build Path > Java Build Path > Libraries > Add JARs

Example:

Tousetheimageinyourcode,forexampledo:

JLabellblImg1=newJLabel("");
JLabel lblImg2 = new JLabel("");
lblImg1.setIcon(new ImageIcon(Principal.class.getResource("/img1.png"))); 
lblImg2.setIcon(new ImageIcon(Principal.class.getResource("/img2.png"))); 

Use the name of the images that are inside your zip.

At the time of exporting the jar it will automatically include your .zip file containing the images.

    
12.05.2014 / 19:12
1

I do not know if it has already been resolved, but I will respond. When I use images in my projects I create a folder inside the project folder and do it like this:

Icon about = new ImageIcon("minhaPasta/Java2.jpg");

And when I use another machine I copy the jar and this folder. It always works.

    
13.05.2014 / 14:51