问题描述
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 AbstractButton
s as well, such as JRadioButton
s.
@camickr 指出在下面的中,您可以一次性解决所有JButton
的问题:
As @camickr points out below you can solve it for all JButton
s 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的单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!