使用 keyBindings (正如@trashgod 已经提到的)是要走的路。获得与通过空格/输入激活按钮完全相同的视觉行为(当它被聚焦时)
- 实现委托给为按下/释放按钮注册的默认操作的操作
- 需要绑定到按键的按下和释放来模拟
- 在 WHEN_ANCESTOR 类型的 inputMap 中安装到按钮父级的绑定
在代码中:
// the delegating action
public static class SimulateButtonAction extends AbstractAction {
AbstractButton button;
public SimulateButtonAction(AbstractButton model, String fire) {
super(fire);
this.button = model;
}
@Override
public void actionPerformed(ActionEvent e) {
Action delegate = button.getActionMap().get(getName());
delegate.actionPerformed(new ActionEvent(button,
ActionEvent.ACTION_PERFORMED, getName()));
}
public String getName() {
return (String) getValue(Action.NAME);
}
}
// example usage
JComponent content = new JPanel(new GridLayout(0, 5));
Action log = new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("triggered: " + ((AbstractButton) e.getSource()).getText());
}
};
String pressed = "pressed";
String released = "released";
ActionMap actionMap = content.getActionMap();
InputMap inputMap = content.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
String[] arrows = {"UP", "DOWN", "LEFT", "RIGHT"};
for (int i = 0; i < arrows.length; i++) {
JButton button = new JButton(log);
button.setAction(log);
button.setText(arrows[i]);
content.add(button);
// simulate pressed
String pressedKey = pressed + arrows[i];
inputMap.put(KeyStroke.getKeyStroke(arrows[i]), pressedKey);
actionMap.put(pressedKey, new SimulateButtonAction(button, pressed));
String releasedKey = released + arrows[i];
inputMap.put(KeyStroke.getKeyStroke(released + " " +arrows[i]), releasedKey);
actionMap.put(releasedKey, new SimulateButtonAction(button, released));
}