Scala 我可以不使用“创建GUI”吗;扩展;
我开始学习Scala,我感到困惑。我可以在没有“扩展SimpleSwingApplication”或“SimpleGUIApplication”的情况下创建GUI吗?我尝试这样做:Scala 我可以不使用“创建GUI”吗;扩展;,scala,scala-swing,Scala,Scala Swing,我开始学习Scala,我感到困惑。我可以在没有“扩展SimpleSwingApplication”或“SimpleGUIApplication”的情况下创建GUI吗?我尝试这样做: import scala.swing._ object Main { def main(args:Array[String]): Unit = { val frame = new Frame {title = "test GUI"} val button = new Button {text =
import scala.swing._
object Main {
def main(args:Array[String]): Unit = {
val frame = new Frame {title = "test GUI"}
val button = new Button {text = "test button"}
val uslPanel = new BoxPanel(Orientation.Vertical) {
contents += button
}
//listenTo(button)
frame.contents_=(uslPanel)
frame.visible_=(true)
}
}
它可以工作,但只要对“listenTo(botton)”进行注释。如何使用“listenTo(…)”而不使用“extends SimpleGui…etc”。swing应用程序特性提供了两件事。(1) 它们将初始代码延迟到事件分派线程(参见)。(2) 它们继承了
Reactor
特性,这为您提供了您现在缺少的listenTo
方法
我认为你应该把SwingApplication
application特性混在一起,这是最简单的。否则,您可以手动执行以下操作:
import scala.swing._
object Main {
def main(args: Array[String]) {
Swing.onEDT(initGUI) // Scala equivalent of Java's SwingUtilities.invokeLater
}
def initGUI() {
val frame = new Frame { title = "test GUI" }
val button = new Button { text = "test button" }
val uslPanel = new BoxPanel(Orientation.Vertical) {
contents += button
}
val r = new Reactor {}
r.listenTo(button)
r.reactions += {
case event.ButtonClicked(_) => println("Clicked")
}
frame.contents = uslPanel
frame.visible = true // or use `frame.open()`
}
}
请注意,Scala Swing中的每个小部件都继承了Reactor
,因此您经常会发现这种样式:
val button = new Button {
text = "test button"
listenTo(this) // `listenTo` is defined on Button because Button is a Reactor
reactions += { // `reactions` as well
case event.ButtonClicked(_) => println("Clicked")
}
}