javafx图像显示使用HBox和VBox for for循环

时间:2016-11-27 04:03:24

标签: java for-loop javafx imageview

我将图片放在hbox中,HBox位于VBox

FileInputStream seats_fileInputStream = new FileInputStream("seat.png");
Image seats_image = new Image(seats_fileInputStream,50,50,false,false);

ImageView[] seats = new ImageView[30];

for(int i = 0;i<30;i++){
    seats[i] = new ImageView(seats_image);
}

HBox seatsRaw_hbox[] = new HBox[5];
VBox seatsLine_vbox = new VBox();

for(int i=0;i<5;i++){
    seatsRaw_hbox[i]= new HBox();
    for(int j=0;j<6;j++){
        seatsRaw_hbox[i].getChildren().addAll(seats[j]);             
    }

    seatsLine_vbox.getChildren().addAll(seatsRaw_hbox[i]);

} 

BorderPane Test = new BorderPane();
Test.setCenter(seatsLine_vbox);

in应在每个HBox中显示6张图片,其中总共有5 HBox个图像,并将它们放入VBox。但我一直只得到一行,我应该得到5行!

输出图片:

enter image description here

1 个答案:

答案 0 :(得分:0)

这有点傻了。在for循环中,我将j&lt; 6其中j表示图像数组索引,它假设从0开始一直到29,但它只是使用前6个并停止。我必须创建另一个变量,它将从0开始并一直到29.因此,这些是修改后的代码:

int seatsCount = 0;
for(int i=0;i<5;i++){
    seatsRaw_hbox[i]= new HBox();
    for(int j=0;j<6;j++){       
        seatsRaw_hbox[i].getChildren().addAll(seats[seatsCount]);
        seatsCount++;
    }
    seatsLine_vbox.getChildren().add(seatsRaw_hbox[i]);

}