我有两个javafx.scene.shape.Box
彼此相邻放置的实例:
我想在选择的节点发生变化时得到通知,即鼠标进入或退出红色或蓝色节点时。
将鼠标从黑色移动到任何彩色框都会正确触发“鼠标进入”事件(反之亦然,从红色/蓝色到黑色会触发“鼠标退出”事件)。
但是,将鼠标从红色直接移动到蓝色(或从蓝色直接移动到红色)不会触发任何事件。
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.stage.Stage;
public class Demo extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
Box redBox = new Box(100, 100, 100);
redBox.setId("red");
redBox.setMaterial(new PhongMaterial(Color.RED));
redBox.setTranslateX(100);
redBox.setTranslateY(100);
redBox.setTranslateZ(20);
Box blueBox = new Box(100, 100, 100);
blueBox.setId("blue");
blueBox.setMaterial(new PhongMaterial(Color.BLUE));
blueBox.setTranslateX(200);
blueBox.setTranslateY(100);
blueBox.setTranslateZ(20);
PointLight pointLight = new PointLight(Color.ANTIQUEWHITE);
pointLight.setTranslateX(800);
pointLight.setTranslateY(-100);
pointLight.setTranslateZ(-1000);
Group root = new Group(redBox, blueBox);
root.getChildren().add(pointLight);
root.setOnMouseEntered(me -> {
System.out.println("Entered " + resolvePickedId(me));
});
root.setOnMouseExited(me -> {
System.out.println("Exited " + resolvePickedId(me));
});
Scene scene = new Scene(root, 300, 200, true);
scene.setFill(Color.rgb(10, 10, 40));
scene.setCamera(new PerspectiveCamera(false));
stage.setScene(scene);
stage.show();
}
private static String resolvePickedId(MouseEvent me) {
Node intersectedNode = me.getPickResult().getIntersectedNode();
return intersectedNode == null ? "(none)" : intersectedNode.getId();
}
}
我错过了什么?