Scala REPL 自动退出
Scala REPL 的行为很奇怪,或者这可能是预期的行为。当我创建 MainFrame 对象并将其可见性设置为 true 时,会显示一个窗口。但是,如果我关闭窗口,Scala REPL 将退出到终端。示例会话:
~$ scala
scala> import swing._
scala> val frame = new MainFrame()
scala> frame.visible = true
~$ //when I close the window
我在 kubuntu 上使用 scala 2.9.1
Scala REPL is behaving oddly or perhaps this is the expected behavior. When I create a MainFrame object and set its visibility true, a window is displayed. However, If I close the window the Scala REPL exits to the terminal. Sample session:
~$ scala
scala> import swing._
scala> val frame = new MainFrame()
scala> frame.visible = true
~$ //when I close the window
I am using scala 2.9.1 on kubuntu
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它是
MainFrame
类本身,加上System.exit
的不太 OO 行为。这是
MainFrame
的完整源代码:看一下,很明显,当窗口关闭时,
System.exit
被调用,JVM 将退出。如果您只是在尝试时发现了这一点,那么解决方法就是不要这样做!如果您想在 REPL 中使用框架,那么您可以覆盖
closeOperation
以不退出 JVM - 或者仅使用Frame
(因为 MainFrame 的唯一附加功能是 JVM 退出行为)。It's the
MainFrame
class itself, coupled with the not-very-OO behaviour ofSystem.exit
.This is the entire source of
MainFrame
:Looking at that, it's pretty clear that when the window is closed,
System.exit
is called and the JVM will quit.If you were just experimenting when you found this, the workaround is to just not do this! If you want to use a frame in the REPL, then you can either override
closeOperation
to not exit the JVM - or just use aFrame
(since the only additional functionality with MainFrame is the JVM exit behaviour).正如文档中所述:
(即,它关闭运行 REPL 的 JVM。)
要防止这种行为,您可以简单地使用
Frame
相反,或者重写closeOperation
方法。这是
MainFrame.scala
的源代码供参考:As it says in the documentation:
(I.e., it shuts down the JVM which the REPL runs in.)
To prevent this behavior you could either simply use a
Frame
instead, or override thecloseOperation
method.Here is the source for
MainFrame.scala
for reference: