删除默认的JButton输入地图 [英] Removing Default JButton input map
问题描述
我有一些麻烦去除Java Swing应用程序在我的组件的默认输入地图信息。这就是我要做的:
I am having some trouble removing the default input map information on my components in a java swing application. This is what I am trying to do:
//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");
}
}
}
但是,由于某些原因,这没有任何效果。 pressing空格键射击还是一个JButton点击等有谁看到不对的code座?由于事先。
But, for some reason, this has no effect. Pressing the spacebar still fires a JButton click, etc. Does anyone see something wrong with this code block? Thanks beforehand.
推荐答案
我无法重现您所描述的问题。我通常修改组件的的InputMap
,而 UIManager的
实例具有默认绑定。在下面的例子中,
I'm having trouble reproducing the problem you describe. I usually modify the component's InputMap
, but the UIManager
instance has the default bindings. In the example below,
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), NIL);
有效地阻止调用<大骨节病>空间骨节病>键按钮的的ActionListener
。取消注释行
button.getActionMap().put(NIL, nil);
,联营<大骨节病>空间骨节病>与有效空操作键,如图中的 doNothing 动作:// docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html#howto\">How~~V制作和删除键绑定的。
associates the Space key with an effectively empty action, as shown in the doNothing
action described in How to Make and Remove Key Bindings.
/**
* @see http://stackoverflow.com/q/12133795/230513
*/
public class NilBindingTest extends JPanel {
private static final String NIL = "none";
private Action nil = new AbstractAction(NIL) {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("\"" + e.getActionCommand() + "\"");
}
};
private JButton button = new JButton(nil);
//private InputMap im = button.getInputMap();
private InputMap im = (InputMap) UIManager.get("Button.focusInputMap");
public NilBindingTest() {
this.add(new JButton("foo"));
System.out.println(Arrays.toString(im.keys()));
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), NIL);
//button.getActionMap().put(NIL, nil);
this.add(button);
}
private void display() {
JFrame f = new JFrame("NilBindingTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new NilBindingTest().display();
}
});
}
}
这篇关于删除默认的JButton输入地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!