1

我有一个paneel.java 文件,它看起来像下面的代码:

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

public class Paneel extends JFrame
{
    public static void main ( String [] args )
    {
        // frame
        JFrame frame = new Paneel();
        frame.setSize ( 1000, 1000 );
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.setTitle( "Remembory" );
        frame.setVisible( true );
    }
    
    class Gifpaneel extends JPanel{
        private ImageIcon gif, animatedGif;
        
        public Gifpaneel() {
            gif = new ImageIcon( "test.gif" );
            animatedGif = new ImageIcon( "animaties/test.gif" );
        }       
        
        public void paintComponent( Graphics g ){
            super.paintComponent( g );
            
            gif.paintIcon( this, g, 100, 100 );
            animatedGif.paintIcon ( this, g, 250, 100 );
        }
        
    }
}

我想展示 test.gif 文件。我该怎么做?因为当我在 Eclipse 中运行它时,我只得到没有图像的 jframe。

4

4 回答 4

14

用这个

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);
    }
}
于 2013-09-18T11:47:09.113 回答
3

您需要设置图像的文件路径..类似这样

final ImageIcon icon = new ImageIcon("C:\\Users\\you\\Desktop\\test.gif");
于 2013-09-18T11:38:49.027 回答
2
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 );
}
于 2013-09-18T11:38:17.410 回答
-1

在您的项目文件上创建名为图像的包并在该特定包中导入图像。现在,取一个标签并选择图标属性并从类路径中选择图像。

于 2014-08-23T06:47:49.347 回答