我在删除 java swing 应用程序中组件上的默认输入映射信息时遇到了一些麻烦。这就是我想要做的:
//List of keys to remove
public static final int[] OVERWRITTEN_KEYS =
{
VK_SPACE
};
//Get default input maps
InputMap[] im = {
(InputMap)UIManager.get("Button.focusInputMap"),
(InputMap)UIManager.get("ToggleButton.focusInputMap"),
(InputMap)UIManager.get("Slider.focusInputMap"),
(InputMap)UIManager.get("RadioButton.focusInputMap"),
(InputMap)UIManager.get("TextArea.focusInputMap"),
(InputMap)UIManager.get("TextField.focusInputMap")
};
//Loop through input maps
for(int i = 0; i < im.length; i++)
{
//Loop through keys
for(int j = 0; j < OVERWRITTEN_KEYS.length; j++)
{
if(im[i] != null)
{
//Overwrite press and release of button
im[i].put(KeyStroke.getKeyStroke(OVERWRITTEN_KEYS[j],0,false), "none");
im[i].put(KeyStroke.getKeyStroke(OVERWRITTEN_KEYS[j],0,true), "none");
}
}
}
但是,由于某种原因,这没有效果。按空格键仍会触发 JButton 单击等。有人看到此代码块有问题吗?预先感谢。