Display image in JFrame

I'm currently learning Java, and at the moment I'm stuck.

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 embedding it in 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 does not give me any errors, but also does not show the image. 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.

+4
source share
5 answers

do it after creating Jlabel

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

also sets the layout for jframe

Game.f.setLayout(new FlowLayout);
+2
source
  • JFrame Game JFrame:
  • setVisible(flag) . JFrame setVisible(true) GUI Swing SwingUtilities.invokeLater(Runnable)
  • setSize(Dimension) JFrame. , pack() JFrame.
  • JScrollPane JLabel , , .

:

     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);
           }
        });

      }
    }
+6

JFrame. , setVisible() .

import javax.swing.*;
class Game1 extends JFrame
{
    public static Display f = new Display();
    public Game1()
    {
        // ....
        Game1.f.add(imageLabel);
        Game1.f.setVisible(true);
    }
}
0

, , - Jframe: ImageIcon JFrame?

0

JLabel Game1, (Display f). add(imageLabel); Game1.f.add(imageLabel);.

:

1) : Game1 extends JFrame , Display , .

2) pack() setSize(1000, 750);

3) setVisible(true); .

4) LayoutManager .

0
source

All Articles