问题
I have the following .ico image, read using image4j library:
List<BufferedImage> BI = ICODecoder.read("aImage.ico");
Next I want to set this image as a frame icon:
myFrame.setIconImage((Image)BI);
Error: java.lang.ClassCastException
I need to convert the type List<\BufferedImage> to the type Image. Any help would be appreciated.
回答1:
You could consider using...
myFrame.setIconImage(BI.get(0));
List
is a list of stuff (or technically Object
s, in your case, BufferedImage
s), where as setIconImage
expects just one...
Alternatively, you could take advantage of of JFrame
's capability of providing multiple different images at different resolutions by using...
myFrame.setIconImages(BI);
Which is probably what you were after in the first place...
回答2:
In this code
List<BufferedImage> BI = ICODecoder.read("aImage.ico");
you are loading into a List
so when you try to do myFrame.setIconImage((Image)BI);
you will not be able to convert a list into an image.
try a .get(0)
on the list to return the Image.
来源:https://stackoverflow.com/questions/22061361/convert-listbufferedimage-to-image