gpt4 book ai didi

java - 按下按钮后获取图像对应的文件名

转载 作者:行者123 更新时间:2023-12-02 05:28:24 24 4
gpt4 key购买 nike

将一组图像文件添加到 File 类型的数组列表(filelist2)中。然后将 ImageView 和按钮添加到 vbox,这样的 vbox 使用 for 循环添加到gripane 的网格中。(迭代次数)等于文件列表的大小2)一旦按下按钮,我需要获取该vbox内图像的相应文件名。假设我按下了 (1,1) {i.e row no01 ,col no1} 处包含的按钮,我需要获取 (1,1) 处图像的文件名 这是屏幕截图:enter image description here

这是我的代码:FXMLController

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

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

}
btnar = new ArrayList<>();
for (int i = 0; i < filelist2.size(); i++) {
downloadbtn = new Button("Download");
btnar.add(downloadbtn);
final int index=i;
downloadbtn.setId(String.valueOf(index));
downloadbtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
try {
System.out.println("sssss");
downloadbtn.getId();
//System.out.println(filelist2.get(Integer.valueOf(downloadbtn.getId())).getName());

} catch (Exception ex) {
Logger.getLogger(HomeUI_2Controller.class.getName()).log(Level.SEVERE, null, ex);
}


}
});
}

System.out.println(filelist2.size());
gridpane.setAlignment(Pos.CENTER);
gridpane.setPadding(new Insets(20, 20, 20, 20));

gridpane.setHgap(20);
gridpane.setVgap(20);

ColumnConstraints columnConstraints = new ColumnConstraints();
columnConstraints.setFillWidth(true);
columnConstraints.setHgrow(Priority.ALWAYS);
gridpane.getColumnConstraints().add(columnConstraints);

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);
vb = new VBox();
vb.getChildren().addAll(pic, (Button) btnar.get(i));

gridpane.add(vb, imageCol, imageRow);
GridPane.setMargin(pic, new Insets(2, 2, 2, 2));
imageCol++;

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

}

最佳答案

为什么不简单

System.out.println(filelist2.get(index).getName());

(实际上,我根本不清楚为什么要创建 filelist2。为什么不这样做

btnar = new ArrayList<>();

for (int i=0; i < filelist1.length; i++) {
downloadbtn = new Button("Download");
btnar.add(downloadbtn);
final int index=i;
downloadbtn.setId(String.valueOf(index));
downloadbtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
try {
System.out.println("sssss");
System.out.println(filelist1[index].getName());

} catch (Exception ex) {
Logger.getLogger(HomeUI_2Controller.class.getName()).log(Level.SEVERE, null, ex);
}


}
});
}

关于java - 按下按钮后获取图像对应的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25780721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com