如何WindowStateListener
在 scala.swing 中为 MainFrame 的反应添加一个?
中的所有课程scala.swing.event
似乎都不合适?
这在开箱即用的情况下是不可能scala.swing
的(我只是确定了 2.9.1 的源代码WindowStateListener
),但是很容易自己动手:
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
更惯用的东西。