如何在scala.swing中为MainFrame的反应添加WindowStateListener
?
scala.swing.event
中的所有类似乎都不合适?
答案 0 :(得分:2)
开箱即用scala.swing
是不可能的(我只是为WindowStateListener
插入了2.9.1源代码来确定),但是很容易推出自己的:
import swing._
case class WindowStateChanged(
override val source: Window,
oldState: Int,
newState: Int
) extends event.WindowEvent(source)
trait StateListeningWindow extends Window { outer =>
this.peer.addWindowStateListener(new java.awt.event.WindowStateListener {
def windowStateChanged(e: java.awt.event.WindowEvent) {
publish(WindowStateChanged(outer, e.getOldState, e.getNewState))
}
})
}
现在只需混合StateListeningWindow
:
object WindowStateDemo extends SimpleSwingApplication {
def top = new MainFrame with StateListeningWindow {
import java.awt.Frame._
reactions += {
case WindowStateChanged(_, _, ICONIFIED) => // Do something.
case WindowStateChanged(_, oldState, newState) => // Something else.
}
}
}
你也可以轻松地将java.awt.Frame
常量包装成更惯用的东西。