如何在Jframe java(eclipse)中加载图像? [英] How do I load images in Jframe java(eclipse)?
本文介绍了如何在Jframe java(eclipse)中加载图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个panelel.java文件,看起来像下面的代码:
I have an paneel.java file which looks like the following code:
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.
I would like to show the test.gif file. How do I get this done? because when I run it in eclipse I only get the jframe with no image in it.
推荐答案
使用此
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);
}
}
这篇关于如何在Jframe java(eclipse)中加载图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文