Skip to content

Instantly share code, notes, and snippets.

@juergenhoetzel
Created June 6, 2010 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juergenhoetzel/427655 to your computer and use it in GitHub Desktop.
Save juergenhoetzel/427655 to your computer and use it in GitHub Desktop.
(import (javax.swing Box BoxLayout JTextField JPanel
JSplitPane JLabel JButton JOptionPane)
(java.awt.event ActionListener)
(java.awt Component GridLayout FlowLayout))
(defn shelf [& components]
(let [shelf (JPanel.)]
(.setLayout shelf (FlowLayout.))
(doseq [c components] (.add shelf c))
shelf))
(defn stack [& components]
(let [stack (Box. BoxLayout/PAGE_AXIS)]
(doseq [c components]
(.setAlignmentX c Component/CENTER_ALIGNMENT)
(.add stack c))
stack))
(defn splitter [top bottom]
(doto (JSplitPane.)
(.setOrientation JSplitPane/VERTICAL_SPLIT)
(.setLeftComponent top)
(.setRightComponent bottom)))
(defn grid [x y f]
(let [g (doto (JPanel.)
(.setLayout (GridLayout. x y)))]
(dotimes [i x]
(dotimes [j y]
(.add g (f))))
g))
(defn button [text f]
(doto (JButton. text)
(.addActionListener
(proxy [ActionListener] []
(actionPerformed [_]
(f))))))
(defn txt [cols t]
(doto (JTextField.)
(.setColumns cols)
(.setText t)))
(defn label [txt] (JLabel. txt))
(defn alert
([msg] (alert nil msg))
([frame msg]
(javax.swing.JOptionPane/showMessageDialog frame msg)))
(defn example-gui []
(let [gui (joy.gui.DynaFrame. "test")
g1 (txt 10 "Charlemagne")
g2 (txt 10 "Pippin")
r (txt 3 "10")
d (txt 3 "5")]
(.display gui
(splitter
(stack
(shelf (label "Player 1") g1)
(shelf (label "Player 2") g2)
(shelf (label "Rounds ") r
(label "Delay ") d))
(stack (grid 21 11 #(label "-"))
(button "Go!" #(alert (str (.getText g1) " vs."
(.getText g2) " for "
(.getText r) " rounds, every "
(.getText d) " seconds."))))))))
@fogus
Copy link

fogus commented Jun 7, 2010

This is really very nice.

@fogus
Copy link

fogus commented Jun 8, 2010

Slight modifications to use doseq instead of (doall (for ...

(defn shelf [& components]
  (let [shelf (JPanel.)]
    (.setLayout shelf (FlowLayout.))
    (doseq [c components] (.add shelf c))
    shelf))

(defn stack [& components]
  (let [stack (Box. BoxLayout/PAGE_AXIS)]
    (doseq [c components] 
      (.setAlignmentX c Component/CENTER_ALIGNMENT)
      (.add stack c))
    stack))

@juergenhoetzel
Copy link
Author

Thanks fogus! doseq is much more idiomatic/appropriate in this case.

@fogus
Copy link

fogus commented Jun 8, 2010

The general rule of thumb is that anything starting with do is meant for side-effects. Hopefully, we say that in the book. ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment