开发者

loop to create a new ImageIcon - java

开发者 https://www.devze.com 2023-02-10 17:41 出处:网络
i have this piece of code ImageIcon[] Image = { new ImageIcon(\"../KingGame/src/game/img/1.gif\"), new ImageIcon(\"../KingGame/src/game/img/2.gif\"),

i have this piece of code

ImageIcon[] Image = { 
            new ImageIcon("../KingGame/src/game/img/1.gif"),
            new ImageIcon("../KingGame/src/game/img/2.gif"),
            new ImageIcon("../KingGame/src/game/img/3.gif"),
            new ImageIcon("../KingGame/src/game/img/4.gif"),
            new ImageIcon("../KingGame/src/game/img/5.gif"),
            new ImageIcon("../KingGame/src/game/img/6.gif"),
            new ImageIcon开发者_JAVA技巧("../KingGame/src/game/img/7.gif"),
            new ImageIcon("../KingGame/src/game/img/8.gif"),
            new ImageIcon("../KingGame/src/game/img/9.gif"),
};

an d i tried with the code below replace the script above

    ImageIcon image[] = new ImageIcon[9];

    for (int i = 1; i < image.length; i++) {
        new ImageIcon("../KingGame/src/game/img/"+i+".gif");
    }

but the result is...any image is loaded. what is the error?

thanks


You forgot to put new images into array:

image[i] = new ImageIcon("../KingGame/src/game/img/"+i+".gif");

Now it does the same thing as your old code.


It should be

for (int i = 0; i < image.length; i++) {
    image[i] =new ImageIcon("../KingGame/src/game/img/"+(i+1)+".gif");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号