javafx 8重叠层事件处理

时间:2015-11-11 13:46:49

标签: javafx event-handling javafx-8

我的程序中有两层,每层都有不同的元素。两层重叠,但层中的元素不重叠。我希望在鼠标悬停在每个图层中的节点上时显示工具提示,但现在顶层只能获取事件。

下面附上一个最小的例子:

public class Main extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
    StackPane root = new StackPane();
    Pane p1 = new Pane();
    Pane p2 = new Pane();

    Arc arc = new Arc(150,150,100,100,0,360);
    arc.setType(ArcType.CHORD);
    arc.setFill(null);
    arc.setStroke(Color.BLUE);
    arc.setStrokeWidth(20);

    Rectangle rectangle = new Rectangle(100,100);
    rectangle.setX(100);
    rectangle.setY(100);

    Tooltip.install(arc, new Tooltip("Semiring"));
    Tooltip .install(rectangle,new Tooltip("Rectangle"));

    p1.getChildren().add(arc);
    p2.getChildren().add(rectangle);

    root.getChildren().addAll(p2,p1);
    primaryStage.setScene(new Scene(root, 300, 300));
    primaryStage.show();
}


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

Nothing happens on the rectangle

1 个答案:

答案 0 :(得分:1)

使用

p1.setPickOnBounds(false);

这实际上意味着如果鼠标位于p1中的非透明像素上,则鼠标事件仅传递到p1。因此,当鼠标不在弧上时,根据需要将鼠标处理委托给p2