简体   繁体   English

是JavaFX类型的ScalaFX窗格的子代吗?

[英]Are the children of a ScalaFX Pane of JavaFX type?

I am quite new to Scala and ScalaFX, so I probably do not understand this correctly. 我对Scala和ScalaFX非常陌生,因此我可能无法正确理解。 My issue: I add a node (fe Button) to a ScalaFX Pane (fe VBox). 我的问题:我向ScalaFX窗格(fe VBox)中添加了一个节点(fe Button)。 When I access the children of the Pane the children type changed from ScalaFX Button to JavaFX Button. 当我访问窗格的子级时,子级类型从ScalaFX Button更改为JavaFX Button。

When I have a look at the source code of ScalaFX I can see that everything is done with delegates. 当我查看ScalaFX的源代码时,我可以看到一切都由委托完成。 So is the ScalaFX node lost? 那么ScalaFX节点丢失了吗? I think there is done some magic (for me as a newbie) with implicit conversion from ScalaFX to JavaFX. 我认为通过从ScalaFX到JavaFX的隐式转换,已经做了一些魔术(对我而言,是一个新手)。 Is it possible to do an implicit conversion the other way round? 是否可以反过来进行隐式转换? Do I use ScalaFX correctly? 我可以正确使用ScalaFX吗?

I wanted to go through all children of a Pane. 我想浏览窗格的所有子项。 When a child is from special type I wanted to perform some operations with this child. 当一个孩子来自特殊类型时,我想对该孩子执行一些操作。 Now I would have to check for JavaFX type and work on a JavaFX node, which does not appears like clean code to me. 现在,我必须检查JavaFX类型并在JavaFX节点上工作,这对我而言似乎不是干净的代码。

Any commments welcome. 任何感激欢迎。 Thanks in advance. 提前致谢。

I attached a running example. 我附上了一个正在运行的例子。

import scalafx.Includes._
import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.scene.Scene
import scalafx.scene.control.Button
import scalafx.scene.layout.VBox
import scalafx.stage.WindowEvent

object TestChildren extends JFXApp {
  stage = new PrimaryStage {
    val contentPane = new VBox()
    val b1 = new Button("B1")
    contentPane.children.add(b1)
    scene = new Scene (contentPane)

    println(b1.getClass)
    println("--")
    for (child <- contentPane.children) {
      println(child.getClass)
    }
  }
}

Yes, there is an implicit conversion. 是的,有一个隐式转换。 I do not know which IDE you are using, but for example IntelliJ 14.1 with the Scala plugin indicates implicit conversions, which is a really useful feature. 我不知道您使用的是哪个IDE,但是例如带有Scala插件的IntelliJ 14.1表示隐式转换,这是一个非常有用的功能。

In your case, 就你而言

contentPane.children.add(b1)

is the line where the implicit conversion happens. 是发生隐式转换的行。 The explicit form is 显式形式为

contentPane.children.add(Button.sfxButton2jfx(b1))

where the implicit conversion is a one-liner in scalafx.scene.control.Button : 其中scalafx.scene.control.Button中的隐式转换是scalafx.scene.control.Button

implicit def sfxButton2jfx(v: Button): jfxsc.Button = if (v != null) v.delegate else null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM