简体   繁体   English

尝试实施 TornadoFX“激励示例”

[英]Trying to implement the TornadoFX "Motivational example"

I'm trying to implement the motivational example from this page: https://docs.tornadofx.io/0_subsection/1_why_tornadofx我正在尝试从此页面实施激励示例: https://docs.tornadofx.io/0_subsection/1_why_tornadofx

For this I need a data class Person as defined here:为此,我需要此处定义的数据 class Person:

class Person(id: Int, name: String, birthday: LocalDate) {
    val idProperty = SimpleIntegerProperty(id)
    var id by idProperty

    val nameProperty = SimpleStringProperty(name)
    var name by nameProperty

    val birthdayProperty = SimpleObjectProperty(birthday)
    var birthday by birthdayProperty

    val age: Int get() = Period.between(birthday, LocalDate.now()).years
}

To do this it was neccessary to make the following imports:为此,必须进行以下导入:

import javafx.beans.property.SimpleIntegerProperty
import javafx.beans.property.SimpleObjectProperty
import javafx.beans.property.SimpleStringProperty
import java.time.LocalDate
import java.time.Period

However, if I try to run the example I get the following error:但是,如果我尝试运行该示例,则会出现以下错误:

Kotlin: Property delegate must have a 'getValue(Person, KProperty<*>)' method. None of the following functions is suitable: 
public open fun getValue(): Int! defined in javafx.beans.property.SimpleIntegerProperty

I can circumvent this by not using delegate types and setting the properties like this:我可以通过不使用委托类型并像这样设置属性来避免这种情况:

    val idProperty = SimpleIntegerProperty(id)
    var id: Int
        get() = idProperty.value
        set(value) { idProperty.value = value}

But that seems to defeat the point of using delegates in TornadoFX when this is their motivational example for using it.但这似乎打败了在 TornadoFX 中使用委托的意义,因为这是他们使用它的动机示例。

Here's what I found on delegate types: https://edvin.gitbooks.io/tornadofx-guide/content/part2/Property_Delegates.html这是我在委托类型上发现的: https://edvin.gitbooks.io/tornadofx-guide/content/part2/Property_Delegates.html

That doesn't help with getting the shorthand of var id by idProperty to work though.不过,这无助于var id by idProperty的简写。

Can somebody point me in the right direction here?有人可以在这里指出我正确的方向吗?

You need to also import the following:您还需要导入以下内容:

import tornadofx.getValue
import tornadofx.setValue

Those are extension operator functions defined for various types in JavaFX (eg, properties, observable values, etc.) so that those types can be used as delegates.这些是为 JavaFX 中的各种类型(例如,属性、可观察值等)定义的扩展运算符函数,因此这些类型可以用作委托。 But those function aren't defined in those types, thus the need for the additional imports.但是那些 function 没有这些类型中定义,因此需要额外的导入。

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

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