[英]Compile error trying to bind mouse event listener in ScalaFx
I'm coding a little test application that uses the Canvas
class in Scala with scalafx
. 我正在编写一个使用
scalafx
在Scala中使用Canvas
类的小测试应用程序。 The problem is with: 问题在于:
gui.canvas.handleEvent(MouseEvent.MouseClicked){
a: MouseEvent => {
println("Mouse pos:" + a.sceneX+ " " + a.sceneY)
}
}
Throwing the error: 抛出错误:
[error] E:\dev\sg\src\main\scala\Test.scala:27: type mismatch;
[error] found : scalafx.scene.input.MouseEvent => Unit
[error] required: _2.HandlerMagnet[javafx.scene.input.MouseEvent,?]
where val _2: scalafx.scene.canvas.Canvas
[error] a: MouseEvent => {
[error] ^
I don't get why this exact same snippet compiles when it appears in the method setupGraphics
but NOT in when it's in the Main
object's code. 我不明白为什么这个完全相同的代码段在出现在
setupGraphics
方法中时会编译,而在Main
对象的代码中却不会出现。
BTW, having browsed quite a bit of ScalaFX code, I'm getting a bit dizzy because I've seen lots different ways to bind to an event, there's the one I'm using , setting the onMouseClicked variable (with and without a first class function), using filterEvent . 顺便说一句,浏览了很多ScalaFX代码后,我有点头晕,因为我看到了很多绑定事件的方法,有一种我正在使用的方法 , 设置onMouseClicked变量 (有无第一个类函数), 使用filterEvent 。 Also, for each you can use an anonymous function or create an event handler object.
同样,对于每个函数,您都可以使用匿名函数或创建事件处理程序对象。
Of all those, this is the only one that worked in this example; 在所有这些中,这是在此示例中唯一起作用的一个; trying to create an
EventHandler[MouseEvent]
and override handle()
also fails in this case. 在这种情况下,尝试创建
EventHandler[MouseEvent]
并覆盖handle()
也会失败。
I'm using Scala 2.10.5 我正在使用Scala 2.10.5
Full Source: 完整资料:
object Test {
import scalafx.Includes._
import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.stage.Stage
import scalafx.scene.Scene
import scalafx.scene.canvas.Canvas
import scalafx.scene.canvas.GraphicsContext
import scalafx.scene.input.MouseEvent
import scalafx.event.EventHandler
class GUI(val canvas:Canvas
,val stage: Stage
,val gc: GraphicsContext){}
object Main extends JFXApp {
var gui=setupGraphics()
gui.canvas.handleEvent(MouseEvent.MouseClicked){
a: MouseEvent => {
println("Mouse pos:" + a.sceneX+ " " + a.sceneY)
}
}
def setupGraphics():GUI={
val canvas = new Canvas(400, 400)
canvas.translateX=0
canvas.translateY=0
val gc = canvas.graphicsContext2D
canvas.handleEvent(MouseEvent.MouseClicked){
a: MouseEvent => {
println("Mouse pos:" + a.sceneX+ " " + a.sceneY)
}
}
stage = new PrimaryStage {
title = "asd"
height = 600
width = 800
scene=new Scene{content=canvas}
}
return new GUI(canvas,stage,gc)
}
}
}
This looks to me like a bug in Scala compiler, especially that you can correct this by using an intermediate variable for canvas
without modifying anything else. 在我看来,这看起来像Scala编译器中的错误,尤其是您可以通过使用
canvas
的中间变量来纠正此问题,而无需修改其他任何内容。 Instead of calling gui.canvas.handleEvent
, first assign canvas
to a variable then call handleEvent
on that: 不用调用
gui.canvas.handleEvent
,而是先将canvas
分配给一个变量,然后在该变量上调用handleEvent
:
var gui=setupGraphics()
val canvas = gui.canvas
canvas.handleEvent(MouseEvent.MouseClicked){
a: MouseEvent => {
println("Mouse pos:" + a.sceneX+ " " + a.sceneY)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.