How do I recognize mouse clicks in Scala?

回眸只為那壹抹淺笑 提交于 2019-12-10 20:08:45

问题


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.

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)
    }
  }
}

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!


回答1:


Maybe that way?

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
  }
}

BTW, SimpleGUIApplication is deprecated




回答2:


The MouseClicked event has an attribute clicks, which should be 2 if it was a double-click. Have a look at java.awt.event.MouseEvent for the original source if you're curious.



来源:https://stackoverflow.com/questions/6877117/how-do-i-recognize-mouse-clicks-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!