-1

I am trying to set the JFrame icon to a custom image I have prepared. I am, however, getting this error when I write this code;


import javax.swing.ImageIcon;
import javax.swing.JFrame;



public class Game
{
    
    
    ImageIcon logo = new ImageIcon(getClass().getClassLoader().getResource("BounceAroundIcon.png"));
    

    public static void main (String[] args) 
    
    {

        

        
        JFrame window = new JFrame();
        
        window.setContentPane(new GamePanel());
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setResizable(false);
        window.pack();
        window.setVisible(true);
        
        
    }
    
}

This has worked with another project of mine, but here it will not work. I'm new to Java programming, so sorry if this is obviously wrong.

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
Boaclick
  • 7
  • 1
  • what is the error? – Lakindu Hewawasam Dec 21 '20 at 16:45
  • You are not doing anything with the `logo` variable. Shouldn't there be a `window.setItcon()` or something? You could pass the `logo` there. – jpllosa Dec 21 '20 at 17:00
  • (1-) *This has worked with another project of mine, but here it will not work.* - so then compare the working code with the code that is not working to determine the difference. – camickr Dec 21 '20 at 17:15

1 Answers1

1

The class JFrame has a method setIconImage(Image). Therefore you need to get an Image from your ImageIcon. Fortunately the ImageIcon class has a method getImage() returning an Ìmage`object.

So, putting these two things together, you need to do:

window.setIconImageImage(logo.getImage());
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49