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

2019-09-30 18:09:04 浏览数 (1)

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

代码语言:txt复制
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("nn")
            }
            "1/x"->{
                if (!strOut.value.isNullOrEmpty())
                    history.value  ="1/("  strIn.value.plus(")=").plus(strOut.value).plus("nn")
            }
            "x²"->{
                if (!strOut.value.isNullOrEmpty())
                    history.value  = "("  strIn.value.plus(")²=").plus(strOut.value).plus("nn")
            }
            "√"->{
                if (!strOut.value.isNullOrEmpty())
                    history.value  ="sqrt("   strIn.value.plus(")=").plus(strOut.value).plus("nn")
            }
        }

        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
        }
    }
}

0 人点赞