What is the best way to do GUIs in Clojure?

2019-01-15 23:55发布

What is the best way to do GUIs in Clojure?

Is there an example of some functional Swing or SWT wrapper? Or some integration with JavaFX declarative GUI description which could be easily wrapped to s-expressions using some macrology?

Any tutorials?

16条回答
虎瘦雄心在
2楼-- · 2019-01-16 00:22

Clojure and SWT is the best approach for doing GUI(s). Essentially, SWT is a plug and play style approach for developing software.

查看更多
虎瘦雄心在
3楼-- · 2019-01-16 00:24

If you want to do GUI programming I'd point to Temperature Converter or the ants colony.

Many things in Swing are done by sub-classing, particularly if you are creating custom components. For that there are two essential functions/macros: proxy and gen-class.

Now I understand where you are going with the more Lispy way. I don't think there's anything like that yet. I would strongly advise against trying to build a grandiose GUI-building framework a-la CLIM, but to do something more Lispy: start writing your Swing application and abstract out your common patterns with macros. When doing that you may end up with a language to write your kind of GUIs, or maybe some very generic stuff that can be shared and grow.

One thing you lose when writing the GUIs in Clojure is the use of tools like Matisse. That can be a strong pointing to write some parts in Java (the GUI) and some parts in Clojure (the logic). Which actually makes sense as in the logic you'll be able to build a language for your kind of logic using macros and I think there's more to gain there than with the GUI. Obviously, it depends on your application.

查看更多
何必那么认真
4楼-- · 2019-01-16 00:25

Stuart Sierra recently published a series of blog posts on GUI-development with clojure (and swing). Start off here: http://stuartsierra.com/2010/01/02/first-steps-with-clojure-swing

查看更多
ら.Afraid
5楼-- · 2019-01-16 00:26
爷的心禁止访问
6楼-- · 2019-01-16 00:27

There is a wrapper for MigLayout in clojure contrib. You can also take a look http://gist.github.com/261140. I am basically putting up whatever code I am writing as I am learning swing/miglayout.

dsm's example re-written in a lispy way using contrib.swing-utils

(ns test
      (:import (javax.swing JButton JFrame))
      (:use (clojure.contrib
          [swing-utils :only (add-action-listener)])))

    (defn handler
      [event]
      (JOptionPane/showMessageDialog nil,
        (str "<html>Hello from <b>Clojure</b>. Button "
          (.getActionCommand event) " clicked.")))

    (let [ frame (JFrame. "Hello Swing") 
           button (JButton. "Click Me")  ]
      (add-action-listener button handler)
        (doto frame
          (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
          (.add button)
          (.pack)
          (.setVisible true)))
查看更多
太酷不给撩
7楼-- · 2019-01-16 00:28

Nobody yet suggested it, so I will: Browser as UI platform. You could write your app in Clojure, including an HTTP server and then develop the UI using anything from HTML to hiccup, ClojureScript and any of the billions of JS libaries you need. If you wanted consistent browser behaviour and "desktop app look'n'feel" you could bundle chrome with your app.

This seems to be how Light Table is distributed.

查看更多
登录 后发表回答