gpt4 book ai didi

java - 如何使工具提示对鼠标事件透明?

转载 作者:搜寻专家 更新时间:2023-10-31 20:33:29 25 4
gpt4 key购买 nike

javafx.scene.Node 能够使其对鼠标事件透明,因此它不会被选为此类事件的目标:

Node.mouseTransparentProperty()
If true, this node (together with all its children) is completely transparent to mouse events. When choosing target for mouse event, nodes with mouseTransparent set to true and their subtrees won't be taken into account.

很遗憾,此功能尚未为 javafx.scene.control.Tooltip 实现。
有一个开放功能request为此 - 但似乎没有太多关于该主题的 Activity 。

我的问题是:是否有任何解决方法?如何使工具提示鼠标透明以将鼠标事件路由到基础控件?

最佳答案

如果有人还在寻找解决方案。我在 javafx-8 中发现了一个 hacky 方法(使用内部 API!!)。它已在 javafx-8+ 中进行了适当的修补,因此从可维护性的角度来看,这不是一个好的选择,但至少是:

    public static boolean correctNativeMouseEvent(MouseEvent event, Scene exclude)
{
Scene targetScene = getTargetScreen(event, exclude);
if(targetScene != null)
{
PickResultChooser chooser = new PickResultChooser();

targetScene.getRoot().impl_pickNode(new PickRay(event.getScreenX() - targetScene.getWindow().getX() - targetScene.getX(),
event.getScreenY() - targetScene.getWindow().getY() - targetScene.getY(),
1, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY), chooser);

PickResult res = chooser.toPickResult();
if(res != null)
{
Point2D pos = res.getIntersectedNode().localToScene(res.getIntersectedPoint().getX(), res.getIntersectedPoint().getY());

MouseEvent newEvent = new MouseEvent(null, null, event.getEventType(), pos.getX(), pos.getY(),
event.getScreenX(), event.getScreenY(),
event.getButton(), event.getClickCount(),
event.isShiftDown(), event.isControlDown(), event.isAltDown(), event.isMetaDown(),
event.isPrimaryButtonDown(), event.isMiddleButtonDown(), event.isSecondaryButtonDown(),
event.isSynthesized(), event.isPopupTrigger(), event.isStillSincePress(), res);

targetScene.impl_processMouseEvent(newEvent);
return true;
}
}
return false;
}

static Scene getTargetScreen(MouseEvent event, Scene exclude)
{
double x = event.getScreenX();
double y = event.getScreenY();

double sx, sy, sw, sh;

Iterator<Window> itr = Window.impl_getWindows();

if(itr.hasNext())
{
for(Window w = itr.next(); itr.hasNext(); w = itr.next())
{
sx = w.getX();
sy = w.getY();
sw = w.getWidth();
sh = w.getHeight();

if(sx < x && x < sx + sw
&& sy < y && y < sy + sh
&& w.getScene() != exclude)
return w.getScene();
}
}
return null;
}

在创建工具提示时,您只需添加以下内容:

Tooltip tp = new Tooltip();
// use filter to catch before anything can be consumed
tp.addEventFilter(MouseEvent.ANY, E -> {
// now correct the event
correctNativeMouseEvent(E, tp.getScene());
// although it is optionally, I would recommend to just consume the event anyways
E.consume();
};

关于java - 如何使工具提示对鼠标事件透明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31437758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com