![](/img/trans.png)
[英]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.