Clojure 编程/示例/温度转换器 GUI
外观
< Clojure 编程 | 示例
简单的 GUI 应用程序示例(来自讨论小组,已更新至 20080329 版本)
(import '(javax.swing JFrame JLabel JTextField JButton)
'(java.awt.event ActionListener)
'(java.awt GridLayout))
(let [frame (new JFrame "Celsius Converter")
temp-text (new JTextField)
celsius-label (new JLabel "Celsius")
convert-button (new JButton "Convert")
fahrenheit-label (new JLabel "Fahrenheit")]
(. convert-button
(addActionListener
(proxy [ActionListener] []
(actionPerformed [evt]
(let [c (Double/parseDouble (. temp-text (getText)))]
(. fahrenheit-label
(setText (str (+ 32 (* 1.8 c)) " Fahrenheit"))))))))
(doto frame
;(.setDefaultCloseOperation (JFrame/EXIT_ON_CLOSE)) ;uncomment this line to quit app on frame close
(.setLayout (new GridLayout 2 2 3 3))
(.add temp-text)
(.add celsius-label)
(.add convert-button)
(.add fahrenheit-label)
(.setSize 300 80)
(.setVisible true)))