到目前为止,我一直使用Netbeans的Swing GUI生成功能来构建我的Java应用程序的GUI界面。
现在我已经开始使用Eclipse进行我的项目开发,并放弃了Netbeans。除了手动编写GUI的所有代码之外,还有哪些其他方法可以快速且更好地构建GUI呢?
过去两年中,我一直在使用 Visual Editor 和 Windows Builder Eclipse 插件。我发现 Windows Builder 的表现非常出色!它为您提供了非常干净和易读的代码。因此,我更喜欢使用 Windows Builder,这样您就可以做到与 Microsoft Visual Studio 功能相同的“拖放等”操作。
不太好的地方:
(defn -main [& args]
(invoke-later
(-> (frame :title "Hello",
:content "Hi there",
:on-close :exit)
pack!
show!)))
在这里,“Hi there”被自动转换为JLabel,但你也可以将任何Swing组件放在:content
中。
以下是我seesaw-buttons
示例项目的更复杂的例子:
(ns seesaw-buttons.core
(:use seesaw.core))
(defn -main [& args]
(invoke-later
(let [open-action (action
:handler (fn [e] (alert "I should open a new something."))
:name "Open ..."
:key "menu O"
:tip "Open a new something something.")
exit-action (action
:handler (fn [e] (.dispose (to-frame e)))
:name "Exit"
:tip "Close this window")]
(-> (frame :title "Hello",
:content (border-panel
:north (toolbar :items [open-action exit-action])
:center "More content here..."),
:on-close :exit)
pack!
show!))))
另一种方法是使用一些XML库,例如SwixML。需要对其进行一些代码更改才能使用,但可以很好地将UI与您的代码分离。另一方面,它只是swing类的包装器。在我看来,这既是优点也是缺点。这也是一个非常好的快速原型设计库,因为您可以(通常)尝试对话框,直到它足够令人满意。