我在clojure中设置了一个swing UI,并且有一个块:
(doto main-frame
(.setUndecorated true)
(.setExtendedState Frame/MAXIMIZED_BOTH)
(.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
(.setVisible true)
)
Run Code Online (Sandbox Code Playgroud)
但现在我想打电话
(.setBackground (.getContentPane main-frame) Color/BLACK)
Run Code Online (Sandbox Code Playgroud)
在我将框架设置为可见之前,有没有比结束doto和使用(.instanceMember实例args*)语法更好的方法呢?
如果你真的想要一个doto,那么也许这样做:
(doto main-frame
(.setUndecorated true)
(.setExtendedState Frame/MAXIMIZED_BOTH)
(.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
(-> (.getContentPane) (.setBackground Color/BLACK))
(.setVisible true))
Run Code Online (Sandbox Code Playgroud)
以上内容依赖于doto不仅限于Java方法的事实,它只是将其第一个参数(已评估)作为后面每个表单的第一个参数插入.
doto虽然上面的内容不太可读,但我还是会结束.或者,也许只是定义一个set-background-on-content-pane函数(显然需要main-frame)并在以下内容中使用它doto:
(defn set-bg-on-frame [fr color] (.setBackground (.getContentPane fr) color))
(doto main-frame
.
.
.
(set-bg-on-frame Color/BLACK)
(.setVisible true))
Run Code Online (Sandbox Code Playgroud)