Swing组件:使用JLable中的setIcon(new ImageIcon("xxx.bmp"))无法添加图片,如何解决?

查看swing的其他知识
因为最近要使用Swing中的JLable,去添加图片时发现对于图片格式为bmp的图片无法正常显示,对于图片格式为jpg,png的都可以正常显示出来,所以一开始感觉很奇怪,于是上网百度了一下,找到了解决方法,在这里记录一下,防止以后遇见这种问题不再出错。

下面就是我最初添加图片的代码:

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

public class Main {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		JFrame frame = new JFrame();
		frame.setBounds(0, 0, 852, 576);
		frame.getContentPane().setLayout(null);
		
		JLabel label = new JLabel("");
		label.setBounds(133, 13, 519, 451);
		frame.getContentPane().add(label);
		
		String path = "G:\\finallyDCTwartermark\\stzz.bmp";
		label.setIcon(new ImageIcon(path));

		frame.setVisible(true);
	}
}

运行结果,发现无法显示图片。
在这里插入图片描述
这是为什么呢?
我们来看看源码;
我们成成点开,
在这里插入图片描述
在这里插入图片描述
然后在查看getImage(filename)的源码,

这个比较长,所以我没用截图

/**
 * Returns an image which gets pixel data from the specified file,
 * whose format can be either GIF, JPEG or PNG.
 * The underlying toolkit attempts to resolve multiple requests
 * with the same filename to the same returned Image.
 * <p>
 * Since the mechanism required to facilitate this sharing of
 * <code>Image</code> objects may continue to hold onto images
 * that are no longer in use for an indefinite period of time,
 * developers are encouraged to implement their own caching of
 * images by using the {@link #createImage(java.lang.String) createImage}
 * variant wherever available.
 * If the image data contained in the specified file changes,
 * the <code>Image</code> object returned from this method may
 * still contain stale information which was loaded from the
 * file after a prior call.
 * Previously loaded image data can be manually discarded by
 * calling the {@link Image#flush flush} method on the
 * returned <code>Image</code>.
 * <p>
 * This method first checks if there is a security manager installed.
 * If so, the method calls the security manager's
 * <code>checkRead</code> method with the file specified to ensure
 * that the access to the image is allowed.
 * @param     filename   the name of a file containing pixel data
 *                         in a recognized file format.
 * @return    an image which gets its pixel data from
 *                         the specified file.
 * @throws SecurityException  if a security manager exists and its
 *                            checkRead method doesn't allow the operation.
 * @see #createImage(java.lang.String)
 */
public abstract Image getImage(String filename);

在这里我们来看看这段翻译的意思(百度翻译了解一下):
* Returns an image which gets pixel data from the specified file,
* whose format can be either GIF, JPEG or PNG.
百度翻译如下:
返回从指定文件获取像素数据的图像,该文件的格式可以是gif、jpeg或png。
所以我们可以看出,通过setIcon(new ImageIcon(“xxx.bmp”))这种方法是不能直接显示出bmp格式的图片的。

所以我们该如何解决呢?
源码如下:

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import java.awt.Image;
import java.io.File;
import java.io.IOException;

public class Main {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		JFrame frame = new JFrame();
		frame.setBounds(0, 0, 852, 576);
		frame.getContentPane().setLayout(null);
		
		JLabel label = new JLabel("");
		label.setBounds(133, 13, 519, 451);
		frame.getContentPane().add(label);
		
		String path = "G:\\finallyDCTwartermark\\stzz.bmp";
		
		File file = new File(path);
		Image image = ImageIO.read(file);
		label.setIcon(new ImageIcon(image));

		frame.setVisible(true);
	}
}

运行结果如下:
在这里插入图片描述
参考资料

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值