简体   繁体   English

Cljfx:渲染器的工作原理

[英]Cljfx: how renderer works

I'm digging in Cljfx right now.我现在正在研究 Cljfx。 But I faced a problem: can't understand how renderer exactly works.但我遇到了一个问题:无法理解渲染器究竟是如何工作的。 I want button's text will be changed after user pressed it.我希望按钮的文本在用户按下后会发生变化。 The initial state is "Button" text and it should be changed to "Pressed".最初的 state 是“Button”文本,应将其更改为“Pressed”。 But nothing happens like that.但是没有那样的事情发生。 What I'm doing wrong?我做错了什么?

(ns examp.core
  (:gen-class)
  (:require [cljfx.api :as fx])
  (:import [javafx.application Platform]))

(def *button-text (atom {:text "Button"}))

(def renderer
  (fx/create-renderer))

(defn label-text[& args]
  {:fx/type :label
   :text "Press the button"})

(defn root [& args]
  {:fx/type :stage
   :showing true
   :title "Cljfx"
   :width 300
   :height 300
   :scene {:fx/type :scene
           :root {:fx/type :v-box
                  :padding {:left 90 :top 19}
                  :spacing 10
                  :children [{:fx/type label-text}
                             {:fx/type :button
                              :min-width 50
                              :min-height 30
                              :text (:text @*button-text)
                              :on-action (fn [_]
                                           (if (= (:text @*button-text) "Button")
                                             (do
                                               (swap! *button-text assoc :text "Pressed")
                                               (println @*button-text)
                                               (renderer {:fx/type root}))
                                             (do
                                               (swap! *button-text assoc :text "Button")
                                               (println @*button-text)
                                               (renderer {:fx/type root}))))}]}}})


(defn -main [& args]
  (Platform/setImplicitExit true)
  (renderer {:fx/type root}))


If you don't find a Cljfx-specific solution, you can always use directly JavaFX functions.如果您没有找到特定于 Cljfx 的解决方案,您始终可以直接使用 JavaFX 函数。 I used .setText :我用过.setText

{:fx/type :button
   :min-width 50
   :min-height 30
   :text "Click me!"
   :on-action (fn [event]
                (.setText (.getSource event) "Clicked!"))}

Or, if you want to alternate between two texts:或者,如果您想在两个文本之间交替:

{:fx/type    :button
   :min-width  50
   :min-height 30
   :text       "Click me!"
   :on-action  (fn [event]
                 (let [source (.getSource event)]
                   (if (= (.getText source) "Click me!")
                     (.setText source "Clicked!")
                     (.setText source "Click me!"))))}

So I inspected examples of Cljfx code more attentively and found solution.所以我更仔细地检查了Cljfx代码的示例并找到了解决方案。 This is how it works with renderer.这就是它与渲染器一起工作的方式。

(ns examp.core
  (:gen-class)
  (:require [cljfx.api :as fx])
  (:import [javafx.application Platform]))

(def text {:text "click me"})

(def renderer (fx/create-renderer))


(defn root [{:keys [text]}]
  {:fx/type :stage
   :showing true
   :title "Window"
   :scene {:fx/type :scene
           :root {:fx/type :v-box
                  :children [{:fx/type :label
                              :text  "press the button"}
                             {:fx/type :button
                              :min-width 100
                              :min-height 50
                              :text text
                              :on-action (fn [_]
                                           (if (= text "click me") 
                                           (renderer
                                             {:fx/type root
                                              :text "clicked"})
                                           (renderer 
                                             {:fx/type root
                                              :text "click me"})))}]}}})

(defn -main [& args]
  (Platform/setImplicitExit true)
  (renderer {:fx/type root
             :text (:text text)}))  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM