再来做个小工具,简易计算器,用到了jfinal enjoy,140行代码不到

要搭建好JDK8的环境,才能运行

import com.jfinal.kit.Kv
import com.jfinal.template.Engine
import javafx.scene.layout.GridPane
import tornadofx.*
import kotlin.Exception
import kotlin.math.pow
import kotlin.math.sqrt

class EnjoyApp : App(EnjoyView::class, MyStyle::class)

class EnjoyView : View("计算器") {
    val engine = Engine.use().setEncoding("utf-8")
            .setDevMode(true)
    private val kv = Kv.create()

    val strOut = stringProperty()
    val history = stringProperty("")
    val strIn = stringProperty("")
    var gp: GridPane by singleAssign()
    override val root = borderpane {
        paddingAll = 10.0
        top = vbox(5) {
            hbox {
                label("式子:")
                textfield(strIn) {
                    text = "1+1*8-789/654"
                }
            }
            hbox {
                label("结果:")
                textfield(strOut) {
                    isEditable = false
                }
            }
            paddingBottom = 10.0
        }
        center = hbox(10) {

            gp = gridpane {
                hgap = 2.0
                vgap = 2.0
            }
            textarea(history) { }
        }
    }

    init {
        val nums = listOf("%", "√", "x²", "1/x", ")", "C", "Del", "÷", "7", "8", "9", "*", "4", "5", "6", "-", "1", "2", "3", "+", "(", "0", ".", "=")

        var i = 0
        (0..5).forEach { r ->
            (0..3).forEach { c ->
                gp.add(button("${nums[i]}") {
                    setPrefSize(65.0, 65.0)
                    action {
                        when (text) {
                            "=" -> {
                                strOut.value = renderStr(strIn.value, kv)
                                addHistory(text)
                            }
                            "Del" -> strIn.value = strIn.value.dropLast(1)
                            "C" -> clear()
                            "÷" -> strIn.value = strIn.value.plus("/")
                            "√" -> {
                                strOut.value = sqrt(renderStr(strIn.value, kv).toDouble()).toString()
                                addHistory(text)
                            }
                            "x²" -> {
                                strOut.value = (renderStr(strIn.value, kv).toDouble().pow(2)).toString()
                                addHistory(text)
                            }
                            "1/x" -> {
                                strOut.value = (renderStr(strIn.value, kv).toDouble().pow(-1)).toString()
                                addHistory(text)
                            }
                            else -> strIn.value = strIn.value.plus(text)
                        }
                    }
                }, c, r)
                i++
            }
        }
    }

    private fun addHistory(operator:String) {
        when(operator){
            "="->{
                if (!strOut.value.isNullOrEmpty())
                    history.value += strIn.value.plus("=").plus(strOut.value).plus("\n\n")
            }
            "1/x"->{
                if (!strOut.value.isNullOrEmpty())
                    history.value +="1/(" +strIn.value.plus(")=").plus(strOut.value).plus("\n\n")
            }
            "x²"->{
                if (!strOut.value.isNullOrEmpty())
                    history.value += "(" +strIn.value.plus(")²=").plus(strOut.value).plus("\n\n")
            }
            "√"->{
                if (!strOut.value.isNullOrEmpty())
                    history.value +="sqrt(" + strIn.value.plus(")=").plus(strOut.value).plus("\n\n")
            }
        }

        clear()
    }

    private fun clear() {
        strOut.value = ""
        strIn.value = ""
    }

    private fun renderStr(inStr: String, kv: Kv): String {
        var result = ""

        if (inStr.isNotEmpty()) {
            try {
                result = engine.getTemplateByString("#($inStr)").renderToString(kv)

            } catch (e: Exception) {
                information("请输入正确的计算式子")
            }

        } else {
            result = ""
        }
        return result
    }
}

class MyStyle : Stylesheet() {
    init {
        s(label, textField, button) {
            fontSize = 20.px
        }
    }
}

原创声明,本文系作者授权云+社区发表,未经许可,不得转载。

如有侵权,请联系 yunjia_community@tencent.com 删除。

编辑于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券