将图像添加到gridpane javafx

时间:2014-08-19 01:57:10

标签: image javafx space scrollpane gridpane

我使用arraylist从目录中添加图像列表。添加图像时,我的ScrollPane变得拥挤。如何在图像之间保持间距?

这是我的代码

File file = new File("D:\\SERVER\\Server Content\\Apps\\icons");
File[] filelist1 = file.listFiles();
ArrayList<File> filelist2 = new ArrayList<>();
hb = new HBox();

for (File file1 : filelist1) {
     filelist2.add(file1);
}

System.out.println(filelist2.size());
gridpane.setPadding(new Insets(50,50,50,50));
gridpane.setHgap(20);
gridpane.setVgap(20);

int imageCol = 0;
int imageRow = 0;

for (int i = 0; i < filelist2.size(); i++) {
    System.out.println(filelist2.get(i).getName());
    image = new Image(filelist2.get(i).toURI().toString());
    pic = new ImageView();
    pic.setFitWidth(130);
    pic.setFitHeight(130);
    pic.setImage(image);
    hb.getChildren().add(pic);
    gridpane.add(pic, imageCol, imageRow );

    imageCol++;

    // To check if all the 4 images of a row are completed
    if(imageCol > 2){
      // Reset Column
      imageCol=0;
      // Next Row
      imageRow++;
}

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试使用HBoxVBox

基本上,它们就像存放你的东西的小容器,你可以在其中添加间隙!

HBox ab = new HBox(10); <--The 10 is adding space (Answer to your question)

如果你想在HBox中添加东西,只需写下

即可
ab.getChildren().addAll(your content here);