我在加载图片的时候遇到一个问题,代码如下:
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class ShowImage extends JFrame{
private static final int IMAGE_NUMBER=6;
private Image[] image = new Image[IMAGE_NUMBER];
public ShowImage(){
for(int i=1;i<=IMAGE_NUMBER;i++){
try{
//File file = new File("pictures//"+String.valueOf(i)+".jpg");
// image[i-1]=ImageIO.read(file);
image[i-1] = Toolkit.getDefaultToolkit().createImage("pictures//"+i+".jpg");
}
catch(Exception ex){
ex.printStackTrace();
}
}
Random random = new Random();
ImageIcon oneIcon = new ImageIcon(image[random.nextInt(IMAGE_NUMBER)]);
this.add(new JLabel(oneIcon));
this.setTitle("随机显示图片");
this.setSize(600,400);
this.setLocationRelativeTo(null);//设置窗口居中显示
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args){
new ShowImage();
}
}
如果用紫色部分加载图片运行程序的时候出现java.lang.IllegalArgumentException: bandOffsets.length is wrong!异常,
用注释部分加载就没有问题,请问下这是怎么回事。
|
|