How do I load images in Jframe java(eclipse)?

后端 未结 4 1800
情书的邮戳
情书的邮戳 2021-01-03 16:58

I have an paneel.java file wich looks like the following code:

import java.awt.*;
import javax.swing.*;

public class Paneel extends JFrame
{
    public stat         


        
相关标签:
4条回答
  • 2021-01-03 17:38
    public static void main ( String [] args )
    {
        // frame
        JFrame frame = new Paneel();
        frame.setSize ( 1000, 1000 );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setTitle( "Remembory" );
    
        // Add following
        GifPaneel gifpan = new GifPaneel();
        gifpan.repaint();
        frame.add(gifpan);
    
    
        frame.setVisible( true );
    }
    
    0 讨论(0)
  • 2021-01-03 17:45

    you need to set a file path to the image..something like this

    final ImageIcon icon = new ImageIcon("C:\\Users\\you\\Desktop\\test.gif");
    
    0 讨论(0)
  • 2021-01-03 17:49

    Use this

    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class ImageInFrame {
        public static void main(String[] args) throws IOException {
            String path = "Image1.jpg";
            File file = new File(path);
            BufferedImage image = ImageIO.read(file);
            JLabel label = new JLabel(new ImageIcon(image));
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(label);
            f.pack();
            f.setLocation(200,200);
            f.setVisible(true);
        }
    }
    
    0 讨论(0)
  • 2021-01-03 17:52

    create package named as images on your project file and import image in that perticular package. Now, take a lable and select the icon property and select image from class path.

    0 讨论(0)
提交回复
热议问题