如何在窗口上暂时防止Mouse Listener? [英] How to prevent Mouse Listener temporally on window?

查看:93
本文介绍了如何在窗口上暂时防止Mouse Listener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java编写游戏,这是我尝试制作漂亮"游戏的第一个游戏.该游戏称为Bantumi-这是一款棋盘游戏,现在我正在为动作编写动画.问题在于,当运行动作动画时,开发板仍会获得鼠标事件,并且如果用户选择了新动作,则正在运行的动作将被丢弃.

I'm writing a game in Java and this is the first one that I'm trying to make "pretty". The game is called Bantumi - it's a board game and right now I'm programming the animations for the movements. The problem is that, when the movement animation is running, the board still gets the Mouse Event and if the user selects a new movement, the running one will be discarded.

对于董事会,我使用的是扩展JLayeredPane的类.这是我的图层方式:

For the board I'm using a class extending JLayeredPane. This how I have my layers:

第0层:带有种子的Holes,因此用户为移动选择一个,每个Holes是带有MouseListener的JPanel.

Layer 0: The Holes with the seeds, so the users selects one for the movement, each Hole being a JPanel with a MouseListener.

第1层:标记当前选中的孔的突出显示

Layer 1: The Highlight that marks the currently selected Hole

第2层:运动的动画.

第10层:我编写的一个自定义通知系统类,上面写着您的回合",重复回合",您赢了"之类的内容.

Layer 10: A custom notification system class that I wrote, it says things like "Your Turn", "Repeat Turn", "You win", etc.

我想在动画运行时防止任何可能的鼠标事件在任何一层出现,我该怎么做?我认为在顶级玩家中添加一个覆盖整个区域的面板就足够了,但是没有用.有什么解决方法吗?

I want to prevent every possible mouse event in any of this layers while the animation is running, how do I do that? I thought that adding a panel covering the whole area in a top player was enough but it didn't work. Any workaround?

推荐答案

我认为,在顶级玩家中添加一个覆盖整个区域的面板就足够了,但这没用.

I thought that adding a panel covering the whole area in a top player was enough but it didn't work.

如果这是一个Swing GUI,则您已经拥有一个覆盖顶层窗口的glassPanel JPanel,但要做此工作的唯一方法是必须添加一个MouseListener(或MouseListener和MouseMotionListener),并且必须使其可见.

If this is a Swing GUI, you've already got a JPanel that covers the top level window, the glassPane, but the only way to make this work is you have to add a MouseListener (or both MouseListener and MouseMotionListener), and you have to make it visible.

您可以通过在顶层窗口或其根窗格中调用getGlassPane()来获取顶层窗口的玻璃窗格,向其添加MouseListener和MouseMotionListener,然后每当您要使GUI对鼠标事件不响应时,请设置通过调用setVisble(true)在玻璃窗格上可见.您可以通过调用setVisble(false)进行相反的操作来关闭此效果.

You can get the top window's glasspane by calling getGlassPane() on either a top level window or its root pane, add a MouseListener and MouseMotionListener to it, and then whenever you want to make the GUI unresponsive to mouse events, set the glass pane to visible by calling setVisble(true) on it. You can toggle this effect off by doing the converse, by calling setVisble(false).

这篇关于如何在窗口上暂时防止Mouse Listener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆