禁用空格键触发JButton的单击 [英] Disabling space bar triggering click for JButton

查看:91
本文介绍了禁用空格键触发JButton的单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JButton认为按下空格键与单击JButton相同(假设JButton具有焦点,我在这里假设).有没有办法关闭这种行为,使他们忽略按空格键?

JButtons consider pressing the space bar to be the same as clicking on the JButton (assuming the JButton has the focus, which I am assuming here). Is there a way to turn off this behavior so they ignore pressing the space bar?

更笼统地说,是否有一种技术可以通过AbstractButtons来做到这一点?

Also, more generally, is there a technique for doing this with AbstractButtons?

推荐答案

您可以通过执行以下操作将其禁用

You can disable it by doing

jbutton.getInputMap().put(KeyStroke.getKeyStroke("SPACE"), "none");

这似乎也适用于其他AbstractButton,例如JRadioButton.

This seems to work for other AbstractButtons as well, such as JRadioButtons.

@camickr 指出在下面的中,您可以一次性解决所有JButton的问题:

As @camickr points out below you can solve it for all JButtons in one go as follows:

InputMap im = (InputMap)UIManager.get("Button.focusInputMap");
im.put(KeyStroke.getKeyStroke("pressed SPACE"), "none");
im.put(KeyStroke.getKeyStroke("released SPACE"), "none");

这篇关于禁用空格键触发JButton的单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆