Clojure:窗口框架关闭时退出程序

发布于 2024-11-16 17:20:00 字数 561 浏览 1 评论 0原文

我希望我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泪痕残 2024-11-23 17:20:00

在 Java 中:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

更详细的示例可以在官方 关于框架的 Java Swing 教程中找到

In Java it's:

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

More elaborate examples can be found in official Java Swing tutorial about Frames

醉生梦死 2024-11-23 17:20:00

我会使用 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.

你げ笑在眉眼 2024-11-23 17:20:00

这是我在我的 博客 上展示的一个简短的演示程序不久前

(ns net.dneclark.JFrameAndTimerDemo
  (:import (javax.swing JLabel JButton JPanel JFrame Timer))
  (:gen-class))

(defn timer-action [label counter]
   (proxy 1 []
     (actionPerformed
      [e]
       (.setText label (str "Counter: " (swap! counter inc))))))

(defn timer-fn []
  (let [counter (atom 0)
        label (JLabel. "Counter: 0")
        timer (Timer. 1000 (timer-action label counter))
        panel (doto (JPanel.)
                (.add label))]
    (.start timer)
    (doto (JFrame. "Timer App")
      (.setContentPane panel)
      (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
      (.setLocation 300 300)
      (.setSize 200 200)
      (.setVisible true)))
  (println "exit timer-fn"))

(defn -main []
  (timer-fn))

请注意timer-fn[]中设置默认关闭操作的行。与 Java 差不多,但有一点语法调整。

该博客条目的目的是展示 Clojure 中的闭包示例。

Here's a short demonstration program I showed on my blog a while ago

(ns net.dneclark.JFrameAndTimerDemo
  (:import (javax.swing JLabel JButton JPanel JFrame Timer))
  (:gen-class))

(defn timer-action [label counter]
   (proxy 1 []
     (actionPerformed
      [e]
       (.setText label (str "Counter: " (swap! counter inc))))))

(defn timer-fn []
  (let [counter (atom 0)
        label (JLabel. "Counter: 0")
        timer (Timer. 1000 (timer-action label counter))
        panel (doto (JPanel.)
                (.add label))]
    (.start timer)
    (doto (JFrame. "Timer App")
      (.setContentPane panel)
      (.setDefaultCloseOperation JFrame/EXIT_ON_CLOSE)
      (.setLocation 300 300)
      (.setSize 200 200)
      (.setVisible true)))
  (println "exit timer-fn"))

(defn -main []
  (timer-fn))

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文