一个非常简单的问题,我希望能有一个简单的答案。
我有一个有点复杂的自定义节点,它扩展了一个组。
当孩子徘徊时,如何阻止JavaFx为群组触发setOnMouseEntered
个事件?
setMouseTransparent
hackery似乎没有做到这一点。
答案 0 :(得分:0)
我做了一个简单的测试应用程序,而且该小组没有获得鼠标事件:
private static final class Foo extends Group {
Foo() {
final Rectangle blue = new Rectangle(100, 100, Color.BLUE);
blue.setLayoutX(50);
blue.setLayoutY(50);
blue.setOnMouseEntered(e -> System.out.println("Entered blue"));
final Rectangle green = new Rectangle(100, 100, Color.GREEN);
green.setLayoutX(250);
green.setLayoutY(90);
green.setOnMouseEntered(e -> System.out.println("Entered green"));
final Rectangle red = new Rectangle(100, 100, Color.RED);
red.setLayoutX(100);
red.setLayoutY(200);
red.setOnMouseEntered(e -> System.out.println("Entered red"));
getChildren().addAll(blue, green, red);
}
}
@Override
public void start(final Stage primaryStage) throws Exception {
final Pane root = new AnchorPane();
final Foo foo = new Foo();
root.getChildren().add(foo);
final Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
这是因为它实现了鼠标选择:
Node#contains
实现为
public boolean contains(double localX, double localY) {
if (containsBounds(localX, localY)) {
return (isPickOnBounds() || impl_computeContains(localX, localY));
}
return false;
}
和impl_computeContains
在Parent中定义,它是Group
protected boolean impl_computeContains(double localX, double localY) {
final Point2D tempPt = TempState.getInstance().point;
for (int i=0, max=children.size(); i<max; i++) {
final Node node = children.get(i);
tempPt.x = (float)localX;
tempPt.y = (float)localY;
try {
node.parentToLocal(tempPt);
} catch (NoninvertibleTransformException e) {
continue;
}
if (node.contains(tempPt.x, tempPt.y)) {
return true;
}
}
return false;
}