8

I am currently learning Java, and I am stuck with something at the moment.

I was looking for a way to add an image to my JFrame. I found this on the internet:

ImageIcon image = new ImageIcon("path & name & extension");
JLabel imageLabel = new JLabel(image); 

And after implementing it to my own code, it looks like this (this is only the relevant part):

class Game1 extends JFrame
{
    public static Display f = new Display();
    public Game1()
    {
        Game1.f.setSize(1000, 750);
        Game1.f.setResizable(false);
        Game1.f.setVisible(true);
        Game1.f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Game1.f.setTitle("Online First Person Shooter");

        ImageIcon image = new ImageIcon("C:\\Users\\Meneer\\Pictures\\image.png");
        JLabel imageLabel = new JLabel(image); 
        add(imageLabel);
        }
}

class Display extends JFrame
{
}

When running this code, it doesn't give me any errors, but it also doesn't show the picture. I saw some questions and people having the same problem, but their code was completely different from mine, they used other ways to display the image.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
user2988879
  • 379
  • 2
  • 6
  • 18

5 Answers5

8
  1. You don't need to use another JFrame instance inside the Game JFrame:
  2. Calling setVisible(flag) from the constructor is not preferable. Rather initialize your JFrame from outside and put your setVisible(true) inside event dispatch thread to maintain Swing's GUI rendering rules using SwingUtilities.invokeLater(Runnable)
  3. Do not give size hint by setSize(Dimension) of the JFrame. Rather use proper layout with your component, call pack() after adding all of your relevant component to the JFrame.
  4. Try using JScrollPane with JLabel for a better user experience with image larger than the label's size can be.

All of the above description is made in the following example:

     class Game1 extends JFrame
    {
       public Game1()
      {
         // setSize(1000, 750);  <---- do not do it
         // setResizable(false); <----- do not do it either, unless any good reason

         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setTitle("Online First Person Shooter");

         ImageIcon image = new ImageIcon("C:\\Users\\Meneer\\Pictures\\image.png");
         JLabel label = new JLabel(image);
         JScrollPane scrollPane = new JScrollPane(label);
         scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
         scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
         add(scrollPane, BorderLayout.CENTER);
         pack();
      }

     public static void main(String[] args)
     {
        SwingUtilities.invokeLater(new Runnable() {

           @Override
           public void run() {
              new Game1().setVisible(true);
           }
        });

      }
    }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Sage
  • 15,290
  • 3
  • 33
  • 38
5

do this after creating Jlabel

imageLabel.setBounds(10, 10, 400, 400);
imageLabel.setVisible(true);

also set the layout to JFrame

Game.f.setLayout(new FlowLayout);
AJ.
  • 4,526
  • 5
  • 29
  • 41
0

You are adding the label to the wrong JFrame. Also, move the setVisible() to the end.

import javax.swing.*;
class Game1 extends JFrame
{
    public static Display f = new Display();
    public Game1()
    {
        // ....
        Game1.f.add(imageLabel);
        Game1.f.setVisible(true);
    }
}
Vlad
  • 18,195
  • 4
  • 41
  • 71
0

Also try to use image from resources, and not from hardcoded path from your PC You can look in here, where sombody asked similar question about images in Jframe: How to add an ImageIcon to a JFrame?

Community
  • 1
  • 1
Khobar
  • 486
  • 7
  • 20
0

Your problem in next you add your JLabel to Game1 but you display another Frame(Display f). Change add(imageLabel); to Game1.f.add(imageLabel);.

Recommendations:

1)according to your problem: Game1 extends JFrame seems that Display is also a frame, use only one frame to display content.

2) use pack() method instead of setSize(1000, 750);

3)call setVisible(true); at the end of construction.

4)use LayoutManager to layout components.

alex2410
  • 10,904
  • 3
  • 25
  • 41