Keyboard Input with swing Java

ε祈祈猫儿з 提交于 2019-12-01 21:38:12

I think here is the problem:

button[0].getInputMap().put(...);

From JComponent.getInputMap() javadoc:

Returns the InputMap that is used when the component has focus. This is convenience method for getInputMap(WHEN_FOCUSED).

So the button must have the focus to properly work. Since you're doing a calculator I'd suggest you use this instead:

button[0].getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(...);

JComponent.getInputMap(int condition):

Returns the InputMap that is used during condition.

Parameters:

condition - one of WHEN_IN_FOCUSED_WINDOW, WHEN_FOCUSED, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!