Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将参数传递给键绑定中的AbstractAction?_Java_Swing_Key Bindings_Action Mapping - Fatal编程技术网

Java 如何将参数传递给键绑定中的AbstractAction?

Java 如何将参数传递给键绑定中的AbstractAction?,java,swing,key-bindings,action-mapping,Java,Swing,Key Bindings,Action Mapping,我有以下键绑定: InputMap iMap=component.getInputMap(JComponent.WHEN在聚焦窗口中); ActionMap aMap=component.getActionMap(); iMap.put(KeyStroke.getKeyStroke(“左移”),“左移”; iMap.put(KeyStroke.getKeyStroke(“左”),“左移”); aMap.put(“MOVE_LONG_LEFT”,moveLeft);//我希望moveLeft接收1

我有以下键绑定:

InputMap iMap=component.getInputMap(JComponent.WHEN在聚焦窗口中);
ActionMap aMap=component.getActionMap();
iMap.put(KeyStroke.getKeyStroke(“左移”),“左移”;
iMap.put(KeyStroke.getKeyStroke(“左”),“左移”);
aMap.put(“MOVE_LONG_LEFT”,moveLeft);//我希望moveLeft接收10作为参数
aMap.put(“向左移动”,向左移动);//我希望moveLeft接收1作为参数
我想在ActionMap中向moveLeft添加一个参数,类似(伪代码):

aMap.put(“向左移动”,向左移动,10);
aMap.put(“向左移动”,向左移动,1);
...
Action moveLeft=新的抽象操作(int步骤){
@凌驾
已执行的公共无效操作(操作事件e){
System.out.println(“向左移动”+步数+“步数”);
}
};
可以在键绑定中传递参数吗

如何将参数传递给抽象操作

操作创建为内部类,然后可以将参数另存为类的实例变量:

class MoveAction extends AbstractAction 
{
    private int steps;

    public MoveAction(int steps)
    {
        this.steps = steps;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("moved Left " + steps + " steps");
    }
};
然后使用如下类:

aMap.put("MOVE_LONG_LEFT", new MoveAction(10)); 
aMap.put("MOVE_LEFT", new MoveAction(1));  

见。
MotionWithKeyBindings
示例演示了这种方法。

可以在KeyBindings中传递参数吗?不,不可能传入参数。如果您解释了您认为需要这样做的原因,可能会有人提供另一种方法来实现相同的结果。我让F1-F10执行相同的操作(即调用相同的函数),但参数会根据按下的函数键从1更改为10。如果keybinding不允许参数,我想唯一的选择是处理
ActionEvent e
,据我所知,这里会有按键。