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:08

I know that you are hinting for classical desktop solutions, but web fits quite well with clojure. I've written a complete audio application where everything is hooked up so that if you add music to the audio folder it is reflected in the web UI. Just saying that Desktop application isn't the only way :)

查看更多
你好瞎i
3楼-- · 2019-01-16 00:10

There's been talk on the mailing list about a few Cells (a la Kenny Tilton's Cells) implementations. It's a pretty neat way to do GUI programming.

查看更多
小情绪 Triste *
4楼-- · 2019-01-16 00:14

I don't think there is an official one, but personally I would take advantage of the fact that I am using one of the most powerful language in the world and just imagine what the perfect gui code would look like:

(form {:title :on-close dispose :x-size 500 :y-size 450}
  [(button {:text "Close" :id 5 :on-click #(System/exit 0) :align :bottom})
   (text-field {:text "" :on-change #(.println System/out (:value %)) :align :center})
   (combo-box {:text "Chose background colour" :on-change background-update-function
               :items valid-colours})])

Your idea would differ but this should hopefully the above gives you some idea.

查看更多
爷、活的狠高调
5楼-- · 2019-01-16 00:17

From this page:

(import '(javax.swing JFrame JButton JOptionPane)) ;'
(import '(java.awt.event ActionListener))          ;'

(let [frame (JFrame. "Hello Swing")
     button (JButton. "Click Me")]
 (.addActionListener button
   (proxy [ActionListener] []
     (actionPerformed [evt]
       (JOptionPane/showMessageDialog  nil,
          (str "<html>Hello from <b>Clojure</b>. Button "
               (.getActionCommand evt) " clicked.")))))

 (.. frame getContentPane (add button))

 (doto frame
   (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
   .pack
   (.setVisible true)))

print("code sample");

And, of course, it would be worth looking at the interoperability section of clojure's website.

查看更多
Deceive 欺骗
6楼-- · 2019-01-16 00:17

So I didn't see Fn-Fx on this list, from Timothy Baldridge (halgiri). This is a Clojure library providing a functional abstraction over JavaFX.

It can be found on Github at https://github.com/halgari/fn-fx.

To use, make sure you are using a recent version of Java (1.8 90+) and add a dependency to the github repo by adding the following to your project.clj:

:plugins [[lein-git-deps "0.0.1-SNAPSHOT"]] :git-dependencies [["https://github.com/halgari/fn-fx.git"]]

I have tried it, and it works out of the box.

查看更多
▲ chillily
7楼-- · 2019-01-16 00:19

I would rather go for clojurefx, it is a bit premature, but it does work and saves you time.

I started my GUI with seesaw and then tried another component in clojurefx.

I have finished both, and I am convinced that I am going to refactor the seesaw one to clojurefx.

After all, JavaFX is the way to go forward.

It feels lighter than seesaw. Or at least, writing it..

Bindings work, listeners work, most of the component work, otherwise, just use one of the macros to create a constructor for that particular case and job done. Or, if you find it difficult, write some methods in Java and ask for help to improve clojurefx.

The guy who wrote clojurefx is busy at the moment, but you can fork the project and do some fixes.

查看更多
登录 后发表回答