我正在尝试使用 UIManager 获取并清除一些默认键绑定,以便空格键不会激活我的 JButtons,如所解释的here https://stackoverflow.com/questions/12133795/removing-default-jbutton-input-map。问题是,可能是由于我的合成器外观和感觉,(InputMap)UIManager.get("Button.focusInputMap");
返回一个null
。有谁知道一种以其他方式轻松清除组件输入映射的方法,或者为什么 UIManager 在这种情况下返回 null?任何提示表示赞赏,预先感谢。
首先:我喜欢装饰 StyleFactory 的想法,正如 @David 在另一个答案中所建议的那样:-) - 所以如果可行的话,建议使用这个方向。
无论如何,忍不住尝试一下:看起来 Nimbus(可能还有其他基于 Synth 的 LAF)需要覆盖这些默认值极其在生命周期的早期:只有在之前完成的情况下,他们才会接受any组件实际上已创建
// setting LAF
InteractiveTestCase.setLAF("Nimbus");
// tweak inputMap, immediately after setting the ui is fine
// uncomment the following line and it doesn't work
// new JPanel();
InputMap inputMap = (InputMap) UIManager.get("Button.focusInputMap");
inputMap.put(KeyStroke.getKeyStroke("SPACE"), "do-nothing");
如果 UIManager 此时没有返回 inputMap,我会将其视为自定义 LAF 的不当行为,并会尝试进一步挖掘发生这种情况的原因。您可以尝试的另一件事是设置一个全新的 inputMap (这将具有在 LAF 切换中幸存的优点,因为它不是 UIResource,例如:
// setting LAF
InteractiveTestCase.setLAF("Nimbus");
// tweak inputMap, immediately after setting the ui is fine
InputMap inputMap = (InputMap) UIManager.get("Button.focusInputMap");
InputMap custom = new InputMap();
if (inputMap != null) {
// copy all bindings to custom
...
} else {
// add the binding we know of (as implementation detail)
custom.put(KeyStroke.getKeyStroke("released SPACE"), "released");
}
// overwrite the binding you want to change
custom.put(KeyStroke.getKeyStroke("SPACE"), "do-nothing");
// set the custom map
UIManager.put("Button.focusInputMap", custom);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)