Swing:如何创建一个像 JToolTip 一样随鼠标移动的自定义小部件
Java Swing 问题。
我有一个显示图表的JPanel
。当我将鼠标移到该图表上时,我希望某些信息显示在随鼠标移动的类似工具提示的小部件上。我怎样才能最好地实现这一点?
我想如果我知道如何将自定义 JComponent
绝对定位在充当我的绘图画布的 JPanel
中,我的问题就会得到解决。然后我可以捕获鼠标移动事件并重新定位/更新小部件。任何其他解决方案(包括可能直接使用 JToolTip
)也将非常受欢迎!
Java Swing question.
I have a JPanel
which displays a graph. When I move the mouse over this graph, I want certain information to be displayed on a tooltip-like widget that moves with the mouse. How can I best implement this?
I guess my problem will be solved if I know how to position a custom JComponent
absolutely within the JPanel
that acts as my drawing canvas. I could then trap the mouse moved event and reposition/update the widget. Any other solution (including possibly using JToolTip
directly) would also be very much welcome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
重写
getToolTipText(MouseEvent)
方法以根据鼠标位置动态设置工具提示。编辑:
如果您希望工具提示随着鼠标不断移动,那么您还需要重写 getToolTipLocation() 方法。
Override the
getToolTipText(MouseEvent)
method to dynamically set the tool tip based on the mouse location.Edit:
If you want the tooltip to continually move with the mouse then you will also need to override the
getToolTipLocation()
method.