如何在JFrame中加入图片

本人目前使用过两种方法
1.使用imageicon类,添加Jlable中然后,将Jlabel加入JFrame中;

imageIcon=new ImageIcon("C:\\Users\\23641\\Pictures\\Saved Pictures\\Image.jpg");//插入图片
labelImage=new JLabel(imageIcon);

2.使用jframe中的paint方法,画笔类graphic 对象g,g.drawimage(x,y,obeserver),obersever一般设置为空即没有问题;

public class GameUtil {
	private GameUtil(){
		
	}
	public static Image getImage(String path)
	{
		BufferedImage bi=null;
		try {
			URL url=GameUtil.class.getClassLoader().getResource(path);
			bi=ImageIO.read(url);
		}catch(IOException e){
			e.printStackTrace();
		}
		return bi;
	}

}
Image myImage=GameUtil.getImage("image/Image.jpg");
g.drawImage(myImage, 0, 0, null);

猜你喜欢

转载自blog.csdn.net/weixin_40867255/article/details/83018203