1

I've been trying to add an image to a JFrame but I can't seem to get it done.

I looked at online tutorials and other similar questions but nothing seems to work.

ImageIcon wiz = new ImageIcon("wizard.png");
ImageIcon assassin = new ImageIcon("assassin.png");

JFrame frame = new JFrame("Select");
frame.setBounds(50, 50,1000, 1000);
JButton w = new JButton("Wizard");
JButton a = new JButton("Assasin");

JFrame f = new JFrame("Image");

JLabel img1 = new JLabel(wiz);

frame.setLayout(null);
f.setLayout(null);
f.setIconImage(wiz.getImage());

w.setBounds(30,380,100,60);
frame.add(w);

a.setBounds(200, 380, 100, 60);
frame.add(a);

f.setVisible(true);
frame.setVisible(true);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Syed
  • 17
  • 2
  • 1
    You add one `ImageIcon` to a `JLabel` but you don't add that `JLabel` to any of the frames. – Titus Jan 12 '19 at 00:54
  • I just did that but still nothing appears – Syed Jan 12 '19 at 01:24
  • Have you set `img1`'s bounds ? – Titus Jan 12 '19 at 01:25
  • would that be with .setBounds()? – Syed Jan 12 '19 at 01:26
  • Yes, just like for the other components. – Titus Jan 12 '19 at 01:27
  • img1.setBounds(200, 380, 100, 60); Added this but still doesn't appear – Syed Jan 12 '19 at 01:30
  • Is your image called "wizard.png"? (Extension is important) Is it in the same folder? BTW Don't use `null-layout` – Frakcool Jan 12 '19 at 01:30
  • Do you want to set the icon of `JFrame` (shown in top left corner)? Or do you want to add the image to `JFrame`'s content area? – Prasad Karunagoda Jan 12 '19 at 01:30
  • Right now I just want the image itself to appear in JFrame and will worry about the positioning once I can get it to appear – Syed Jan 12 '19 at 01:32
  • What @PrasadKarunagoda is saying is "You want to display the APP ICON (IconImage) or as an Image inside the area?" – Frakcool Jan 12 '19 at 01:33
  • Oh ok, I want to display it as an image inside the area – Syed Jan 12 '19 at 01:35
  • Thank you guys for the help, I think I got it to work – Syed Jan 12 '19 at 01:43
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. [This answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). 3) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. .. – Andrew Thompson Jan 12 '19 at 02:26
  • .. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 4) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Jan 12 '19 at 02:26

1 Answers1

4

I think the main problem in your program is that you try absolute positioning of components (e.g. JLabel) using setLayout(null) and setBounds() on components.

In Swing, the correct way to place components is by using layout managers. See this tutorial for details about how to use layout managers: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

As a sample program, I've successfully set images (both as JFrame's icon and inside JFrame's contents area) in below program. Try it and see.

This is a screenshot of my sample JFrame.

enter image description here

import javax.swing.*;

public class FrameWithIcon
{
  public static void main(String[] args)
  {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Since I'm not setting a layout manager to contentPane, it's default (BorderLayout) is used

    //This sets the image in JFrame's content area
    f.getContentPane().add(new JLabel(new ImageIcon("star.png")));

    //This sets JFrame's icon (shown in top left corner of JFrame)
    f.setIconImage(new ImageIcon("star.png").getImage());

    f.setBounds(300, 200, 400, 300);
    f.setVisible(true);
  }
}
Prasad Karunagoda
  • 2,048
  • 2
  • 12
  • 16