javafx - 鼠标事件在底层被忽略

标签 javafx

我有两层(= AnchorPanes),其中一层与 StackPane 堆叠在一起。所以这两个层都填满了整个场景。问题是,只有顶层接收鼠标事件。

这就是场景的构建方式:

Layout of the Scene

只有按钮 B 接收点击事件,但按钮 A 没有。

Button A don't Receive Click Events

如果我将图层 B 设置为鼠标透明 ( layerB.setMouseTransparent(true) ),按钮 A 将接收鼠标事件。但是鼠标透明效果也适用于所有 child ,因此按钮 B 不再接收鼠标事件。

如何让按钮 A 和按钮 B 接收鼠标事件?

这是完整的工作来源:

public class LayerTest extends Application {
    @Override
    public void start(final Stage primaryStage) throws Exception {
        final Node layerA = createLayerA();
        final Node layerB = createLayerB();
        final Parent root = new StackPane(layerA, layerB);
        final Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.setWidth(250);
        primaryStage.setHeight(200);
        primaryStage.show();
    }

    private Node createLayerA() {
        final AnchorPane layerA = new AnchorPane();
        final Button buttonA = new Button("Button A");
        AnchorPane.setLeftAnchor(buttonA, 10d);
        AnchorPane.setTopAnchor(buttonA, 10d);
        buttonA.setOnMouseClicked(e -> System.out.println("Button A clicked"));
        layerA.getChildren().setAll(buttonA);
        return layerA;
    }

    private Node createLayerB() {
        final AnchorPane layerB = new AnchorPane();
        final Button buttonB = new Button("Button B");
        AnchorPane.setRightAnchor(buttonB, 10d);
        AnchorPane.setBottomAnchor(buttonB, 10d);
        buttonB.setOnMouseClicked(e -> System.out.println("Button B clicked"));
        layerB.getChildren().setAll(buttonB);
        return layerB;
    }

    public static void main(String[] args) {
        launch(args);
    }
}

最佳答案

解决方案

将以下行添加到您的示例代码中:

layerB.setPickOnBounds(false);

这将允许鼠标与您可以通过堆叠元素的层看到的可见元素进行交互。

如果顶层元素与底层元素重叠,则单击顶层与底层重叠的部分将使顶层消耗鼠标事件,而底层不会接收它(这可能是您想要的) .

替代口译

如果您真的想拦截和处理所有层中的鼠标事件,请查看 Uluk 评论中的链接问题:
  • JavaFX 2 event dispatching to underlying nodes
  • JavaFx, event interception/consumption

  • 方法说明

    setPickOnBounds 的描述方法:

    Defines how the picking computation is done for this node when triggered by a MouseEvent or a contains function call. If pickOnBounds is true, then picking is computed by intersecting with the bounds of this node, else picking is computed by intersecting with the geometric shape of this node.



    Panes have no visible background by default, so why they should consume mouse events?



    对于 modena.css(JavaFX 8 附带的默认样式表), Pane 实际上默认具有非常微弱的阴影背景,因此它们可以使用鼠标事件。为了防止这种情况,您可以将 Pane 的背景颜色设置为 null 或将 Pane 设置为 mouseTransparent .

    此行为在 JavaFX 2 和 JavaFX 8 之间发生了变化。JavaFX 2 附带一个名为 caspian.css 的默认样式表,它不会为 Pane 设置背景。

    关于javafx - 鼠标事件在底层被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24607969/

    相关文章:

    css - JavaFX TextArea 垂直对齐

    css - javafx和css如何使用变量

    java - 我想要可以在 Windows、Mac 和 Linux 中打开“打开方式”对话框的 java 程序?

    java - 打开文件并将其内容设置为 JavaFX NullPointer 中的 TextArea

    java - 通过相对路径名加载图像

    java - Java fx webview 中的 Google 身份验证

    ios - 在Application Loader上上传ipa时出错

    JavaFX 使用 SVGPath 作为掩码

    JavaFX 8.0 : How to change the focus traversal key?

    javafx场景构建器滚动 Pane