简体   繁体   English

Java鼠标单击问题

[英]Java Mouse Clicking Issue

Okay, so I created a pong game and it's cool and all but every time I go to the game I have to click on the window to use any controls. 好的,所以我创建了一个pong游戏,它很酷,但是每次我进入该游戏时,我都必须单击该窗口以使用任何控件。 I was wondering if there is any way to be able to use the controls ( like wsad ) without having to click on the window. 我想知道是否可以通过任何方式使用控件(例如wsad)而不必单击窗口。 Just a question. 就一个问题。 Thanks if you answer and sorry that I can't vote you up because I don't got enough reputation. 多谢您回答,对不起,因为我没有足够的声誉,我无法投票赞成您。

KeyListener is well known for having focus related issues. KeyListener因与焦点相关的问题而闻名。 This basically boils down to the fact that KeyListener will only respond to key presses if the component it is attached to is focusable and has focus. 这基本上可以归结为一个事实,即KeyListener仅在其所连接的组件可聚焦且具有焦点时才响应按键。

Instead, you should try using the Key Bindings API which allows you to control the focus granuality, to the extent that so long as the window has focus, you can respond to key events (so long as some other component doesn't consume them first ;)) 相反,您应该尝试使用Key Bindings API ,该API允许您控制焦点的粒度,只要窗口具有焦点,就可以响应关键事件(只要其他一些组件不首先使用它们) ;))

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

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