Problems with Image in JLabel

1

I'm doing a simulator and I'm trying to put an image in a JLabel and then that JLabel add it to a JFrame with a background image, the frame with the background image is in another class, the problem is that when I execute the program does not appear the image in the JFrame this is the code where I added the JLabel

public static void CreaTaquillero(JLabel taquilleros, int x) {
          ventanasim.setLayout(null);
          Image img= new 
          ImageIcon("C:\Users\Luis\Desktop\icon.png").getImage();
          ImageIcon img2=new ImageIcon(img.getScaledInstance(100, 84, 
          Image.SCALE_SMOOTH));

          taquilleros.setIcon(img2);
          taquilleros.setBounds(1100,10,30,30);
          taquilleros.setSize(taquilleros.getPreferredSize());
          ventanasim.add(taquilleros);
    }
    
asked by Luis Daniel 17.05.2018 в 00:52
source

1 answer

-1

Reviewing your code I suggest, verify that your image is actually on the specified route, check the name and extension of the file are correct:

"C:\Users\Luis\Desktop\icon.png"

The code is correct in terms of scaling the image.

In addition to debugging, you can also validate if the image is not found by displaying a message in JLabel :

public static void CreaTaquillero(JLabel taquilleros, int x) {
          ventanasim.setLayout(null);
          Image img= new 
          ImageIcon("C:\Users\Luis\Desktop\icon.png").getImage();
          ImageIcon img2=new ImageIcon(img.getScaledInstance(100, 84, 
          Image.SCALE_SMOOTH));

        if (img2 != null) {
          taquilleros.setIcon(img2);
          taquilleros.setBounds(1100,10,30,30);
          taquilleros.setSize(taquilleros.getPreferredSize());    
        } else {
                taquilleros.setText("NO SE ENCUENTRA IMAGEN EN RUTA ESPECIFICADA!");
        }

          ventanasim.add(taquilleros);

    }
    
answered by 17.05.2018 в 01:16