简体   繁体   中英

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. 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

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.

How do people know to use the Enter key?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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