我添加使用的arraylist.When图像目录的图像列表添加,我的ScrollPane被crowded.How我能保持图像之间的间距?
这里是我的代码
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++;
}
尝试使用HBox
和VBox
。
基本上,他们就像您存储你的东西,你可以添加间隙插入它的小容器!
HBox ab = new HBox(10); <--The 10 is adding space (Answer to your question)
如果您要添加的东西到HBox中,简单的写
ab.getChildren().addAll(your content here);