2011-09-06 37 views
3

Scala REPL表現得很奇怪,或許這是預期的行爲。當我創建一個MainFrame對象並將其可見性設置爲true時,將顯示一個窗口。但是,如果我關閉窗口Scala REPL退出到終端。簡單的會話:Scala REPL自動退出

~$ scala 
scala> import swing._ 
scala> val frame = new MainFrame() 
scala> frame.visible = true 
~$        //when I close the window 

上的Kubuntu

回答

7

這是MainFrame類本身,再加上System.exit的不是非常OO行爲。

這是MainFrame整個源:

class MainFrame extends Frame { 
    override def closeOperation() { sys.exit(0) } 
} 

望着那,這是很清楚的是,當窗口被關閉,System.exit被調用,JVM將退出。

如果您只是在試驗時發現此問題,則解決方法是不要這樣做!如果您想在REPL中使用框架,則可以覆蓋closeOperation以不退出JVM - 或者僅使用Frame(因爲MainFrame的唯一附加功能是JVM退出行爲)。

7

因爲它說,在the documentation我使用Scala的2.9.1:

關閉框架和關閉時退出應用程序。

(即,它會關閉該REPL運行在JVM)

爲了防止這種行爲,你既可以只使用一個Frame代替,或重寫closeOperation方法。

這裏是source for MainFrame.scala供參考:

/** 
* A frame that can be used for main application windows. Shuts down the 
* framework and quits the application when closed. 
*/ 
class MainFrame extends Frame { 
    override def closeOperation() { sys.exit(0) } 
}