Beginner to Swing

后端 未结 6 1694
难免孤独
难免孤独 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:46

    Try this link for Swing. As Zach said you will need to use JFrame and TimerTask should be used for your requirements.

    You can also try other alternative frameworks for Swing.

    0 讨论(0)
  • 2021-01-12 08:50

    Well, for the every five minutes bit, java.util.TimerTask should be of help. For general Swing information, this link to the Java Tutorials ought to help.

    To have a Window, specifically, JFrame is probably your best bet.

    To display single or multiline text, you ought to look into JLabel or JTextArea, respectively.

    To display images, ImageIcon ought to do the trick.

    For other needs, the Java Tutorial ought to be a big help.

    As trashgod suggested, javax.swing.Timer has some advantages when it comes to GUIs over java.util.TimerTask. This article on using timers in Swing applications should help you decide which to use.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-12 08:57

    Clojure's software transactional memory allows you to set watches on variables; your callback is executed whenever the variable is changed (by anything). This lends itself very well to GUI programming. Your GUI can auto-update whenever anything touches the variable.

    Here is a short but non-trivial example of how to do this, with explanation of what is going on: http://www.paullegato.com/blog/swing-clojure-gui-black-scholes/

    0 讨论(0)
  • 2021-01-12 09:04

    In a Swing context, javax.swing.Timer has some advantages; there's an example here. Depending on what you want to display, JEditorPane may be appropriate.

    0 讨论(0)
  • 2021-01-12 09:04

    On top of the resources mentioned by @Zach L (particularly regarding the timers), I would take a good look at Seesaw, especially since you're writing this in Clojure.

    In particular, I note the seesaw.timer for firing the refresh events. Using a JTextPane (read-only) or a JEditorPane (editable) would work well for displaying richly formatted results (like HTML).

    0 讨论(0)
提交回复
热议问题