public Image images[] = new Image[20];
for(i=0; i<10; i++){
images[i]=new Image(getClass().getResource("/images/"+i+".jpg"));
}
我正在尝试将图像添加到数组,但它给出了错误无法实例化类型 Image j
可能是什么原因?
public Image images[] = new Image[20];
for(i=0; i<10; i++){
images[i]=new Image(getClass().getResource("/images/"+i+".jpg"));
}
我正在尝试将图像添加到数组,但它给出了错误无法实例化类型 Image j
可能是什么原因?
抽象类不能直接实例化。你可以使用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"));
}
}
Image是一个抽象类,因此不能被实例化。Image您应该使用扩展、喜欢BufferedImage或的类之一VolatileImage。