What happens is that when i run my program from netbeans it works fine and it shows all the images but when i run the .jar file it doesn't show any of them...the images are declared below the "//Imagenes automata" comment by the way, im new in Java so please try to be clear in your answer. ohhh and the .jpg images are saved in a folder inside the project folder

Here is my code, i have two classes, here is my main class

import javax.swing.*;
import java.util.Scanner;
import java.awt.*;
import java.awt.event.*;


public class Main {

   
    public static void main(String[] args) {
    GlobalDataStore dato = new GlobalDataStore();

///////////////////////Interfaz////////////////////////////////////////

 //Frame
 JFrame frame = new JFrame("Vending Machine");
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 //Panel
 JPanel container = new JPanel();
 container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
 JPanel panel = new JPanel();
 JPanel panel2 = new JPanel();
 panel2.setLayout(new BoxLayout(panel2, BoxLayout.PAGE_AXIS));
 panel2.setBorder(BorderFactory.createEmptyBorder(0, 50, 0, 0));
 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
 panel.setAlignmentX(Component.LEFT_ALIGNMENT);

 //TextField
 JTextField field = new JTextField();
 field.setEditable(false);

 //Imagenes automata
 
 ImageIcon inicial=new ImageIcon("Transicioni.jpg");
 JLabel label=new JLabel(inicial);
 ImageIcon uno=new ImageIcon("Transicion1.jpg");
 JLabel label1=new JLabel(uno);
 ImageIcon dos=new ImageIcon("Transicion2.jpg");
 JLabel label2=new JLabel(dos);
 ImageIcon tres=new ImageIcon("Transicion3.jpg");
 JLabel label3=new JLabel(tres);
 ImageIcon cuatro=new ImageIcon("Transicion4.jpg");
 JLabel label4=new JLabel(cuatro);
 ImageIcon cinco=new ImageIcon("Transicion5.jpg");
 JLabel label5=new JLabel(cinco);
 ImageIcon seis=new ImageIcon("Transicion6.jpg");
 JLabel label6=new JLabel(seis);
 ImageIcon siete=new ImageIcon("Transicion7.jpg");
 JLabel label7=new JLabel(siete);
 ImageIcon ocho=new ImageIcon("Transicion8.jpg");
 JLabel label8=new JLabel(ocho);
 ImageIcon nueve=new ImageIcon("Transicion9.jpg");
 JLabel label9=new JLabel(nueve);

 JLabel money = new JLabel("Coins");
 JLabel opcion = new JLabel("Options");
 JLabel credito = new JLabel("Credit");

 label1.setVisible(false);
 label2.setVisible(false);
 label3.setVisible(false);
 label4.setVisible(false);
 label5.setVisible(false);
 label6.setVisible(false);
 label7.setVisible(false);
 label8.setVisible(false);
 label9.setVisible(false);

 //Botones
 JButton button = new JButton ("$ 0.5  ");
 JButton button2 = new JButton ("$ 0.10");
 JButton button3 = new JButton ("$ 0.25");
 JButton retorno = new JButton (" Return  ");
 ImageIcon cup = new ImageIcon("pepsi.gif");
 JButton dispense = new JButton (cup);
 dispense.setBackground(Color.WHITE);
 ImageIcon cup2 = new ImageIcon("cola.png");
 JButton dispense2 = new JButton (cup2);
 dispense2.setBackground(Color.WHITE);
 JButton salir = new JButton (" Exit  ");

 //Action Listeners
 button.addActionListener(new Cinco());
 button2.addActionListener(new Diez());
 button3.addActionListener(new Quarter());
 retorno.addActionListener(new Rturn());
 dispense.addActionListener(new Dispense());
 dispense2.addActionListener(new Dispense());
 salir.addActionListener(new Exit());

 //Agregacion
 container.add(panel);
 panel.add(label);
 panel.add(label1);
 panel.add(label2);
 panel.add(label3);
 panel.add(label4);
 panel.add(label5);
 panel.add(label6);
 panel.add(label7);
 panel.add(label8);
 panel.add(label9);
 container.add(panel2);
 panel2.add(Box.createRigidArea(new Dimension(0,19)));
 field.setBounds(1100,80,50,20);
 frame.add(field);
 credito.setBounds(1100,20,100,100);
 frame.add(credito);
 panel2.add(Box.createRigidArea(new Dimension(0,10)));
 panel2.add(money);
 panel2.add(Box.createRigidArea(new Dimension(0,5)));
 panel2.add(button);
 panel2.add(Box.createRigidArea(new Dimension(0,5)));
 panel2.add(button2);
 panel2.add(Box.createRigidArea(new Dimension(0,5)));
 panel2.add(button3);
 panel2.add(Box.createRigidArea(new Dimension(0,100)));
 panel2.add(opcion);
 panel2.add(Box.createRigidArea(new Dimension(0,5)));
 panel2.add(dispense);
 panel2.add(Box.createRigidArea(new Dimension(0,5)));
 panel2.add(dispense2);
 panel2.add(Box.createRigidArea(new Dimension(0,50)));
 panel2.add(retorno);
 panel2.add(Box.createRigidArea(new Dimension(0,5)));
 panel2.add(salir);
 frame.setSize(1260,725);
 frame.add(container);
 frame.setResizable(false);
 frame.setVisible(true);

 //////////////////////////////////////////Programa////////////////////

  while(dato.globalData!=-1){
  switch(dato.globalData){

	  case 0:
field.setText("$"+Integer.toString(dato.globalData));
	  label.setVisible(true);
	  label1.setVisible(false);
          label2.setVisible(false);
          label3.setVisible(false);
          label4.setVisible(false);
          label5.setVisible(false);
          label6.setVisible(false);
          label7.setVisible(false);
          label8.setVisible(false);
          label9.setVisible(false);
          break;

      case 5:
          field.setText("$."+Integer.toString(dato.globalData));
          label.setVisible(false);
	  label1.setVisible(true);
          label2.setVisible(false);
          label3.setVisible(false);
          label4.setVisible(false);
       label5.setVisible(false);
       label6.setVisible(false);
       label7.setVisible(false);
       label8.setVisible(false);
       label9.setVisible(false);
	   break;

	   case 10:
       field.setText("$."+Integer.toString(dato.globalData));
       label.setVisible(false);
       label1.setVisible(false);
       label2.setVisible(true);
       label3.setVisible(false);
       label4.setVisible(false);
       label5.setVisible(false);
       label6.setVisible(false);
       label7.setVisible(false);
       label8.setVisible(false);
       label9.setVisible(false);
	   break;

	   case 15:
       field.setText("$."+Integer.toString(dato.globalData));
       label.setVisible(false);
       label1.setVisible(false);
       label2.setVisible(false);
       label3.setVisible(true);
       label4.setVisible(false);
       label5.setVisible(false);
       label6.setVisible(false);
       label7.setVisible(false);
       label8.setVisible(false);
       label9.setVisible(false);
	   break;

	   	   case 20:
       field.setText("$."+Integer.toString(dato.globalData));
       label.setVisible(false);
       label1.setVisible(false);
       label2.setVisible(false);
       label3.setVisible(false);
       label4.setVisible(true);
       label5.setVisible(false);
       label6.setVisible(false);
       label7.setVisible(false);
       label8.setVisible(false);
       label9.setVisible(false);
	   break;

	   	   case 25:
       field.setText("$."+Integer.toString(dato.globalData));
       label.setVisible(false);
       label1.setVisible(false);
       label2.setVisible(false);
       label3.setVisible(false);
       label4.setVisible(false);
       label5.setVisible(true);
       label6.setVisible(false);
       label7.setVisible(false);
       label8.setVisible(false);
       label9.setVisible(false);
	   break;

	   case 30:
       field.setText("$."+Integer.toString(dato.globalData));
       label.setVisible(false);
       label1.setVisible(false);
       label2.setVisible(false);
       label3.setVisible(false);
       label4.setVisible(false);
       label5.setVisible(false);
       label6.setVisible(true);
       label7.setVisible(false);
       label8.setVisible(false);
       label9.setVisible(false);
	   break;

	   case 35:
       field.setText("$."+Integer.toString(dato.globalData));
       label.setVisible(false);
       label1.setVisible(false);
       label2.setVisible(false);
       label3.setVisible(false);
       label4.setVisible(false);
       label5.setVisible(false);
       label6.setVisible(false);
       label7.setVisible(true);
       label8.setVisible(false);
       label9.setVisible(false);
	   break;

	   case 40:
       field.setText("$."+Integer.toString(dato.globalData));
       label.setVisible(false);
       label1.setVisible(false);
       label2.setVisible(false);
       label3.setVisible(false);
       label4.setVisible(false);
       label5.setVisible(false);
       label6.setVisible(false);
       label7.setVisible(false);
       label8.setVisible(true);
       label9.setVisible(false);
	   break;

	   case 45:
       field.setText("$."+Integer.toString(dato.globalData));
       label.setVisible(false);
       label1.setVisible(false);
       label2.setVisible(false);
       label3.setVisible(false);
       label4.setVisible(false);
       label5.setVisible(false);
       label6.setVisible(false);
       label7.setVisible(false);
       label8.setVisible(false);
       label9.setVisible(true);
	   break;
  }

  }

 System.exit(0);
}



     static class Cinco implements ActionListener{

       
	  public void actionPerformed (ActionEvent e){
           GlobalDataStore dato1 = new GlobalDataStore();
           if(dato1.globalData<=45)
          dato1.globalData = dato1.globalData+5;


        
	  }

  }

          static class Diez implements ActionListener{


	  public void actionPerformed (ActionEvent e){
           GlobalDataStore dato1 = new GlobalDataStore();
           if(dato1.globalData<=35)
          dato1.globalData = dato1.globalData+10;

	  }
  }

          static class Quarter implements ActionListener{


	  public void actionPerformed (ActionEvent e){
           GlobalDataStore dato1 = new GlobalDataStore();
           if(dato1.globalData<=20)
          dato1.globalData = dato1.globalData+25;

	  }
  }
          static class Rturn implements ActionListener{

	  public void actionPerformed (ActionEvent e){
           GlobalDataStore dato1 = new GlobalDataStore();
          dato1.globalData = 0;

	  }
  }
          static class Dispense implements ActionListener{

	  public void actionPerformed (ActionEvent e){
          GlobalDataStore dato1 = new GlobalDataStore();
          if(dato1.globalData>=25)
          dato1.globalData = dato1.globalData-25;

	  }
  }
          static class Exit implements ActionListener{

	  public void actionPerformed (ActionEvent e){
          GlobalDataStore dato1 = new GlobalDataStore();
          dato1.globalData = -1;

	  }
  }

}

And this is the other class i have i only use this to get this variable work as a global variable

public class GlobalDataStore {

    public static int globalData = 0;

}

Recommended Answers

Well if you searched before posting you would have found for example this post by Ezzaral

Jump to Post

You really should not stuff all that GUI code into your main() method, but that's another discussion altogether.

You can still get a reference to the class statically and use getResource like this

Main.class.getResource(...)
Jump to Post

All 6 Replies

Well if you searched before posting you would have found for example this post by Ezzaral

I did, in fact i saw that post a few hours ago. when i try that i get this error:

"non static method getClass() cannot be referenced from a static context"

You really should not stuff all that GUI code into your main() method, but that's another discussion altogether.

You can still get a reference to the class statically and use getResource like this

Main.class.getResource(...)

do you mean something like this?

ImageIcon inicial=new ImageIcon(Main.class.getResource("Transicioni.jpg"))

It's working now :)... thanks for your time and for answering guys!

You can thank properly by closing thread (Mark this Thread as Solved under last post)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.