2011-12-15 63 views
2

我在關閉我的斯卡拉鞦韆架時出現問題。下面是我的退出按鈕關閉斯卡拉鞦韆架

val buttonExit = new Button { 
    text = "Exit" 
    action = Action("Exit") { 
    WorldActor.run(false) 
    closer 
    } 
} 

的代碼越接近函數定義爲:

def closer(){ 
    top.close 
} 

,其中頂部是大型機上。每次我嘗試關閉時,它都會暫停並停止響應。

+0

「WorldActor」是scala「Actor」嗎? – 2011-12-15 21:18:40

回答

5

好像你可以撥打

dispose() 
的框架

disposescala.swing.Window上實現,因此適用於幀和對話框。

調用dispose關閉(在可恢復的方式,使用packvisible = true重新打開)其它幀並終止應用程序,如果叫上最後一幀。

要在調用System.exit之前調用任何關閉代碼的任何Frame調用quit()上終止應用程序。

這裏有一個快速的黑客攻擊,說明

import swing._ 

object SwingThing extends SimpleSwingApplication { 
    def top = new MainFrame {frame => 
    val sf = new Frame {secondFrame => 
     title = "Secondary Frame" 
     visible = true 
     contents = new FlowPanel { 
     contents += new Button(Action("Close Me") {secondFrame.dispose()}) 
     contents += new Button(Action("Exit")  {quit()}) 
     } 
    } 
    val recoverBtn = new Button(Action("Recover") {sf.pack(); sf.visible = true}) 
    val closeBtn = new Button(Action("Close Me") {frame.dispose()}) 
    val exitBtn = new Button(Action("Exit")  {quit()}) 

    contents = new FlowPanel { 
     contents += recoverBtn 
     contents += closeBtn 
     contents += exitBtn 
    } 
    } 
} 
1

我不是Scala的專家,但我做了一些Java swing開發。嘗試使用

我覺得

WorldActor.run(false) 

暫停程序,請嘗試刪除,也嘗試添加System.exit(0)

def closer(){ 
    exit(0) //Java's System.exit(0) ? 
} 

或者可能在top.close之後放System.exit(0)

另外,你有沒有設置默認關閉操作什麼都不做?

peer.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE) 
+0

在哪裏以及如何設置clasa中的默認關閉操作。你知道嗎? – 2011-12-15 19:02:27

0

我不擅長scala.swing._但我認爲以下幾點:

,其中頂部是大型機
如何定義主機?

如果定義爲:

def top = new MainFrame { ... } 

以下代碼:

val a = top 
val b = top 

產生兩個不同的實例。

因此b.close不關閉a
反之亦然,即a.close不關閉b