2012-12-03 28 views
2

我不知道爲什麼下面的代碼不工作:問題與的KeyEvents

object Main extends SimpleSwingApplication { 

    val dim = new Dimension(500, 110) 

    def top = new MainFrame { 
    contents = new FlowPanel{ 
     listenTo(keys, mouse.clicks) 
     reactions += { 
     case MouseClicked(_,_,_,_,_) => println("Mouse clicked") 
     case KeyPressed(_, Key.C, _, _) => println("C pressed") 
     case KeyTyped(_, Key.C, _, _) => println("C typed") 
     case KeyReleased(_, Key.C, _, _) => println("C released") 
     } 
    } 
    size = dim 
    } 

} 

的鼠標點擊將被認可,但按鍵不會。我也嘗試了不同的鍵或修飾符,但似乎沒有任何效果。我究竟做錯了什麼?

(在偶然的情況下,這可能有些事情要與環境:我在Windows 7上運行此代碼SBT)

+0

正如一個側面說明:它不是那麼不可能,因爲你可能會想:我有不同的操作系統[它的行爲不同](http://stackoverflow.com/q/5736872/298389)上的swing popup的問題。 –

+0

也許是因爲面板不是用於keyevents的?它是否適用於TextField? –

回答

2

this answer工作,看來你需要包括行

focusable = true 

爲您的FlowPanel。它應該然後工作。

+0

謝謝,抱歉發佈這個有點重複的問題。 (我想問題是我正在用[scala-swing]搜索) – SHildebrandt

+0

@SHildebrandt我發現Google比這個網站上的搜索更好用! –