Clojure:窗口框架关闭时退出程序
我希望我的 Clojure 程序在 JFrame 关闭时退出。
我试图捕获并处理关闭事件,如下所示:
(def exit-action (proxy [WindowAdapter] []
(windowClosing [event] (fn [] (System/exit 0)))
)
)
(.addWindowListener frame exit-action)
这不会引发任何明显的错误,但它似乎也没有达到我想要的效果。
感谢您的帮助。
答案:
改编 Rekin 的答案就成功了:
(.setDefaultCloseOperation frame JFrame/EXIT_ON_CLOSE)
请注意,那是:
setDefaultCloseOperation
不是:
setDefaultOperationOnClose
I'd like my Clojure program to exit when a JFrame is closed.
I'm attempting to trap and handle the close event as such:
(def exit-action (proxy [WindowAdapter] []
(windowClosing [event] (fn [] (System/exit 0)))
)
)
(.addWindowListener frame exit-action)
This doesn't throw any obvious errors but it also doesn't appear to do what I want.
Assistance is appreciated.
Answer:
Adapting Rekin's answer did the trick:
(.setDefaultCloseOperation frame JFrame/EXIT_ON_CLOSE)
Note that that is:
setDefaultCloseOperation
not:
setDefaultOperationOnClose
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 Java 中:
更详细的示例可以在官方 关于框架的 Java Swing 教程中找到
In Java it's:
More elaborate examples can be found in official Java Swing tutorial about Frames
我会使用
EXIT_ON_CLOSE
,但您的第一次尝试不起作用的原因是代理正文应包含(System/exit 0)
,而不是(fn [](系统/退出0))
。您不是退出,而是返回(然后丢弃)一个函数,当调用时,该函数会退出。I would use
EXIT_ON_CLOSE
, but the reason your first attempt didn't work is that the body of proxy should contain(System/exit 0)
, not(fn [] (System/exit 0))
. Rather than exiting, you were returning (and then throwing away) a function that, when called, would exit.这是我在我的 博客 上展示的一个简短的演示程序不久前
请注意timer-fn[]中设置默认关闭操作的行。与 Java 差不多,但有一点语法调整。
该博客条目的目的是展示 Clojure 中的闭包示例。
Here's a short demonstration program I showed on my blog a while ago
Note the line in timer-fn[] that sets the default close operation. Just about like Java but with a little syntax fiddling.
The purpose of the blog entry was to show an example of a closure in Clojure.