在javafx中反序列化图像的数组列表后,我尝试向每个imageView添加eventhandler(imageView.setOnMouseClicked()),以便在单击imageView时,文本应显示在窗口的中心(或至少在控制台中打印smthng进行测试!) 当我打开使用directoryChooser的Eventhandler(setOnAction())时,此过程运行良好。 知道序列化和反序列化可以正常工作。我只希望至少在控制台上打印smthng。
这是我的代码的一部分: -反序列化后的事件处理部分: `
deserializeCurrentImages(currentImages);
if (currentImages.size() > 0) {
for (LoadedImage image : currentImages) {
ImageView imgView = image.getImageViewOfImage(image.getImageFile());
imgView.setOnMouseClicked((e) -> {
System.out.println("Image View clicked");
t.setText((String) imgView.getUserData());
});
gridpane.add(imgView, image.getPosition().getColumnIndex(), image.getPosition().getRowIndex());
}
bP.setCenter(gridpane);
}
`
这是DirectoryChooser事件处理程序的代码(在正常工作的地方): `
openFolder.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
int colIndex = 0;
int rowIndex = 0;
File file = directoryChooser.showDialog(primaryStage);
for (File subFile : file.listFiles()) {
Path path = Paths.get(file.getPath());
Position imagePosition = new Position(colIndex, rowIndex);
try {
LoadedImage loadedImage = new LoadedImage(subFile.getName(), Files.size(path), subFile,
imagePosition, file.getName());
currentImages.add(loadedImage);
colIndex++;
} catch (IOException e1) {
e1.printStackTrace();
}
}
serializeCurrentImages(currentImages);
currentImages.forEach(image -> {
ImageView imV = image.getImageViewOfImage(image.getImageFile());
imV.setOnMouseClicked((e) -> {
t.setText((String) imV.getUserData());
});
VBox vbox = addVBox();
gridpane.add(imV, image.getPosition().getColumnIndex(), image.getPosition().getRowIndex());
gridpane.add(vbox, image.getPosition().getColumnIndex(), 1);
});
bP.setCenter(gridpane);
}
});
`
谢谢。