鼠标事件后,为什么图像从标题窗格中消失?

时间:2015-12-22 12:36:31

标签: java javafx imageview

我有一个标题窗格,我在此窗格中显示图像,但图像非常大,所以我调整它以适应窗格。由于图像变小,用户可能希望以原始大小查看它,因此我在鼠标单击时实现了一种方法,在新的大窗口中打开原始图像。

    imgViewPicture.setFitWidth(titledPanePicture.getPrefWidth());
    imgViewPicture.setPreserveRatio(true);
    imgViewPicture.setSmooth(true);
    imgViewPicture.setCache(true);
    titledPanePicture.setContent(imgViewPicture);

问题是,当我点击titledPanePicture时,小图像消失,其内容被清除。为什么?对于解决方案,我在mouseclick事件中添加了内容的重新加载。但我不喜欢它,我认为titledPanePicture的内容应保持不变。

这是我的鼠标点击事件:

    Stage stage = new Stage();
    stage.initModality(Modality.APPLICATION_MODAL);
    Group root = new Group();
    Scene scene = new Scene(root);
    scene.setFill(Color.BLACK);
    HBox box = new HBox();
    imgViewPicture.setFitWidth(0);
    box.getChildren().add(imgViewPicture);
    root.getChildren().add(box); // before tPane

    stage.setTitle("Picture Original Size");
    stage.setScene(scene); 
    stage.sizeToScene(); 
    stage.show(); 
    reloadContentofTitledPane(); // This line is the fix but I find it unnecessary

提示将不胜感激。

1 个答案:

答案 0 :(得分:1)

您似乎在两个场景中使用相同的ImageView。但是,Node只能放在单个场景图中的单个位置。

为新Scene创建一个新节点。

ImageView newImageView = new ImageView(imgViewPicture.getImage());
...
box.getChildren().add(newImageView);