Clojure에서 GUI를 수행하는 가장 좋은 방법은 무엇입니까?
Clojure 에서 GUI를 수행하는 가장 좋은 방법은 무엇입니까 ?
기능적인 Swing 또는 SWT 래퍼 의 예가 있습니까? 또는 일부 매크로를 사용하여 s- 표현식 으로 쉽게 래핑 될 수있는 JavaFX 선언적 GUI 설명 과의 통합
튜토리얼이 있습니까?
나는 시소 를 겸손히 제안 할 것이다 .
다음 은 Java 또는 Swing 지식이 없다고 가정 하는 REPL 기반 학습서 입니다.
시소는 @tomjen이 제안한 것과 매우 흡사합니다. "Hello, World"는 다음과 같습니다.
(use 'seesaw.core)
(-> (frame :title "Hello"
:content "Hello, Seesaw"
:on-close :exit)
pack!
show!)
그리고 여기 @Abhijith와 @dsm의 예가 있습니다.
(ns seesaw-test.core
(:use seesaw.core))
(defn handler
[event]
(alert event
(str "<html>Hello from <b>Clojure</b>. Button "
(.getActionCommand event) " clicked.")))
(-> (frame :title "Hello Swing" :on-close :exit
:content (button :text "Click Me" :listen [:action handler]))
pack!
show!)
스튜어트 시에라 (Stuart Sierra)는 최근 clojure (및 스윙)를 사용한 GUI 개발에 관한 일련의 블로그 게시물을 발표했습니다. 여기서 시작하십시오 : http://stuartsierra.com/2010/01/02/first-steps-with-clojure-swing
GUI 프로그래밍을하고 싶다면 온도 변환기 또는 개미 식민지를 가리 킵니다 .
특히 사용자 정의 구성 요소를 만드는 경우 하위 클래스를 통해 Swing의 많은 작업이 수행됩니다. 이를 위해 proxy 와 gen-class의 두 가지 필수 함수 / 매크로가 있습니다.
이제 나는 당신이 더 Lispy 방식으로 어디로 가고 있는지 이해합니다. 아직 그런 것이 없다고 생각합니다. 멋진 GUI 빌드 프레임 워크 a-la CLIM을 구축 하지 말 것을 강력히 권유하지만 Lispy를 더 많이 사용하려면 Swing 응용 프로그램 작성을 시작하고 일반적인 패턴을 매크로로 추상화하십시오. 그렇게 할 때, 당신은 일종의 GUI를 작성하는 언어로 끝날 수도 있고, 공유하고 성장할 수있는 매우 일반적인 것들 일 수도 있습니다.
Clojure에서 GUI를 작성할 때 잃어버린 한 가지는 Matisse와 같은 도구를 사용한다는 것입니다. 이것은 Java (GUI)의 일부와 Clojure (논리)의 일부를 작성하는 것을 강력하게 지적합니다. 실제로 논리에서와 같이 매크로를 사용하여 종류의 논리에 대한 언어를 작성할 수 있으며 GUI보다 더 많은 이점이 있다고 생각합니다. 분명히 응용 프로그램에 따라 다릅니다.
이 페이지에서 :
(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");
물론 clojure 웹 사이트 의 상호 운용성 섹션을 살펴볼 가치가 있습니다 .
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.
There is a wrapper for MigLayout in clojure contrib. You can also take a look at this gist. 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)))
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.
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.
Here is another very basic swing wrapping example:
; time for some swing
(import '(javax.swing JFrame JTable JScrollPane))
(import '(javax.swing.table DefaultTableModel))
(let
[frame (JFrame. "Hello Swing")
dm (DefaultTableModel.)
table (JTable. dm)
scroll (JScrollPane. table)]
(doto dm
(.setNumRows 30)
(.setColumnCount 5))
(.. frame getContentPane (add scroll))
(doto frame
(.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
(.pack)
(.setVisible true)))
I asked myself the same question of writing a GUI in Clojure with Swing and came up with the library signe
It lets you use represent your domain model as a single Clojure data structure wrapped inside an atom.
See the examples here.
I've been developing a Java applet in which everything is written in Clojure except the applet code, which is written in Java. The applet invokes the Clojure code's callbacks of init, paint, etc from java's hooks for those methods that are defined by the applet model. So the code ends up being 99.999 percent Clojure and you don't have to think about the tiny Java piece at all for the most part.
There are some drawbacks to this approach, which I hope to discuss in more detail on the Clojure Google Group. I think the Clojure developers should include a native way of building applications. Presently you can do whatever GUI stuff you like from the REPL, but if you want a deliverable GUI application, it is necessary to write some Java to call the Clojure code. Also, it seems like the architecture of a Java Applet kind of forces you outside of Clojure's more idiomatic best practices, requiring you to use mutable state, etc.
But also, I am not very far along with Clojure yet and it might be the case that it is possible and I just haven't discovered how to do it correctly yet.
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.
Clojure and SWT is the best approach for doing GUI(s). Essentially, SWT is a plug and play style approach for developing software.
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.
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 :)
My preferred Clojure UI environment uses IO.js (Node for ES6) + Electron (Container) + Quiescent (ReactJS wrapper).
참고URL : https://stackoverflow.com/questions/233171/what-is-the-best-way-to-do-guis-in-clojure
'IT' 카테고리의 다른 글
XML 명령 줄 처리를위한 Grep 및 Sed (0) | 2020.06.16 |
---|---|
인식 할 수없는 SSL 메시지, 일반 텍스트 연결? (0) | 2020.06.16 |
Java에서 일반 클래스 인스턴스화 (0) | 2020.06.16 |
오라클“(+)”연산자 (0) | 2020.06.16 |
일반 리포지토리 EF 4.1의 요점 (0) | 2020.06.16 |