Hello, SWT(Clojure) World!

Posted on 12月 3rd, 2012 by cx20

SWT(Clojure)

SWT(Standard Widget Toolkit) は Java で GUI を扱うためのライブラリである。
IBM により AWT や Swing を置き換える目的で作成された。
以下は Clojure による SWT の使用例となっている。

ソースコード

(import 
  (org.eclipse.swt SWT)
  (org.eclipse.swt.widgets Display)
  (org.eclipse.swt.widgets Shell)
  (org.eclipse.swt.widgets Label)
  (org.eclipse.swt.layout FillLayout)
  (org.eclipse.swt.events ShellAdapter))
 
(def display
  (Display.))
 
(def shell
  (Shell. display))
 
(def layout
  (FillLayout. SWT/VERTICAL))
 
(def label
  (Label. shell SWT/BORDER))
 
(doto label
  (.setText "Hello, SWT World!"))
 
(doto shell
  (.setText "Hello, World")
  (.setLayout layout)
  (.setSize 640 480)
  (.addShellListener
    (proxy [ShellAdapter] []
      (shellClosed [evt]
        (System/exit 0))))
  (.open))
 
(defn while-loop [display shell]
  (loop []
    (if (.isDisposed shell)
      (.dispose display)
      (do
        (if (not (.readAndDispatch display))
          (.sleep display))
        (recur)))))
 
(while-loop display shell))

実行方法

C:¥> SET CLASSPATH=org.eclipse.swt.win32.win32.x86_3.6.1.v3655c.jar;%CLASSPATH%
C:¥> clj Hello.clj

実行結果

+------------------------------------------+
|Hello, World!                    [_][~][X]|
+------------------------------------------+
|Hello, SWT World!                         |
|                                          |
|                                          |
|                                          |
|                                          |
|                                          |
|                                          |
|                                          |
|                                          |
|                                          |
+------------------------------------------+

Tags:

Categories: Clojure, Java, SWT

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

WP-SpamFree by Pole Position Marketing