本文介绍了Stackpane - MouseClick由其子节点监听的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我正在尝试根据描述构建图片库。

I am trying to build a image gallery based on the description here.

我有 StackPane ScrollPane AnchorPane 作为其子项,以相同的顺序添加。

I have a StackPane, with ScrollPane and AnchorPane as its children, added in the same order.

ScrollPane 包含ImageView中包含的图像列表,而 Anchorpane 有两个按钮,固定在左右两角,用于滚动图像!

ScrollPane contains a list of images contained in ImageView's, while the Anchorpane has two buttons, anchored to the left and right corners, to scroll the images !

问题

滚动工作正常,因为按钮放在AnchorPane上,这是StackPane上的顶级子项。

The scrolling is working fine, as the buttons are placed on the AnchorPane, which is the top child on the StackPane.

我想实现双击imageView以全屏打开它即可。由于 imageView ScrollPane 的子项,因此无法侦听鼠标事件。

I want to implement the double-click on the imageView to open it in fullscreen. Since the imageView is a child of ScrollPane, it is not able to listen to the mouse event.

是有一种方法可以使ImageView监听mouseEvent吗?

Is there a way through which I can make the ImageView listen to the mouseEvent ?

推荐答案

而不是放置 AnchorPane ScrollPane StackPane 中,尝试使用 AnchorPane 作为此结构的根。首先使用适当的锚点将 ScrollPane 添加到它,然后添加按钮。这样按钮仍然位于顶部,但是滚动窗格的内容上没有任何东西干扰鼠标事件。

Instead of placing the AnchorPane and ScrollPane in a StackPane, try using the AnchorPane as the root of this structure. Add the ScrollPane to it first with appropriate anchors and then add the buttons. That way the buttons will still be on top, but there will be nothing interfering with the mouse events on the content of the scroll pane.

ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(...);


// scrollPane fills entire anchor pane:

AnchorPane.setLeftAnchor(scrollPane, 0.0);
AnchorPane.setRightAnchor(scrollPane, 0.0);
AnchorPane.setTopAnchor(scrollPane, 0.0);
AnchorPane.setBottomAnchor(scrollPane, 0.0);

Button button1 = new Button(...);
Button button2 = new Button(...);

// button1 in top left:
AnchorPane.setTopAnchor(button1, 0.0);
AnchorPane.setLeftAnchor(button1, 0.0);

// button2 in top right:
AnchorPane.setTopAnchor(button2, 0.0);
AnchorPane.setRightAnchor(button2, 0.0);

AnchorPane anchorPane = new AnchorPane();
anchorPane.getChildren().addAll(scrollPane, button1, button2);

这篇关于Stackpane - MouseClick由其子节点监听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 05:58