用jfinal enjoy做了个PC端简易计算器

观看演示

https://ask.qcloudimg.com/draft/6167008/73y1k5cjic.gif

import com.jfinal.kit.Kv
import com.jfinal.template.Engine
import javafx.scene.layout.GridPane
import tornadofx.*
import kotlin.Exceptionimport 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        }
    }}


评论区

JFinal

2019-10-01 14:52

很有创意的用法,超赞

jfinal4cyy

2019-10-01 15:52

下载体验:https://gitee.com/y2h/USEFUL/blob/master/soft/%E8%AE%A1%E7%AE%97%E5%99%A8.zip

简单代码

2019-10-04 22:48

都说kotlin好,可我看着还是Java的语法亲切、自然啊

elber25977

2019-10-07 18:02

学习了,谢谢分享

热门分享

扫码入社