Beginner to Swing

后端 未结 6 1762
难免孤独
难免孤独 2021-01-12 08:33

I\'d like to create a simple Swing app. I\'ve got very, very, very little experience with Swing, however. I want to create a one window app that refreshes every 5 minutes

6条回答
  •  庸人自扰
    2021-01-12 08:51

    You're right. Swing is the way to go, but connecting all the pieces can be a bit tough if you're learning Clojure and Swing. There are a few short examples floating around showing how to create simple Swing GUIs in Clojure. Here's another short example that combines a simple GUI with a Timer object.

    (ns net.dneclark.JFrameAndTimerDemo
      (:import (javax.swing JLabel JButton JPanel JFrame Timer))
      (:gen-class))
    
    (defn timer-action [label counter]
     (proxy [java.awt.event.ActionListener] []
       (actionPerformed
         [e]
          (.setText label (str "Counter: " (swap! counter inc))))))
    
    (defn timer-fn []
      (let [counter (atom 0)
        label (JLabel. "Counter: 0")
        timer (Timer. 1000 (timer-action label counter))
        panel (doto (JPanel.)
                (.add label))]
      (.start timer)
      (doto (JFrame. "Timer App")
        (.setContentPane panel)
        (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
        (.setLocation 300 300)
        (.setSize 200 200)
        (.setVisible true))))
    
    (defn -main []
      (timer-fn))
    

    When run, this will create a small window with a label that is updated every second. From your description, you would change the frequency of the timer from 1,000ms to 300,000ms to trigger an action every 5 minutes. To do something other than updating a label, you would change the contents of the timer-action function.

    I think this is thread safe, but haven't checked for sure. There are cautions and tutorials about thread safety when updating Swing components too. You'll probably want to check those too.

    I hope this is informative enough to give yo a few clues as to where to look for further information.

    EDIT: I wanted to point out one more interesting thing here. Note that the 'timer-action' function is changing the value of one of its arguments. The 'counter' argument is an atom defined in 'timer-fn', yet the action listener is able to change it. This is something you cannot usually do in Java. Maybe someone smarter than me can comment on whether this constitutes a "closure". In my previous experience with languages like Pascal, I would say the argument passing is "call-by-reference" as opposed to Java's strict "call-by-value" argument passing. Is this something different?

    EDIT 2: After checking my facts with another question, this is, in fact, an example of a closure in Clojure.

提交回复
热议问题