簡體   English   中英

Cljfx:渲染器的工作原理

[英]Cljfx: how renderer works

我現在正在研究 Cljfx。 但我遇到了一個問題:無法理解渲染器究竟是如何工作的。 我希望按鈕的文本在用戶按下后會發生變化。 最初的 state 是“Button”文本,應將其更改為“Pressed”。 但是沒有那樣的事情發生。 我做錯了什么?

(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}))


如果您沒有找到特定於 Cljfx 的解決方案,您始終可以直接使用 JavaFX 函數。 我用過.setText

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

或者,如果您想在兩個文本之間交替:

{: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!"))))}

所以我更仔細地檢查了Cljfx代碼的示例並找到了解決方案。 這就是它與渲染器一起工作的方式。

(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