Jump to content
  • Advertisement
Sign in to follow this  
Gallivan

[java] JLabel Image

This topic is 3813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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"));
        content.add(heroImage);
    }
    
    public static void main(final String[] args) {
        // MAIN WINDOW ++
        JFrame mainWindow = new JFrame("Game Window");
        mainWindow.setSize(1020,764);
        mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      
        Container content = mainWindow.getContentPane();
        content.setBackground(Color.white);
        mainWindow.setVisible(true);
        // MAIN WINDOW --
        createHero(content);
    }
}


Share this post


Link to post
Share on other sites
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!