使用javafx框架tornadofx制作诗经查看工具

2019-12-02 17:41:06 浏览数 (1)

在build.gradle中添加2个依赖:

代码语言:javascript复制
// 用于将汉字转换拼音
implementation("com.rnkrsoft.bopomofo4j:bopomofo4j:1.0.0")
// 用于将json字符串转换成java对象
implementation("xyz.calvinwilliams:okjson:0.0.10.0")

为了让各位都动起手来一起学习,以后的博客中贴一半代码,另一半代码以图片形式显示。用到的json数据

代码语言:javascript复制
// 模拟json数据格式及okjson的要求定义数据模型
class Shici0{
    var title:String=""
    var chapter:String=""
    var section:String=""
    var content:LinkedList<String>? = null
}

class Shici {
    var shici: LinkedList<Shici0>? = null
}

GUI:

代码语言:javascript复制
override val root = borderpane {
        setPrefSize(900.0, 600.0)
        top = vbox(10) {
            hbox(4) {
                hyperlink("https://gitee.com/calvinwilliams/okjson")
                hyperlink("https://gitee.com/mirrors/chinese-poetry")
            }
            hbox(4) {
                button("Load json file") {
                    action {
                        okjsonGo()
                    }
                }

                textfield(titleFilterField) { promptText = "根据名称过滤" }
                combobox(chapterFilterCombo, chapter)

                combobox(sectionFilterCombo, section)

                button("Clear Filters") {
                    action {
                        // 清空过渡条件
                        chapterFilterCombo.value = null
                        sectionFilterCombo.value = null
                        titleFilterField.value = ""
                    }
                }
//                text("右击鼠标可以将诗词内容转换成拼音")
            }
            paddingBottom = 10.0
        }
        center = hbox(4) {
            listview(filteredItems) {
                cellFormat {
                    text = "${it.title} - ${it.chapter} - ${it.section}"
                }
                onUserSelect(1) {
                    outStr.value = it.content?.joinToString("tntn")
                }
                minWidth = 200.0
            }
            textarea(outStr) {
                promptText = "结果"
                contextmenu {
                    item("所有拼音").action {
                        hz2py(text, ydType = ydType.value)
                    }
                    item("选中拼音").action {
                        hz2py(selectedText, ydType.value)
                    }
                }
            }
            textarea(pingyin)
        }

        bottom = hbox {
            label(msg)
        }

        paddingAll = 10.0
        style {
            fontSize = 16.px
        }
    }

代码语言:javascript复制
  private fun hz2py(hzstr: String, ydType: Int = 0) {
        runAsync {
            Bopomofo4j.local()
            pingyin.value = Bopomofo4j.pinyin(hzstr, ydType, false, false, " ")
        }
    }

    private var shici: Shici? = Shici()

    private fun okjsonGo() {
        shici = OKJSON.stringToObject(jsonStr, Shici::class.java, OKJSON.OPTIONS_DIRECT_ACCESS_PROPERTY_ENABLE)
        if (shici == null) {
            msg.value = "OKJSON.stringToObject failed["   OKJSON.getErrorCode()   "]["   OKJSON.getErrorDesc()   "]"
        } else {
            msg.value = "右击鼠标可以将诗词内容转换成拼音, OKJSON.stringToObject ok "
            shicis.addAll(shici?.shici!!.toTypedArray())
            shici?.shici?.let {
                it.forEach {
                    if (section.contains(it.section).not())
                        section.addAll(it.section)

                    if (chapter.contains(it.chapter).not())
                        chapter.addAll(it.chapter)
                }
            }
//            section.addAll(shici?.shici.toTypedArray())
//            chapter.addAll(shici?.chapter!!.toTypedArray())
        }
    }
}

0 人点赞