我有一个扩展 jframe 的类,其键绑定映射到一个不可见的按钮。当我按下键盘上的键时,它会执行按钮的命令。但是,如果用户按住按钮,它将每秒执行该命令一百次,从而使程序崩溃。我该如何防止这种情况,以便命令只执行......假设......按下键时每秒一次?
代码片段如下:
JButton fire = new JButton("");
KeyStroke spaceBar = KeyStroke.getKeyStroke("SPACE");
FireCommand fc = new FireCommand();
this.fire.setAction(fc);
imap.put(SpaceBar,"space");
amap.put("space",fc);