我是一名玩scala swing的scala新手。並想翻譯scala.swing.Point
scala.swing.event.MousEvent
:Scala;類型(X)=> X的表達式不符合類型SupX
我想將鼠標事件發送給負責處理形狀選擇的類。但是因爲一個形狀相對於他的容器有一個位置(表),但是MouseEvent.point是相對於之前我應該翻譯/相關的窗口。
所以,我有一個Selection
類誰收到MouseEvent
S:
case class Selection(sheet:Sheet) {
def on(event:Event) = event match {
case clicked:MouseClicked => {
clicked.modifiers match {
case scala.swing.event.Key.Modifier.Control =>
sheet.getItemAt(clicked.point).map(addToSelection)
case _ =>
sheet.getItemAt(clicked.point).map(setSelection)
}
}
}
}
而一個Sheet
誰是他的形狀容器(並知道如何翻譯點)。
class Sheet extends Component {
private val selection = Selection(this)
listenTo(mouse.clicks, mouse.moves)
reactions += {
case e:MousePressed => selection.on(translate(e))
case e:MouseClicked => selection.on(translate(e))
}
/** Here is my problem :
* > Expression of type (MouseEvent) => MousePressed doesn't conform to expected type Event
*/
def translate(original: MouseEvent): Event = original match {
case pressed:MousePressed =>
pressed.copy(point=relativize(pressed.point))
case clicked:MouseClicked =>
clicked.copy(point=relativize(pressed.point))
case other:MouseEvent=>
other
}
}
我可以用一個醜陋的投繞過這個問題:
case pressed:MousePressed =>
pressed.copy(point=relativize(pressed.point)).asInstanceOf[MousePressed]
但後來我有另一個更奇怪的編譯器的問題:
Error:(32, 21) missing arguments for method copy in class MousePressed; follow this method with `_' if you want to treat it as a partially applied function
pressed.copy(point = relativize(pressed.point)).asInstanceOf[MousePressed]
在這裏,我迷路了,並需要你幫忙做這個簡單的轉換。
當然,所有的方法都使用scala.swing._
類型(和scala.swing之間從不混合。and
java.awt中。`)
非常感謝
非常感謝。而且它消除了投射的需要(我的Ide需要誰來解決這個錯誤的表達方式) –