[英]Starting off with JavaFX : Can not set javafx.scene.control.Label field application.SceneController.myLabel to javafx.scene.text.Text
[英]How to Use javafx.scene.layout.HBox and javafx.scene.control.Label from Clojure
我正在Clojure中開發javafx-project,但是不能使用這些javafx的類:javafx.scene.layout.Hbox,javafx.scene.control.Label和javafx.scene.control。[沒有Buttom的東西]。
我找不到解決方案。
謝謝!
這是代碼
temp / core.clj
```
(ns temp.core
(:import (javafx.application Application)
(javafx.scene.text Text Font FontWeight)
(javafx.scene.control Label TextField PasswordField Button)
(javafx.scene.layout GridPane HBox)
(javafx.scene.paint Color)
(javafx.geometry Pos Insets)
(javafx.event EventHandler)
(javafx.stage Stage))
(:gen-class))
(def x (Label. "Hello"))
;; get message
;; 2. Unhandled clojure.lang.Compiler$CompilerException
;; 1. Caused by java.lang.NoClassDefFoundError
;; javafx.scene.control.Labeled
(def H (HBox. 8))
;; get message
;; 1. Unhandled java.lang.IllegalArgumentException
;; No matching ctor found for class javafx.scene.layout.HBox
```
project.clj
(defproject temp "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.8.0"]]
:main temp.core
:aot [temp.core]
:target-path "target/%s"
:profiles {:uberjar {:aot :all}})
我可能會找到解決方案(我不知道為什么此解決方案是正確的歸因)
這是解決方案
(ns temp.core
(:import (javafx.application Application)
(javafx.scene.text Text Font FontWeight)
(javafx.scene.control Label TextField PasswordField Button)
(javafx.scene.layout GridPane HBox)
(javafx.scene.paint Color)
(javafx.geometry Pos Insets)
(javafx.event EventHandler)
(javafx.stage Stage))
(:gen-class))
(defn x [] (Label. "Hello"))
(defn H [] (HBox. 8))
;; return error in 'lein run' :
;; java.lang.IllegalArgumentException: No matching ctor found for class
;; javafx.scene.layout.HBox
請告訴我為什么該解決方案可以正常工作
在初始化JavaFX運行時系統並啟動它方面,您還需要做更多的工作。 這是一個非常小的項目。
這是萊寧根的項目文件。 它與您所擁有的沒有太大不同。
(defproject fxdemo "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.8.0"]]
:aot :all
:main fxdemo.core)
還有一個很小的程序,僅顯示JavaFX子系統的版本。
(ns fxdemo.core
(:gen-class
:extends javafx.application.Application)
(:import (javafx.application Application)
(javafx.scene.layout HBox)
(javafx.scene.control Label)
(javafx.scene Scene)
(javafx.stage Stage)))
(defn -start
"Build the application interface and start it all up. Called by the
JavaFX runtime."
[this stage]
(let [ver (System/getProperty "javafx.runtime.version")
lbl (Label. (str "JavaFX Version: " ver))
root (HBox.)
scene (Scene. root 250 150)]
(.add (.getChildren root) lbl)
(doto ^Stage stage
(.setScene scene)
(.setTitle "fxdemo")
(.show))))
(defn -main
"Start up the GUI part of the application."
[& args]
(Application/launch fxdemo.core args))
在我的系統上,執行lein run
會顯示以下窗口:
您不能真正獨立於框架使用框架的各個部分,例如HBox
和Label
。 如果查看(gen-class...
語句),則需要您的主程序來擴展JavaFX Application
類。要初始化JavaFX子系統,您需要使用Application/launch...
方法,通常在程序條目中函數,如圖所示(有很多例外和其他方法,但是它們太復雜了,在這里無法解釋。這是最簡單/最安全的方法。)到JavaFX子系統調用-start
方法時,事情大多已初始化並准備就緒。
另外,請注意在main
函數和start
函數名稱之前的連字符“-”,它們是Java和Clojure之間互操作的一部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.