无法实例化类型 Image java?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18960758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:55:41  来源:igfitidea点击:

Cannot instantiate the type Image java?

javaimageswingembedded-resourceimageicon

提问by john brown

public Image  images[] = new Image[20];

    for(i=0; i<10; i++){
images[i]=new Image(getClass().getResource("/images/"+i+".jpg"));
        }

I am trying to add images to array but it gives the error Cannot instantiate the type Image j

我正在尝试将图像添加到数组,但它给出了错误无法实例化类型 Image j

What can be the reason?

原因是什么?

回答by Reimeus

Abstract classes cant be instantiated directly. You could use ImageIO.readwhich returns BufferedImage, a sub-class of Image

抽象类不能直接实例化。你可以使用ImageIO.readwhich returns BufferedImage,一个子类Image

void loadImages() throws IOException {

    for (int i = 0; i < 10; i++) {
        images[i] = ImageIO.read(getClass().getResource("/images/" + i + ".jpg"));
    }
}

回答by Julien

Imageis an abstract class and thus cannot be instancied. You should use one of the class who extend Image, like BufferedImageor VolatileImage.

Image是一个抽象类,因此不能被实例化。您应该使用扩展类之一Image,例如BufferedImageor VolatileImage

Source: Javadoc of Image

来源:图像的 Javadoc