我在clojure中设置了一个swing UI,并且有一个像:
这样的块 (doto main-frame
(.setUndecorated true)
(.setExtendedState Frame/MAXIMIZED_BOTH)
(.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
(.setVisible true)
)
但现在我想打电话
(.setBackground (.getContentPane main-frame) Color/BLACK)
在我将框架设置为可见之前,有没有比结束doto和使用(.instanceMember实例args *)语法更好的方法呢?
答案 0 :(得分:5)
如果你真的想要一个doto
,那么可能会这样做:
(doto main-frame
(.setUndecorated true)
(.setExtendedState Frame/MAXIMIZED_BOTH)
(.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
(-> (.getContentPane) (.setBackground Color/BLACK))
(.setVisible true))
上述内容依赖于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))