2

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

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

4

1 回答 1

2

这在开箱即用的情况下是不可能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更惯用的东西。

于 2012-04-14T15:55:47.327 回答