Jbutton 按钮数字换成图片后,只有部分按钮有图片,有的按钮上的图片不能显示。
提示说数组越界,但具体不知道怎么回事。按钮数组是6*5,存储按钮位置的数组是8*7,调用图片的代码private static Icon[] icons = new ImageIcon[6*5];
private static final String imgDir="d:/shuiguo";
static{
try{
File dir = new File(imgDir);
File[] imgFiles = dir.listFiles(new FilenameFilter(){
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".jpg");
}
});
for(int i=0; i<5*6; i++){
icons[i]=new ImageIcon(imgFiles[i].getAbsolutePath());
}
}catch(Exception e){e.printStackTrace();}
}
实例化30个按钮的代码 diamondsButton[cols][rows] = new JButton(icons[grid[cols + 1][rows + 1]-1]);