在类getPathGiveIcon中,将字符串数组传递给getPaths()方法。这个getPaths()将返回一个ImageIcons数组。在这个过程中,我试图从字符串数组中的每个路径名创建一个文件,但是在这一行中会出现错误。imgi = ImageIO.read(fa);//在这一行中,错误请引导我克服这个错误.
下面是getPaths()方法的完整代码。
public class GetPathGiveIcon {
ImageIcon[] iic;
File f = new File(" ");
int i = 0;
public ImageIcon[] getPaths(String[] s)
{
BufferedImage[] img = null;
for(String st : s)
{
System.out.println(st);
}
for(String st : s)
try
{
{
File fa = new File(st);
img[i] = ImageIO.read(fa);
System.out.println(" inside try block the value of every string = " + st);
}
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println(" Value of I before scaling " + i);
i = 0;
for(BufferedImage bi : img)
{
iic[i] = new ImageIcon(bi);
Image scaled = iic[i].getImage().getScaledInstance(150,150,
Image.SCALE_DEFAULT);
iic[i] = new ImageIcon(scaled);
i++;
}
System.out.println(" Value of I after scaling " + i);
return iic;
}
}发布于 2014-10-10 09:15:35
我总是等于0,所以您的代码将只填充数组的第一个元素。
以下是解决办法:
BufferedImage[] img = new BufferedImage[s.length];
for(String st : s)
{
try
{
File fa = new File(st);
img[i++] = ImageIO.read(fa);
System.out.println(" inside try block the value of every string = " + st);
}
catch(Exception e)
{
e.printStackTrace();
}
}编辑
与iic相同的问题是,必须初始化
iic = new ImageIcon[img.length];
i = 0;
for(BufferedImage bi : img)
{
iic[i] = new ImageIcon(bi);
// [...]发布于 2014-10-10 09:01:54
那是因为
BufferedImage[] img = null; 必须初始化img数组。
BufferedImage[] img = new BufferedImage[s.length]; https://stackoverflow.com/questions/26295574
复制相似问题