我在关闭 Scala 摆动框架时遇到问题。这是我的退出按钮的代码
val buttonExit = new Button {
text = "Exit"
action = Action("Exit") {
WorldActor.run(false)
closer
}
}
更近的函数定义为:
def closer (){
top.close
}
其中 top 是 MainFrame。每次我尝试关闭时,它都会暂停并停止响应。
我在关闭 Scala 摆动框架时遇到问题。这是我的退出按钮的代码
val buttonExit = new Button {
text = "Exit"
action = Action("Exit") {
WorldActor.run(false)
closer
}
}
更近的函数定义为:
def closer (){
top.close
}
其中 top 是 MainFrame。每次我尝试关闭时,它都会暂停并停止响应。
好像可以打电话
dispose()
在框架上。
dispose
被实施scala.swing.Window
所以适用于框架和对话框。
如果在最后一个 Frame 上调用,调用dispose
会关闭(以可恢复的方式,使用pack
并visible = true
重新打开)额外的 Frame 并终止应用程序。
quit()
在调用 System.exit 之前提供的任何关闭代码的任何 Frame 调用上终止应用程序。
这是一个快速的技巧来说明
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
}
}
}
我不是 Scala 方面的专家,但我做过一些 Java swing 开发。尝试使用
我认为
WorldActor.run(false)
暂停程序,尝试删除它,也尝试添加System.exit(0)
.
def closer(){
exit(0) //Java's System.exit(0) ?
}
或者可能System.exit(0)
放在top.close
.
另外,您是否将默认关闭操作设置为不执行任何操作?
peer.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE)
我不熟练,scala.swing._
但我认为以下几点:
哪里 top 是 MainFrame
如何定义 MainFrame?
如果你定义为:
def top = new MainFrame { ... }
以及以下代码:
val a = top
val b = top
产生两个不同的实例。
所以b.close
不关闭a
。
反之亦然,即a.close
不关闭b
。