Sign in to follow this  

[java] JLabel Image

Recommended Posts

I'm having troubles getting my image to show up on screen. I've triple checked and (AFAIK) it is in the right directory. I'm not very experienced with content panes, so I feel that is the problem. Any help is appreciated:
import java.awt.*;
import javax.swing.*;
public final class Game {

    public static void createHero(Container content) {
        JLabel heroImage = new JLabel(new ImageIcon("hero.jpeg"));
    public static void main(final String[] args) {
        // MAIN WINDOW ++
        JFrame mainWindow = new JFrame("Game Window");
        Container content = mainWindow.getContentPane();
        // MAIN WINDOW --

Share this post

Link to post
Share on other sites
I've actually never used a "content" container before for something like this, but there are a few things that may be tripping this up.

First, I would try hard-coding the image path for now to make sure that it is actually pulling in the image ("C:\\hero.jpg") for example.

Second, I always have problems with layout managers. Try creating a JPanel first and adding the JLabel to that, then adding the JPanel to getContentPane().

Third, maybe you're only changing the local Container in createHero? Try returning it in your method and adding it to the contentPane instead.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this