問(wèn)題描述
是否有正確的方法來(lái)解決兩個(gè)同級(jí)窗格之間的事件傳播問(wèn)題?
Is there a correct way to solve the problem with event propagation between two sibling panes?
例如,我們有內(nèi)部有 2 個(gè)窗格的 StackPane.
For example we have StackPane with 2 panes inside.
StackPane p = new StackPane();
Region p1 = new Region();
Region p2 = new Region();
p.getChildren().addAll(p1, p2);
此示例中的 p2 捕獲鼠標(biāo)事件,即使未消耗事件,p1 也無(wú)法對(duì)其做出反應(yīng).
p2 in this example capture mouse events and p1 can't react on it even if event is not consumed.
如果事件不被 p2 消費(fèi),是否有正確的方法將事件傳播到 p1?
Is there a correct way to propagate event to p1 if it not consumed by p2?
setMouseTransparent 不能解決我的問(wèn)題,因?yàn)槲倚枰獌蓚€(gè)子元素都對(duì)鼠標(biāo)做出反應(yīng).
setMouseTransparent not solve my problem because I need that both children elements react on mouse.
感謝您的建議.
推薦答案
我的問(wèn)題部分解決了.也許我不太正確地提出問(wèn)題.我編寫(xiě)了圖形編輯器之類(lèi)的應(yīng)用程序,并在帶有指南、網(wǎng)格、選擇工具等的 stackpane 上有工具層窗格,并且需要該層的子層可以處理鼠標(biāo),并且窗格本身對(duì)于鼠標(biāo)事件是透明的.
My problem was partially solved. Maybe I not quite correctly formulate question. I write app like graphic-editor and have tools-layer panes on stackpane with guides, grid, selection-tools etc. and need that children of this layers can handle mouse and panes itself will be transparent for mouse events.
問(wèn)題已通過(guò)覆蓋 pickNode 解決,而不是在公共 API 中,但它可以工作.也許可以幫助某人.
Problem was solved by override pickNode, not in public API, but it work. Maybe help somebody.
protected Node impl_pickNodeLocal(double localX, double localY) {
if (containsBounds(localX, localY)) {
ObservableList<Node> children = getChildren();
for (int i = children.size()-1; i >= 0; i--) {
Node picked = children.get(i).impl_pickNode(localX, localY);
if (picked != null) return picked;
}
// hack to make pane itself transparent for mouse
// if (contains(localX, localY)) return this;
}
return null;
}
這篇關(guān)于JavaFX 2 事件分派到底層節(jié)點(diǎn)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!