【问题标题】:How do I load images in Jframe java(eclipse)?如何在 Jframe java(eclipse) 中加载图像?
【发布时间】:2013-09-23 03:34:33
【问题描述】:

我有一个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。

【问题讨论】:

  • 您尚未添加创建 Gifpaneel 对象并将其添加到您的 JFrame。
  • 我该如何完成这项工作?
  • 请查看如何add images to Eclipse Project,以及此answer 了解更多信息。希望它有所帮助:-)

标签: java image swing gif paintcomponent


【解决方案1】:

使用这个

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

【讨论】:

    【解决方案2】:

    你需要为图像设置一个文件路径..像这样

    final ImageIcon icon = new ImageIcon("C:\\Users\\you\\Desktop\\test.gif");
    

    【讨论】:

    • 虽然是个好建议,但这不是这里的大问题。如果未设置绝对路径,它将使用相对于执行位置的路径。
    【解决方案3】:
    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 );
    }
    

    【讨论】:

    • 请不要显式调用paintComponent(...),这不是程序员的工作,只需调用gifpan.repaint(),它会隐式调用paintComponent(...) :-)
    • 实际上,在这种特定情况下,您甚至都不应该打电话给repaint(),因为frame.setVisible(true) 会隐含地这样做 :-) 其余的你最欢迎并保持微笑:-)
    【解决方案4】:

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

    【讨论】:

    • 仅供参考,Stackoverflow 只懂英文!
    • 这个答案将从一些示例代码中受益匪浅。照原样......这看起来像是回答问题的有效尝试,但这不是一个很好的尝试,所以我不希望有人支持它。即便如此,这也是一种尝试,所以我将其标记为“看起来不错”,以回应有人提出的建议将其删除的标志。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多