简体   繁体   English

在一段时间内停止接收Java中的所有ActionEvent / Stop侦听?

[英]Stop receiving all ActionEvents/Stop Listening in Java for a period of time?

I have a simple program that utilizes Java Swing Timer to display an image for 400 miliseconds, in this period of time I just want to stop all ActionListeners or stop taking ActionEvents. 我有一个简单的程序,利用Java Swing Timer在400毫秒内显示图像,在这段时间内,我只想停止所有ActionListener或停止采取ActionEvents。 I've got 40+ buttons and want a simple way to do this. 我有40多个按钮,想要一个简单的方法来做到这一点。

Is there anyway to do that in Java? 反正用Java可以做到吗?

Can you determine that you are in this "image displayed" state? 您可以确定自己处于此“显示的图像”状态吗? The image goes up and you set the state to "image displayed" or whatever. 图像上升,您将状态设置为“显示的图像”或其他。 Go through your widgets and decide which ones are supposed to be dead while the image is up. 浏览您的小部件,并确定在图像启动时应该死掉的小部件。 Turn them into Observers of this state value. 将它们变成该状态值的观察者。 When the state changes, they either enable or disable, as appropriate. 状态更改时,它们会根据需要启用或禁用。 The image code doesn't do anything directly to any widget. 图像代码不直接对任何窗口小部件执行任何操作。 It just declares that the state is now "image displayed". 它只是声明状态现在为“图像显示”。 It's up to the Observers to decide what to do, if anything, with that information. 由观察员决定如何处理该信息(如果有)。

Or use the GlassPane. 或使用GlassPane。 That works too. 那也行。 Of course, the GlassPane shuts down everything . 当然,GlassPane会关闭所有内容 If you need to be more selective, you need a more fine-tuned approach. 如果您需要更具选择性,则需要一种更精细的方法。

您可以使用临时GlassPane实例通过向其注册空侦听器来使用所有事件。

Use an undecorated modal JDialog to display the image. 使用未经修饰的模式JDialog来显示图像。 Before you make the dialog visible you would start a Timer. 在显示对话框之前,您将启动一个计时器。 When the Timer fires in 400 ms you close the dialog. 当计时器在400毫秒后触发时,您将关闭对话框。

I've had similar issues and typically found that its a design issue that got me in that situation. 我遇到过类似的问题,通常会发现它的设计问题使我陷入了这种情况。 Being the case, I still had to find away around it. 既然如此,我仍然必须四处寻找。 To fix the issue, I kept a list of the elements that I wanted to disable (stop listening) and iterated through them at the beginning and end of the timer. 为了解决此问题,我保留了要禁用(停止监听)的元素列表,并在计时器的开始和结束时对其进行了迭代。 For buttons it should be as simple as: 对于按钮,它应该很简单:

for(Component c : listOfToggledComponents){
     c.setEnabled(shouldItBeEnabled);
}

For buttons, this will grey out the button. 对于按钮,这将使按钮变灰。 Similar things happen to other swing components. 其他摆动组件也会发生类似的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM