简体   繁体   English

在JFrame上输入密钥

[英]Enter key on JFrame

So what I've been attempting to do is essentially have it set to when the enter key is pressed on my JFrame in java it detects it so I can do an action like start a game by switching to the next screen. 因此,我一直试图做的基本上是将其设置为在Java中的JFrame上按下回车键时它会检测到它,因此我可以执行操作,例如通过切换到下一个屏幕来启动游戏。 So I was wondering if anyone could help me figure out how to do this in particular. 所以我想知道是否有人可以帮助我弄清楚如何做到这一点。 Just being able to have something that constantly checks to see if the Enter Key has been pressed on the JFrame 能够不断检查JFrame上是否已按下Enter键的东西

have it set to when the enter key is pressed on my JFrame in java it detects it so I can do an action like start a game by switching to the next screen. 将其设置为在Java中的JFrame上按下回车键时,它将检测到它,因此我可以执行操作,例如通过切换到下一个屏幕来启动游戏。

How do people know to use the Enter key? 人们如何知道使用Enter键?

Typically you would have a button like "Start Game" for the user to click. 通常,您将有一个类似“开始游戏”的按钮供用户单击。

You can easily make this the default button by using: 您可以使用以下命令轻松将其设置为默认按钮:

frame.getRootPane().setDefaultButton( startGameButton );

Now the user will be able to either: 现在,用户将能够:

  1. click on the button 点击按钮
  2. use the Enter key to invoke the button. 使用Enter键调用该按钮。

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

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