JavaFX节点未注册鼠标事件 [英] JavaFX Nodes Not Registering Mouse Events
问题描述
我已经准备好把头发拉出来了。我正在研究游戏GUI界面,它有一个由单元网格组成的地图。
I'm ready to pull my hair out over this. I'm working on game GUI interface which has a map consisting of a grid of cells.
单元格网格由StackPane组成,然后由几个层组成ImageViews或Shapes等。
The grid of cells is composed of a StackPane and then several layers inside consisting of either ImageViews or Shapes etc.
整个东西包含在GridPane中,它包含BorderPane的中心元素;
The whole thing is contained in a GridPane, which comprises the center element of a BorderPane;
我不能将鼠标事件添加到底层场景,因为所有节点都得到它 - >没有好处。我需要突出显示鼠标下的节点。
I can't add a mouse event to the underlying scene because then all the nodes get it -> no good. I need to highlight the nodes under the mouse.
这是创建堆栈的代码:
private void initializePanes() {
myParent = new StackPane();
myObjectLayer = new StackPane();
myOverlayLayer = new StackPane();
myParent.getChildren().addAll(myObjectLayer, myOverlayLayer);
myParent.getStylesheets().add(myConfig.getString("StyleSheet"));
}
以下是设置EventHandler的代码 - >无效或寄存器。我也试过了EventFilters - 没有骰子,除非我写入底层场景,这是一个禁忌。
and Here's the code to set an EventHandler -> nothing works or registers. I've tried EventFilters too - no dice, not unless I write to the underlying scene, which is a No-Go.
private void initializeHandlers() {
myParent.addEventHandler(MouseEvent.ANY, (e) -> System.out.println("I'm responding"));
}
帮助将不胜感激!
编辑:这是问题所在,整个应用程序的布局如下:
EDIT: here's the issue, The overall application is laid out as so:
BorderPane:
中心 - > StackPane
在这个堆栈窗格中我有一个ScrollPane,它包含一个GridPane,其中包含单元格(我在上面发布了代码)。
BorderPane: Center -> StackPane Within that stackpane I have a ScrollPane, which contains a GridPane, which contains the cells (which I posted code for above).
我有点JavaFX的新手,所以这可能不是一个很好的方法。但由于某些原因,鼠标事件不会使它超过底层堆栈窗口 - >即它们被边框的中心面板吸收。我无法将事件处理程序添加到滚动窗格,网格窗格或gridpane中的任何单元格。
I'm somewhat new to JavaFX so maybe this isn't a good way to do this. But for some reason, mouse events do not make it past the underlying stackpane -> i.e. they get absorbed by the center panel of the borderpane. I cannot add event handlers to the scroll pane, the grid pane, or any of the cells within the gridpane.
推荐答案
想出来。
主堆栈窗格中有另一层用于Map - >它正在拦截调用。删除它 - 我会弄清楚如何在以后实现它。
Figured it out. There was another layer in the main stack pane for the Map -> it was intercepting the calls. Removed it - I'll figure out how to implement it later.
这篇关于JavaFX节点未注册鼠标事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!