【问题标题】:adding images to a gridpane javafx将图像添加到网格窗格 javafx
【发布时间】:2014-10-12 00:45:56
【问题描述】:

我正在使用 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++;
}

【问题讨论】:

  • 阅读gridpane的javadoc。 “可选布局约束”部分。
  • 你能发一张你所面临问题的图片吗?
  • 我名声不好无法上传照片,我可以邮寄给你吗?
  • 我对 javascript 的经验很少,试试 .setMargin 肉食示例:image.setMargin(new Insets(2,2,2,2))
  • image.setMargin(new Insets(2,2,2,2)) 必须更正为 GridPane.setMargin(imageview, new Insets(2,2,2,2));跨度>

标签: image javafx space scrollpane gridpane


【解决方案1】:

尝试使用HBoxVBox

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 2019-03-27
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    相关资源
    最近更新 更多