使用java / javafx在文件名中插入for循环的i变量

时间:2014-06-21 12:47:46

标签: java javafx

我如何在下面的imageview中插入for循环中的i,而不是i来替换文件名中的12以便它可以变为;

ImageView Moon_img12 = new ImageView(new Image(getClass().getResourceAsStream("/Images/tiles/i.png")));

代码:

for(int i=0; i<=total_donnation; i++)
{
    ImageView Moon_img12 = new ImageView(new Image(getClass().getResourceAsStream("/Images/tiles/12.png")));

 } 

2 个答案:

答案 0 :(得分:1)

尝试

ImageView [] moon_images = // init array

//循环

ImageView Moon_img = new ImageView(new Image(getClass()。getResourceAsStream(“/ Images / tiles /”+ i +“。png

moon_images [i] = Moon_img;

答案 1 :(得分:0)

谢谢大家,我现在用你的帮助解决了这个问题。

   GridPane grid = new GridPane();
            int max_columns = 5;
            int current_row = 0;
            int current_column = 0;
            int total_donnation = 7;
            for(int i=1; i<=total_donnation; i++)
            {
                ImageView Moon_img = new ImageView(new Image(getClass().getResourceAsStream("/Images/tiles/"+i+".png")));
                grid.add(Moon_img, current_column,current_row);       
                current_column = current_column+1;
                if (current_column == max_columns )
                {
                    current_row = current_row+1;
                    current_column = 0;
                }
            }
            return grid;