尝试在跷跷板中制作菜单。我无法弄清楚主要内部代码中的“项目”新动作开放动作等应该是什么。我认为它可能是菜单项文本,或者可能是函数回调。最后,有一个示例显示如何控制菜单项文本,然后能够在我的代码中给出回调,这将是一件好事。不知道该怎么做。
修改:此代码现在可以正常运行。
(ns hello-seesaw.core
(:gen-class)
(:use seesaw.core))
(defn handler
[event]
(alert event
(str "<html>Hello from <b>Clojure</b>. Button "
(.getActionCommand event) " clicked.")))
(defn menu-handler
[event]
()
)
(def new-action (menu-item :text "New" :listen [:action menu-handler]))
(def open-action (menu-item :text "Open" :listen [:action menu-handler]))
(def save-action (menu-item :text "Save" :listen [:action menu-handler]))
(def exit-action (menu-item :text "Exit" :listen [:action menu-handler]))
(def copy-action (menu-item :text "Copy" :listen [:action menu-handler]))
(def paste-action (menu-item :text "Paste" :listen [:action menu-handler]))
(defn -main [& args]
(invoke-later
(->
(frame :title "Hello Swing" :on-close :exit
:content (button :text "Click Me" :listen [:action handler])
:menubar
(menubar :items
[
(menu :text "File" :items [new-action open-action save-action exit-action])
(menu :text "Edit" :items [copy-action paste-action])
]
)
)
pack!
show!))
)
答案 0 :(得分:3)
每个*-action
项都会引用一种菜单项(menu-item
,checkbox-menu-item
或radio-menu-item
)。其中每个都有same options为button
,因此您可以像在第一帧中执行按钮一样配置每个。
您对新按钮的定义可能如下所示:
(def new-action (menu-item :text "New" :listen [:action handler]))
顺便说一句,如果你尝试在定义了两个帧的情况下运行程序,你将获得IllegalArgumentException
(因为你将第一帧调用的结果传递给第二帧)。一个简单的解决方案是将两者结合起来。
(frame :title "Hello Swing" :on-close :exit
:content (button :text "Click Me" :listen [:action handler])
:menubar
(menubar :items
[(menu :text "File" :items [new-action open-action save-action exit-action])
(menu :text "Edit" :items [copy-action paste-action])]))