在我的clojure應用程序中,我有一個JavaFX窗口,應用程序在主函數返回後關閉。我希望應用程序保持打開狀態,直到用戶關閉窗口(或從菜單中選擇「退出」或其他)。在leiningen主要回報後繼續運行應用程序
我該怎麼做?
這裏的core.clj文件,最後20行左右是相關的人:「Hello World」的
(ns pdb-java-client.core
(:gen-class)
(:import (javafx.scene.control LabelBuilder)))
;; JavaFX UI
(import '(javafx.scene SceneBuilder)
'(javafx.scene.canvas CanvasBuilder Canvas GraphicsContext)
'(javafx.scene.control ButtonBuilder)
'(javafx.scene.layout VBoxBuilder)
'(javafx.scene.image WritableImage PixelWriter)
'(javafx.scene.transform Scale Translate)
'(javafx.stage StageBuilder))
; instead of extending javafx.application.Application
(defonce force-toolkit-init (javafx.embed.swing.JFXPanel.))
; some helper functions and macros to make JavaFX easier to type
(defn run-later*
[f]
(javafx.application.Platform/runLater f))
(defmacro run-later
[& body]
`(run-later* (fn [] [email protected])))
(defn run-now*
[f]
(let [result (promise)]
(run-later
(deliver result (try (f) (catch Throwable e e))))
@result))
(defmacro run-now
[& body]
`(run-now* (fn [] [email protected])))
(defn event-handler*
[f]
(reify javafx.event.EventHandler
(handle [this e] (f e))))
(defmacro event-handler [arg & body]
`(event-handler* (fn ~arg [email protected])))
(def stage (atom nil))
; build a scene
(run-now (reset! stage (.. StageBuilder create
(title "Fracjure")
(scene (.. SceneBuilder create
;(height 480) (width 640)
(root (.. VBoxBuilder create
;(minHeight 480) (minWidth 640)
(children [(.. LabelBuilder create
(text "Hello World!")
build)])
build))
build))
build)))
;(run-now (.show @stage))
(defn -main
"I don't do a whole lot ... yet."
[& args]
(println "Hello, World!")
(run-now (.show @stage)))
注意的是,上面的代碼工作,則顯示JavaFX的窗口說,「你好,世界!「打印到終端,然後程序退出。
我發現在Mac OS/X優勝美地和JavaFX支持中存在錯誤。事實上,它有時會使我的整個計算機崩潰。我向蘋果提出了一項支持請求,他們意識到這個問題。這個問題可能與該錯誤有關。 – Jason 2015-01-22 15:14:27