【问题标题】:Guide from TornadoFX: how to implement Customer classTornadoFX 指南:如何实现 Customer 类
【发布时间】:2018-11-26 10:32:38
【问题描述】:

我从这里开始关注 TornadoFX 指南,尝试运行示例向导: Wizard

并且已经实现了如下的附加类 Customer,它没有运行:

package com.example.demo.app

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

class Customer(name: String, zip: Int, city: String, type: String) {
    val zipProperty = SimpleIntegerProperty(zip)
    var zip by zipProperty

    val nameProperty = SimpleStringProperty(name)
    var name by nameProperty

    val cityProperty = SimpleStringProperty(city)
    var city by cityProperty

    val typeProperty = SimpleStringProperty(type)
    var type by typeProperty

}

我如何添加Customer.Type 如此处引用,这些类取自指南:

package com.example.demo.view

import com.example.demo.app.Customer
import com.example.demo.app.CustomerModel
import tornadofx.*
class CustomerWizard : Wizard() {
    val customer: CustomerModel by inject()

    override val canGoNext = currentPageComplete
    override val canFinish = allPagesComplete

    init {
        add(BasicData::class)
        add(AddressInput::class)
    }
}

class BasicData : View("Basic Data") {
    val customer: CustomerModel by inject()

    override val complete = customer.valid(customer.name)

    override val root = form {
        fieldset(title) {
            field("Type") {
                combobox(customer.type, Customer.Type.values().toList()) //Customer.Type, what is it?
            }
            field("Name") {
                textfield(customer.name).required()
            }
        }
    }
}

class AddressInput : View("Address") {
    val customer: CustomerModel by inject()

    override val complete = customer.valid(customer.zip, customer.city)

    override val root = form {
        fieldset(title) {
            field("Zip/City") {
                textfield(customer.zip) {
                    prefColumnCount = 5
                    required()
                }
                textfield(customer.city).required()
            }
        }
    }
}

错误如下,让我想知道Type是什么?枚举,类,...? Error:(26, 50) Kotlin: Unresolved reference: Type

【问题讨论】:

    标签: kotlin tornadofx


    【解决方案1】:

    在上面的例子中,Type 是一个枚举,定义在Customer 类中,例如这样:

    class Customer(name: String, zip: Int, city: String, type: Customer.Type) {
        enum class Type {
            Private, Company
        }
    
        val zipProperty = SimpleIntegerProperty(zip)
        var zip by zipProperty
    
        val nameProperty = SimpleStringProperty(name)
        var name by nameProperty
    
        val cityProperty = SimpleStringProperty(city)
        var city by cityProperty
    
        val typeProperty = SimpleObjectProperty<Type>(type)
        var type by typeProperty
    
    }
    

    请注意,typeProperty 也已更改为 SimpleObjectProperty&lt;Type&gt;

    【讨论】:

    • ty @edvin syse。但是为什么向导的取消按钮不拒绝最新的更改呢?这是一个错误吗?
    • 您的代码中可能存在错误,但您尚未发布向导代码,因此我无法确定。您必须记住,编辑的输入值仍将在 ViewModel 中更改,但在执行 viewmodel.commit() 之前不会被写回域对象。您还可以执行 viewmodel.rollback() 以显式回滚到域对象中的值。
    • 我现在回答了 :)
    猜你喜欢
    • 2023-03-26
    • 2017-11-05
    • 2021-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 2011-12-30
    • 1970-01-01
    相关资源
    最近更新 更多