Sch*_*ipp 8 swing scala button right-click
我目前正在尝试使用scala对扫雷进行编码,但我无法找到右键单击按钮的方法.
我已经在互联网上搜索了它的方法,但我肯定无法找到它.
如果有人能帮助我,我会非常感激:)
谢谢,
Schnipp
(注意:Scala对我来说是一种新语言,我不是Java用户,所以如果我的问题听起来很笨,我很抱歉)
编辑:
我试图找到(或实现)一个功能'ButtonClickedRight',可以听到右键单击按钮.
像这样
import scala.swing._
import scala._
import scala.swing.event._
object Right extends MainFrame with App {
title = ""
visible = true
val b = new button("")
listenTo(b)
reactions += {
case ButtonClicked(`b`) => *code*
case ButtonClickedRight(`b`) => *code*
}
}
Run Code Online (Sandbox Code Playgroud)
编辑2 -
我想知道用户是否点击了按钮"1".我遇到的问题是,当我点击标签而不是按钮时,此代码会打印"鼠标点击"+ e.point +"type"+ e.modifiers.
object App extends SimpleSwingApplication {
lazy val ui = new GridPanel(2,1) {
contents += new Button("1")
contents += new Label("2")
listenTo(mouse.clicks)
reactions += {
case e: MouseClicked =>
println("Mouse clicked at " + e.point+" type "+e.modifiers)
}
}
def top = new MainFrame {
contents = ui
visible = true
preferredSize = new Dimension(500,500)
}
}
Run Code Online (Sandbox Code Playgroud)
我认为你走在正确的道路上,根据我对 scala swings 的理解,我认为问题在于你没有正确附加监听器。对于其中一个,我会将按钮分配给一个值并仅在其上调用listenTo:
val button = new Button("1")
listenTo(button)
Run Code Online (Sandbox Code Playgroud)
然后,在反应中,我会编写模式检查,以防它来自按钮(如果您只调用传递listenTo按钮,则可能是多余的)并且它具有正确的按钮:
case ButtonClicked(b) if b == button && b.peer.getButton == MouseEvent.BUTTON_2 => ...
Run Code Online (Sandbox Code Playgroud)
因此,您在编辑中提供的代码将变为:
object App extends SimpleSwingApplication {
lazy val ui = new GridPanel(2,1) {
val button = new Button("1")
contents += button
contents += new Label("2")
listenTo(button)
reactions += {
case evt @ MouseClicked(b, pt, _, _, _) if b == button && evt.peer.getButton == java.awt.event.MouseEvent.BUTTON2 =>
println(s"Mouse clicked at (${pt.x}; ${pt.y}) - button: ${evt.peer.getButton}")
}
}
def top = new MainFrame {
contents = ui
visible = true
preferredSize = new Dimension(500,500)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
889 次 |
| 最近记录: |