Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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
Clojure 在leiningen main返回后保持应用程序运行_Clojure_Javafx_Leiningen - Fatal编程技术网

Clojure 在leiningen main返回后保持应用程序运行

Clojure 在leiningen main返回后保持应用程序运行,clojure,javafx,leiningen,Clojure,Javafx,Leiningen,在clojure应用程序中,我有一个JavaFX窗口,应用程序在主函数返回后关闭。我希望应用程序保持打开状态,直到用户关闭窗口(或从菜单中选择“退出”或其他) 我该怎么做 这是core.clj文件,最后20行左右是相关的: (ns pdb-java-client.core (:gen-class) (:import (javafx.scene.control LabelBuilder))) ;; JavaFX UI (import '(javafx.scene SceneBuil

在clojure应用程序中,我有一个JavaFX窗口,应用程序在主函数返回后关闭。我希望应用程序保持打开状态,直到用户关闭窗口(或从菜单中选择“退出”或其他)

我该怎么做

这是core.clj文件,最后20行左右是相关的:

(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 [] ~@body)))

(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 [] ~@body)))

(defn event-handler*
  [f]
  (reify javafx.event.EventHandler
    (handle [this e] (f e))))

(defmacro event-handler [arg & body]
  `(event-handler* (fn ~arg ~@body)))

(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窗口显示为“Hello World!”,将“Hello,World!”打印到终端,然后程序退出。

将最后一行更改为

(run-now (.showAndWait @stage)))

另一种可能性是提供JavaFX的
应用程序
类的实现,这种实现可能更健壮、更符合标准

您可以在Leiningen项目中包含Java代码

package mypackage;

import javafx.application.Application;
import javafx.stage.Stage;

public class MyApplication extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        // Do nothing. This is just to get JavaFX started properly.
    }
    public static void initJavaFXApplication() {
        Application.launch();
    }
}
然后从Clojure打来电话:

(MyApplication/initJavaFXApplication)
通过向project.clj文件中添加以下行,Leiningen支持Java和Clojure源代码:

:source-paths ["src/clojure"]
:java-source-paths ["src/java"]

并相应地更新目录结构。

我发现Mac OS/X Yosemite和JavaFX支持中存在漏洞。事实上,它有时会使我的整个电脑崩溃。我向苹果公司提交了一份支持请求,他们知道这个问题。这个问题可能与这个bug有关。虽然这个答案可行,但它似乎不符合JavaFX标准,我怀疑它可能会在其他地方为复杂的应用程序带来问题。