Being new to Clojure , I started off playing with the codes ,and this is the result, a 2cents calculator. It is not yet a done deal, however, my laptop got fried, so I would be redundant for few days, that's why I' ve decided to 'outsource' :).I invite your comments and any code that might make this toy to worth 2 cents in today's market.
Emeka (import '(javax.swing JFrame JLabel JTextField JButton) '(java.awt.event ActionListener) '(java.awt GridLayout) '(java.awt BorderLayout)) (def gool (ref "")) (def perm1 (ref 0)) (def perm2 (ref 0)) (def sign-to (ref 0)) (defn twocents [] ;; setting up buttons (let [frame (new JFrame "2Cents Calculator" ) temp-text (new JTextField ) clear-button (new JButton "C") one-button (new JButton "1") two-button (new JButton "2") three-button (new JButton "3") four-button (new JButton "4") five-button (new JButton "5") six-button (new JButton "6") seven-button (new JButton "7") eight-button (new JButton "8") nine-button (new JButton "9") zero-button (new JButton "0") plus-button (new JButton "+") minus-button (new JButton "-") calculate-button (new JButton "=") divide-button (new JButton "/")] ;; Adding event listeners to buttons (. plus-button (addActionListener (proxy [ActionListener] [] (actionPerformed [evt] (let [c (. Double (parseDouble (. temp-text (getText))))] (. temp-text (setText (str " ")) ) (dosync (ref-set perm1 c)))(dosync (ref-set gool (str )) (ref-set sign-to +)))))) (. minus-button (addActionListener (proxy [ActionListener] [] (actionPerformed [evt] (let [c (. Double (parseDouble (. temp-text (getText))))] (. temp-text (setText (str " ")) ) (dosync (ref-set perm1 c)))(dosync (ref-set gool (str )) (ref-set sign-to -)))))) (. divide-button (addActionListener (proxy [ActionListener] [] (actionPerformed [evt] (let [c (. Double (parseDouble (. temp-text (getText))))] (. temp-text (setText (str " ")) ) (dosync (ref-set perm1 c)))(dosync (ref-set gool (str )) (ref-set sign-to /)))))) (. calculate-button (addActionListener (proxy [ActionListener] [] (actionPerformed [evt] (let [c (. Double (parseDouble (. temp-text (getText))))] (. temp-text (setText (str (sign-to @perm1 c))))))))) (. clear-button (addActionListener (proxy [ActionListener] [] (actionPerformed [evt] (let [c nil] (. temp-text (setText (str " "))) (dosync (ref-set perm1 c)) (dosync (ref-set gool c)) ))))) (. one-button (addActionListener (proxy [ActionListener] [] (actionPerformed [evt] (. temp-text (setText (dosync (ref-set gool (str @gool "1"))))))))) (. two-button (addActionListener (proxy [ActionListener] [] (actionPerformed [evt] (. temp-text (setText (dosync (ref-set gool (str @gool "2"))))))))) (. three-button (addActionListener (proxy [ActionListener] [] (actionPerformed [evt] (. temp-text (setText (dosync (ref-set gool (str @gool "3"))))))))) (. four-button (addActionListener (proxy [ActionListener] [] (actionPerformed [evt] (. temp-text (setText (dosync (ref-set gool (str @gool "4"))))))))) (. five-button (addActionListener (proxy [ActionListener] [] (actionPerformed [evt] (. temp-text (setText (dosync (ref-set gool (str @gool "5"))))))))) (. six-button (addActionListener (proxy [ActionListener] [] (actionPerformed [evt] (. temp-text (setText (dosync (ref-set gool (str @gool "6"))))))))) (. seven-button (addActionListener (proxy [ActionListener] [] (actionPerformed [evt] (. temp-text (setText (dosync (ref-set gool (str @gool "7"))))))))) (. eight-button (addActionListener (proxy [ActionListener] [] (actionPerformed [evt] (. temp-text (setText (dosync (ref-set gool (str @gool "8"))))))))) (. nine-button (addActionListener (proxy [ActionListener] [] (actionPerformed [evt] (. temp-text (setText (dosync (ref-set gool (str @gool "9"))))))))) (. zero-button (addActionListener (proxy [ActionListener] [] (actionPerformed [evt] (if (empty? @gool) (do (. temp-text (setText (dosync (ref-set gool (str "0" @gool))))))))))) (doto frame (.setLayout (new GridLayout 4 2 3 3)) ;; setting up the layout. (.add temp-text) (.add one-button) (.add two-button) (.add three-button) (.add four-button) (.add five-button) (.add six-button) (.add seven-button) (.add eight-button) (.add nine-button) (.add zero-button) (.add plus-button) (.add divide-button) (.add minus-button) (.add calculate-button) (.add clear-button) (.setSize 250 300) (.setVisible true)))) (twocents) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---