重定向 Swing 鼠标事件

发布于 2024-11-14 10:46:06 字数 360 浏览 4 评论 0原文

我试图使在 Java3D 画布上显示 Java Swing 组件并与之交互成为可能。我通过将透明 JPanel 绘制到缓冲图像来显示组件,然后使用 J3DGraphics2D 在画布上绘制该缓冲区。

我不明白的是如何将鼠标事件转发到 JPanel 中的 swing 组件。 我希望 Canvas3D 上的所有键盘和鼠标事件都分派到 JPanel,然后如果它们没有被任何 swing 组件捕获(例如,鼠标不在其中任何一个组件上),则回退到 Canvas3D。

我尝试调用 Container.dispatchEvent(AWTEvent),但它没有成功将事件分派到正确的组件,即使鼠标光标位于容器中的按钮上方也是如此。

有谁知道如何做到这一点?这应该是可能的。

I am trying to make it possible to display and interact with Java Swing components on top of a Java3D canvas. I am displaying the components by painting a transparent JPanel to a buffered image, and then painting that buffer over the canvas using J3DGraphics2D.

What I can't figure out is how to forward mouse events to the swing components in the JPanel.
I want all keyboard and mouse events on the Canvas3D to be dispatched to the JPanel, and then fall back through to the Canvas3D if they aren't captured by any swing components (e.g. the mouse isn't over any of them).

I tried calling Container.dispatchEvent(AWTEvent), but it doesn't successfully dispatch the events to the proper components, even when for example the mouse cursor is right over a button in the Container.

Does anyone know a way to do this? It should be possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷情 2024-11-21 10:46:06

终于,我想通了!它已经完成了——使用 JCanvas3D 和 JLayeredPane。这是在 postRender() 中渲染 Swing 组件的相反方法 - JCanvas3D 渲染到屏幕外缓冲区中,然后使用 AWT 绘制到屏幕上,创建一个轻量级画布,可以与 JLayeredPane 中的组件正确交互,即使它们是透明的。

需要注意的一件事 - JCanvas3D 将所有输入重定向到屏幕外的 Canvas3D,但一开始我的 Orbiter 并没有像重量级 Canvas3D 那样工作。您所要做的就是添加鼠标和JCanvas3D 的关键侦听器,因为如果没有为这些事件注册侦听器,AWT 甚至不会传递这些事件。

At long last, I figured it out! It's already been done -- use JCanvas3D and a JLayeredPane. This is the opposite approach to rendering the Swing components in postRender()-- JCanvas3D renders into an offscreen buffer, and then paints to the screen with AWT, creating a lightweight canvas that interacts properly with components in the JLayeredPane, even if they are transparent.

One thing to watch out for -- JCanvas3D redirects all input to the offscreen Canvas3D, but at first my Orbiter didn't work like it had with a heavyweight Canvas3D. All you have to do is add mouse & key listeners to the JCanvas3D, because AWT won't even deliver those events if there are no listeners registered for them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文