按下鼠标时更改 mouseListener
我正在用 Java 开发一款游戏,并面临以下挑战。
我有 2 个 JPanel
,需要以可视方式将形状从一个 JPanel
拖动到另一个。我已经使用 JFrame
中的 GlassPane
完成了这项工作。当我按下鼠标拖动形状时,GlassPane
会激活并将形状传输到 glassPane。因此,您需要将 mousePressed
状态从 JPanels mouseAdapter
传输到 glassPanes mouseAdapter
。我通过使用 Robot 类解决了这个问题,该类在 glassPane 激活后模拟另一个 mousePressed
事件。
现在问题来了,此解决方法仅适用于 Windows,不适用于 mac osx,在 osx 上,只要按下鼠标按钮,鼠标就会一直与 JPanels mouseAdapter
通信。那么有谁知道如何在按下鼠标按钮时以正确的方式将 mousePressed
状态从一个 mouseAdapter
转移到另一个? (释放按钮并再次按下不是一种选择,因为这会破坏拖动的目的。)
I'm working on a game in Java and have the following challenge.
I have 2 JPanels
and need to visually drag shapes from one JPanel
to another. I've got this working using the GlassPane
from the JFrame
. When I press the mouse to drag a shape, the GlassPane
activates and transfers the shape to the glassPane. Because of this you need to transfer the mousePressed
state from the JPanels mouseAdapter
to the glassPanes mouseAdapter
. I solved this by using the Robot Class which simulates another mousePressed
event after the glassPane has been acivated.
Now here comes the problem, this workaround only works on windows and not on mac osx, on osx the mouse keeps talking to the JPanels mouseAdapter
as long as the mousebutton is pressed. So does anyone know how to transfer the mousePressed
state from one mouseAdapter
to another while pressing the mousebutton, in a proper way? (Releasing the button and pressing it again is not an option as this would defeat the purpose of dragging.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不只将 MouseListener 添加到 glasspane,并在 mousePressed 方法中获取鼠标的位置(Point),然后通过在保存组件的容器上调用 getComponentAt(Point p) 来获取可拖动组件?然后,您可以将组件放入玻璃板中并将其拖到那里。例如,这是我使用 JLayeredPane (类似于使用玻璃板)完成此操作的一种方法: DragLabelOnLayeredPane
或者另一种想法:为什么不简单地将 MouseAdapter 添加到拖动的组件本身上并将其保留在组件上?只要您注意获取鼠标相对于屏幕的位置并相对于其容器移动组件,无论该组件是在 contentPane 还是 glasspane 中,您都应该没有问题。
编辑:或者采纳 MeBigFatGuy 的极好建议。
编辑 2:深夜半醉尝试一个不简单的概念验证程序,该程序仅通过向玻璃窗格添加 MouseListener 来移动形状。
Why not add the MouseListener just to the glasspane, and in the mousePressed method, get the mouse's location (Point) and then get the draggable component by calling getComponentAt(Point p) on the Container that holds your Component? You can then place the component into the glasspane and drag it there. For instance here is one way I've done this using a JLayeredPane (which is similar to using a glasspane): DragLabelOnLayeredPane
Or another thought: why not simply add the MouseAdapter on the dragged component itself and leave it on the component? As long as you take care to get mouse position relative to the screen and move the component relative to its container, you should have no problem whether the component is in the contentPane or the glasspane.
edit: or go with MeBigFatGuy's excellent suggestion.
edit 2: A semi-drunk late at night attempt at a not-brief-as-I'd-like proof of concept program, a program that moves shapes by adding a MouseListener to the glass pane only.