我有一些按钮,并且我已向它们添加了工具提示。
Button button = new Button("Example");
Tooltip tt = new Tooltip("example");
button.setTooltip(tt);
默认情况下,当我将鼠标悬停在按钮上时,会弹出工具提示;但是,工具提示出现在光标下方。我正在寻找一种方法来更改工具提示的默认位置,当鼠标悬停在左右 10px 之类的位置时。如下所示:
tooltip.setLocation(mouseLocation+10, mouseLocation-10);
我查看了JavaFX: how to set correct tooltip position? 这并不是我真正想要的,因为它更多的是让工具提示相对于屏幕出现。
请您参考如下方法:
虽然这个问题有点老了,但仍然没有答案。
- 第 1 部分
What about something that is using
Button
current position in screen and set the tooltip position accordingly?
Button button = new Button();
button.setTooltip(new Tooltip("Hey i am a Button"));
button.getTooltip().setOnShowing(s->{
//Get button current bounds on computer screen
Bounds bounds = button.localToScreen(button.getBoundsInLocal());
button.getTooltip().setX(bounds.getMaxX());
button.getTooltip().setY(bounds.getMinY());
});
- 第 2 部分
(You can do also with mouse but add somewhere a mouseMoveListener,in the example it is added on the
Button
but i would prefer to add it on theStage
Scene
if we are speaking about a lot ofButton
)
double currentMouseX;
double currentMouseY;
button.setOnMouseMoved(m->{
currentMouseX=m.getScreenX();
currentMouseY=m.getScreenY();
});
button.getTooltip().setOnShowing(s->{
button.getTooltip().setX(currentMouseX+button.getTooltip().getWidth()+5);
button.getTooltip().setY(currentMouseY);
});
Finnaly:
实际上,在 IDE 中,您使用的是 button.getTooltip().setOn..
类型,您将看到可用于在显示或隐藏之前和之后控制工具提示的所有可用方法。