Adding Image to JFrame

So, I am using Eclipse with the Windows linker. I'm just wondering if in any case I can import an image that appears on a JFrame, which I can easily move and resize, instead of setting the location and size and drawing it.

+8
java image background swing jframe
source share
3 answers

There is no specialized image component in Swing (which, in my opinion, is sad). So there are several options:

  • As @Reimeus said: Use JLabel with an icon.
  • In the window builder, create a JPanel that will represent the location of the image. Then add your own image component to JPanel using a few lines of code that you never have to change. They should look like this:

    JImageComponent ic = new JImageComponent(myImageGoesHere); imagePanel.add(ic); 

    where JImageComponent is a self-created class that extends JComponent , which overrides the paintComponent() method to paint the image.

+14
source share

Here is a simple example of adding an image to a JFrame :

 frame.add(new JLabel(new ImageIcon("Path/To/Your/Image.png"))); 
+14
source share

If you are using Netbeans for development, use jLabel and change its icon property.

+2
source share

All Articles