在scala.swing中添加WindowStateListener

时间:2012-04-13 14:17:48

标签: java swing scala

如何在scala.swing中为MainFrame的反应添加WindowStateListener

scala.swing.event中的所有类似乎都不合适?

1 个答案:

答案 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常量包装成更惯用的东西。