【问题标题】:Stackpane - MouseClick to be listened by both its childrenStackpane - MouseClick 被它的两个孩子收听
【发布时间】:2014-07-01 12:54:34
【问题描述】:

背景

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

我有一个StackPane,其中ScrollPaneAnchorPane 作为其子级,以相同的顺序添加。

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

问题

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

我想实现双击imageView全屏打开。由于 imageViewScrollPane 的子对象,因此无法监听鼠标事件。

有没有办法让 ImageView 监听 mouseEvent ?

【问题讨论】:

  • 您的布局要求对我来说不是很清楚。按钮似乎漂浮在ScrollPane 上方的想法? AnchorPane 是接收鼠标事件并阻止它们传播到按钮的问题吗?
  • 没错,按钮应该浮动在 ScrollPane 上。作为 AnchorPane 的一部分的按钮能够接收 MouseEvents。但是,位于 AnchorPane 下方的滚动窗格无法接收它。我希望 ScrollPane(或者更具体地说,它的子项 ImageViews)接收鼠标事件

标签: javafx


【解决方案1】:

不要将AnchorPaneScrollPane 放在StackPane 中,而是尝试使用AnchorPane 作为此结构的根。首先使用适当的锚将ScrollPane 添加到其中,然后添加按钮。这样按钮仍将位于顶部,但不会干扰滚动窗格内容上的鼠标事件。

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);

【讨论】:

  • 这非常适合这部分,但我想知道,是否有办法让first child of StackPane 收听事件
  • 如果您使用您的原创设计,请尝试在AnchorPane 上致电setMouseTransparant(true)
  • 嗯,我已经试过了。在这种情况下,AnchorPane 上的buttons 也会停止响应MouseEvents
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-15
  • 1970-01-01
相关资源
最近更新 更多