0 Replies - 293 Views - Last Post: 19 March 2009 - 04:30 PM

#1 Raynes   User is offline

  • D.I.C Lover
  • member icon

Reputation: 614
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

[Lisp] Swing example in Clojure

Posted 19 March 2009 - 04:30 PM

Description: To use this snippet you will need the JRE installed along with Clojure. Basic compilation instructions will work nothing special has to be done to compile this. I recommend installing Enclojure and Netbeans for a quick way to try this example.This snippet creates a fully functional calculator with a simple GUI using the Swing GUI toolkit.
(ns rayne.main
  (:gen-class)
  (:import (javax.swing JFrame JTextField JButton JPanel BoxLayout BorderFactory)
           (java.awt.event ActionListener)
           (java.awt GridLayout BorderLayout Dimension)))

(def numbers (ref []))
(def times-clicked (ref 0))

(defn calc
  "Gets the numbers from the vector and calculates them"
  [[n1 n2] op]
    (cond
      (= op "+") (+ n1 n2)
      (= op "*") (* n1 n2)
      (= op "-") (- n1 n2)
      (= op "/") (try (double (/ n1 n2))
                 (catch ArithmeticException _ "Cannot divide by zero."))))

(defn proof-number
  "Returns n if n is a number. Returns 0 otherwise"
  [n]
  (if (number? n) n 0))

(defn add-op-button
  "Creates a new button and adds the works too it."
  [op text button]
  (.addActionListener button
      (proxy [ActionListener] []
        (actionPerformed [e]
        (dosync
          (ref-set numbers (conj @numbers (proof-number (read-string (.getText text)))))
          (ref-set times-clicked (inc @times-clicked))
          (.grabFocus text)
          (if (= @times-clicked 2)
            (do
              (let [result (.toString (calc @numbers op))]
                (.setText text result)
                (ref-set numbers [(read-string result)])
                (ref-set times-clicked 1)))
            (do
              (.setText text ""))))))))

(defn clear-button
  "Special case clear button"
  [button text]
  (.addActionListener button
    (proxy [ActionListener] []
      (actionPerformed [e]
        (dosync
          (ref-set times-clicked 0)
          (ref-set numbers [])
          (.setText text ""))))))

(defn -main
  "Main function, sets up the frame and glues everything together"
  []
  (let [frame (JFrame. "Calculator")
        add-button (JButton. "+")
        sub-button (JButton. "-")
        mul-button (JButton. "*")
        div-button (JButton. "/")
        clr-button (JButton. "Clear!")
        text-field (JTextField.)
        outter-pane (JPanel. (BorderLayout. 0 1))
        pane (JPanel. (GridLayout. 1 0))]
    (doto pane
      (.add add-button (add-op-button "+" text-field add-button))
      (.add sub-button (add-op-button "-" text-field sub-button))
      (.add mul-button (add-op-button "*" text-field mul-button))
      (.add div-button (add-op-button "/" text-field div-button))
      (.add clr-button (clear-button clr-button text-field)))
    (doto outter-pane
      (.add text-field BorderLayout/NORTH)
      (.add pane BorderLayout/EAST))
    (doto frame
      (.add outter-pane)
      (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
      (.setResizable false)
      (.setSize 341 100)
      (.setVisible true))))


Is This A Good Question/Topic? 0
  • +

Page 1 of 1