在帧上启动键绑定

时间:2011-12-07 21:46:07

标签: java swing clojure

我有以下代码,旨在将键绑定的地图添加到JFrame。不幸的是,虽然它编译并且在运行程序时我没有得到任何错误,但绑定不起作用。

我错过了什么?

(defn create-action                                                                                                                                                                                                                           
  "Returns an Action that, when called, executes the given fn."                                                                                                                                                                               
  [f]                                                                                                                                                                                                                                         
  (proxy [AbstractAction] []                                                                                                                                                                                                                  
    (actionPerformed [e] (f))))                                                                                                                                                                                                               

(defn init-jframe-key-bindings!                                                                                                                                                                                                               
  "Adds the keybindings to the frame.                                                                                                                                                                                                         

   keymap take the form of:                                                                                                                                                                                                                   
   {\"KEYSTROKE\" [:key-name fn]                                                                                                                                                                                                              
    ...}"                                                                                                                                                                                                                                     
  [frame keymap]                                                                                                                                                                                                                              
  (let [actionmap (.getActionMap (.getRootPane frame))                                                                                                                                                                                        
        inputmap (.getInputMap (.getRootPane frame) JComponent/WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)]                                                                                                                                           
    (doseq [[keystroke [keyword action]] keymap]                                                                                                                                                                                                
      (.put actionmap (name keyword) (create-action action))                                                                                                                                                                              
      (.put inputmap (KeyStroke/getKeyStroke keystroke) (name keyword)))))

绑定的添加如下:

  (doto frame 
    (.setFocusable true)
    (init-jframe-key-bindings!                                                                                                                                                                                                              
      {"RIGHT" [:next-view to-next-view]                                                                                                                                                                                                     
       "LEFT" [:prev-view to-previous-view]                                                                                                                                                                                                  
       "T" [:thresh-test conduct-thresh-test]                                                                                                                                                                                                
       "A" [:add-marks #(dosync (ref-set ref-mark-mode :a))]                                                                                                                                                                                 
       "D" [:del-marks #(dosync (ref-set ref-mark-mode :d))]})) 

编辑解决方案是使用JComponent/WHEN_IN_FOCUSED_WINDOW代替JComponent/WHEN_ANCESTOR_OF_FOCUSED_COMPONENT。我不确定为什么会出现这种情况,因为焦点应该满足成为焦点组件的祖先的要求(但也许不是,我的无数组件的东西)并且仍然希望听到答案,但是后人的解决方案。

1 个答案:

答案 0 :(得分:2)

for很懒。正在调用您的init-jframe-key-bindings!函数,它返回一个惰性序列,当实现时,它实际上会添加键绑定。但你永远不会实现它;你丢掉它的结果。如果您想要副作用,请使用for而不是doseq