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

  • Post author:
  • Post category:其他



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

运行结果如下:

在这里插入图片描述


参考资料



版权声明:本文为qq_40935723原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。