我有以下键绑定:
InputMap iMap = component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap aMap = component.getActionMap();
iMap.put(KeyStroke.getKeyStroke("shift LEFT"), "MOVE_LONG_LEFT");
iMap.put(KeyStroke.getKeyStroke("LEFT"), "MOVE_LEFT");
aMap.put("MOVE_LONG_LEFT", moveLeft); // I WANT moveLeft TO RECEIVE 10 AS PARAMETER
aMap.put("MOVE_LEFT", moveLeft); // I WANT moveLeft TO RECEIVE 1 AS PARAMETER
我想在actionmap中为moveleft添加一个参数,比如(伪代码):
aMap.put("MOVE_LONG_LEFT", moveLeft, 10);
aMap.put("MOVE_LEFT", moveLeft, 1);
...
Action moveLeft = new AbstractAction(int steps) {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("moved Left " + steps + " steps");
}
};
可以在keybindings中传递参数吗?
1条答案
按热度按时间zu0ti5jz1#
如何将参数传递给抽象操作
创建您的
Action
作为内部类,则可以将参数另存为类的示例变量:然后你使用这个类,比如:
见;使用键盘移动。这个
MotionWithKeyBindings
示例演示了这种方法。