Search code examples
javaswingjpanelimageicon

Adding an image into a GUI made with Eclipse


I'm still pretty new to Java, and in our programming class we are working in a group. My part of the assignment is to insert an image at the top part of the GUI.

this is the code I have so far..

ImageIcon image = new ImageIcon(getClass().getResource("EXTS.png"));
    JPanel.add(image, BorderLayout.NORTH);

but right under the .add part of the Jpanel.add is that red squiggly line telling me that I should change my Image to a component and when I do that it tells me to switch it back to an Image?? this is what I'm getting confused on why would it tell me to change it back if it won't use it the way it is now? So I guess my question is what should I do to fix this problem?

Also exactly how would I position it, I know it goes into the layout spot in the North but will that be dead center? or does it start from the 0,0 top left and then pixel in?

Thank you in advanced!

(P.s. this is the path to the image file if it should be different please tell me otherwise it's fine -- Project 3/Images/EXTS.png)


Solution

  • An Icon is not a component. You need to add the Icon to a component like a JLabel:

    ImageIcon image = new ImageIcon(getClass().getResource("EXTS.png"));
    //JPanel.add(image, BorderLayout.NORTH);
    JPanel.add(new JLabel(image), BorderLayout.NORTH);