Java - 等待某种类型的按键继续
在 Java 中实现“按 x 继续”类型的最佳方法是什么?
具体来说,我有一个扩展 JFrame 的自定义类和一个扩展 JPanel 的自定义类。我有一个 Main.java (其中有我的 JFrame 类的实例),并且有一点我不想继续,直到用户按下空格键:
Main.java:
...code...
frame.waitForSpace();
...more code which gets executed only after space is pressed...
那么,在我的框架类中,如何我应该实现这个:
MyFrame.java:
/* This method only finishes when the space bar is pressed */
public void waitForSpace() {
}
顺便说一下,我的 JFrame 上有 KeyListener,它在按下空格按钮时起作用。但我不太确定如何将其纳入我想做的事情中。
示例代码会很棒!
What is the best way to implement a "press x to continue" type of thing in Java?
Specifically, I have a custom class which extends JFrame and a custom class which extends JPanel. I have a Main.java (which has an instance of my JFrame class), and there is a point where I do not want to continue until the user has pressed the space bar:
Main.java:
...code...
frame.waitForSpace();
...more code which gets executed only after space is pressed...
So, in my frame class, how should I implement this:
MyFrame.java:
/* This method only finishes when the space bar is pressed */
public void waitForSpace() {
}
By the way, I have the KeyListener on my JFrame which works when the space button is pressed. But I'm not quite sure how to incorporate that into what I'm trying to do.
Example code would be awesome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当用户按下空格键时,将您想要执行的任何操作放入该处理程序中。很可能您不想每次按下空格键时都执行(无论什么)操作,但只是在某些时候执行;因此,事件处理程序应该取决于某个变量的值,并且在您希望用户按空格键之前运行的代码应该将该变量设置为表示“执行此操作”的值。处理程序应在运行后将变量设置回默认值。
Put whatever you want to do when the user presses space into that handler. Most likely you don't want to do (whatever) every time the space bar is pressed, but only some of the time; therefore the event handler should be contingent on the value of some variable, and the code that runs before you want the user to press space should set that variable to the value that means "do it." The handler should set the variable back to the the default value after it runs.
请记住,Swing GUI 是事件驱动的。所以不要等待任何事情。相反,为您的类提供某种状态字段(可能是布尔值),在按键时更改状态变量(可能使用键绑定),然后不允许某些行为,除非状态已更改(通过 if 语句)。
Remember that Swing GUI's are event driven. So don't wait for anything. Instead give your class a state field of some sort, perhaps a boolean, change the state variable on key press, perhaps using key bindings, and then don't allow certain behaviors unless the state has been changed (via if statements).
当调用await()时使用CountDownLatch,当前main()线程将等待,直到从另一个线程调用countDown()方法。
KeyEventDispatcher 允许您全局添加击键侦听器。因此,每当按下一个键时,dispatchKeyEvent() 都会从 EDT(事件调度线程)中调用,您可以在其中检查是否按下了“空格”并释放 main() 正在等待的倒计时锁存器。
但是,如果调用 waitForSpace() 的线程是 EDT,那么您将强制 EDT 等待自身,从而导致死锁。据我所知,等待 EDT 后仍能收到关键事件是不可能的。
只要 JFrame 具有焦点,这就会起作用:
Using a CountDownLatch when await() is invoked, the current main() thread will wait until the countDown() method is called from another thread.
The KeyEventDispatcher lets you add a keystroke listener globally. So whenever a key is pressed dispatchKeyEvent() gets called from the EDT (Event Dispatching Thread) and there you can check for 'space' being pressed and release the countdown latch that main() is waiting on.
However, if the thread that calls waitForSpace() is the EDT, then you will be forcing the EDT to wait for itself, causing deadlock. It is impossible to wait on the EDT and still receive key events as far as I know.
This will work so long as a JFrame has focus:
不要使用 KeyLIstener。
使用按键绑定。您已经获得了键绑定教程的链接。
Don't use a KeyLIstener.
Use Key Bindings. You've already been given a link to the key bindings tutorial.