如何识别 Scala 中的鼠标点击? [英] How do I recognize mouse clicks in Scala?
问题描述
我正在编写一个小的 GUI 程序.除了我想识别鼠标双击之外,一切正常.但是,我根本无法识别鼠标点击(因此),尽管我可以点击按钮并从列表中选择代码.
I'm writing a small GUI program. Everything works except that I want to recognize mouse double-clicks. However, I can't recognize mouse clicks (as such) at all, though I can click buttons and select code from a list.
以下代码改编自 Ingo Maier 的The scala.swing 包":
The following code is adapted from Ingo Maier's "The scala.swing package":
import scala.swing._
import scala.swing.event._
object MouseTest extends SimpleGUIApplication {
def top = new MainFrame {
listenTo(this.mouse) // value mouse is not a member of scala.swing.MainFrame
reactions += {
case e: MouseClicked =>
println("Mouse clicked at " + e.point)
}
}
}
我尝试了多种变体:mouse vs. Mouse、SimpleSwingApplication、从 java.awt.event 导入 MouseEvent 等.错误信息很清楚——在 MainFrame 中没有鼠标值——那么,它在哪里?帮助!
I've tried multiple variations: mouse vs. Mouse, SimpleSwingApplication, importing MouseEvent from java.awt.event, etc. The error message is clear enough--no value mouse in MainFrame--so, where is it then? Help!
推荐答案
也许是这样?
object App extends SimpleSwingApplication {
lazy val ui = new Panel {
listenTo(mouse.clicks)
reactions += {
case e: MouseClicked =>
println("Mouse clicked at " + e.point)
}
}
def top = new MainFrame {
contents = ui
}
}
顺便说一句,SimpleGUIApplication
已弃用
这篇关于如何识别 Scala 中的鼠标点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!