J.F78
J.F78

Reputation: 1

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

Upvotes: 0

Views: 286

Answers (1)

camickr
camickr

Reputation: 324207

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.

Upvotes: 1

Related Questions