2012-04-13 47 views

回答

2

這是不可能的scala.swing開箱(我只是grepped的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常量的東西更地道。

+1

謝謝。我可能可以將這種技術用於我還需要的其他事件偵聽器(如屬性更改偵聽器)。 – Ralph 2012-04-14 16:15:59