下面是我所看到的基于javafx示例的工作原理,但我在(ctlA.match(ke))上看到了一个错误,指向"match“,并说”所期望的标识符,但是找到了'match‘“。任何有复杂KeyEvent处理的scalafx示例链接都将不胜感激。
import scalafx.Includes._
import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.scene.input.{KeyCode, KeyCombination, KeyCodeCombination, KeyEvent}
import scalafx.scene.Scene
import scalafx.stage.Stage
object Main extends JFXApp {
val ctlA = new KeyCodeCombination(KeyCode.A, KeyCombination.ControlDown)
stage = new PrimaryStage {
scene = new Scene {
onKeyPressed = { ke =>
if (ctlA.match(ke))
println("Matches ^A")
else
println("No match")
}
}
}
}发布于 2018-06-20 15:34:07
这是个奇怪的问题。ScalaFX显然只是JavaFX API的包装器,因此它会尽可能忠实地遵循该API。在本例中,存在一个小问题,因为match既是属于KeyCodeCombination的函数的名称,也是Scala关键字的名称--这就是为什么编译在达到此点时失败的原因: Scala编译器认为这是match关键字,因此无法理解它。
幸运的是,有一个简单的解决方案:只需将match封装在后面,这样您的代码就变成:
import scalafx.Includes._
import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.scene.input.{KeyCode, KeyCombination, KeyCodeCombination, KeyEvent}
import scalafx.scene.Scene
import scalafx.stage.Stage
object Main extends JFXApp {
val ctlA = new KeyCodeCombination(KeyCode.A, KeyCombination.ControlDown)
stage = new PrimaryStage {
scene = new Scene {
onKeyPressed = { ke =>
if (ctlA.`match`(ke))
println("Matches ^A")
else
println("No match")
}
}
}
}你的程序现在运行得很好!
https://stackoverflow.com/questions/50914767
复制相似问题