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

观看演示

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

  1. import com.jfinal.kit.Kv
  2. import com.jfinal.template.Engine
  3. import javafx.scene.layout.GridPane
  4. import tornadofx.*
  5. import kotlin.Exceptionimport kotlin.math.pow
  6. import kotlin.math.sqrt
  7.  
  8. class EnjoyApp : App(EnjoyView::class, MyStyle::class)
  9. class EnjoyView : View("计算器") {
  10.     val engine = Engine.use().setEncoding("utf-8")
  11.             .setDevMode(true)
  12.     private val kv = Kv.create()
  13.  
  14.     val strOut = stringProperty()
  15.     val history = stringProperty("")
  16.     val strIn = stringProperty("")
  17.     var gp: GridPane by singleAssign()
  18.     override val root = borderpane {
  19.         paddingAll = 10.0
  20.         top = vbox(5) {
  21.             hbox {
  22.                 label("式子:")
  23.                 textfield(strIn) {
  24.                     text = "1+1*8-789/654"
  25.                 }
  26.             }
  27.             hbox {
  28.                 label("结果:")
  29.                 textfield(strOut) {
  30.                     isEditable = false
  31.                 }
  32.             }
  33.             paddingBottom = 10.0
  34.         }
  35.         center = hbox(10) {
  36.  
  37.             gp = gridpane {
  38.                 hgap = 2.0
  39.                 vgap = 2.0
  40.             }
  41.             textarea(history) { }
  42.         }
  43.     }
  44.  
  45.     init {
  46.         val nums = listOf("%", "√", "x²", "1/x", ")", "C", "Del", "÷", "7", "8", "9", "*", "4", "5", "6", "-", "1", "2", "3", "+", "(", "0", ".", "=")
  47.  
  48.         var i = 0
  49.         (0..5).forEach { r ->
  50.             (0..3).forEach { c ->
  51.                 gp.add(button("${nums[i]}") {
  52.                     setPrefSize(65.0, 65.0)
  53.                     action {
  54.                         when (text) {
  55.                             "=" -> {
  56.                                 strOut.value = renderStr(strIn.value, kv)
  57.                                 addHistory(text)
  58.                             }
  59.                             "Del" -> strIn.value = strIn.value.dropLast(1)
  60.                             "C" -> clear()
  61.                             "÷" -> strIn.value = strIn.value.plus("/")
  62.                             "√" -> {
  63.                                 strOut.value = sqrt(renderStr(strIn.value, kv).toDouble()).toString()
  64.                                 addHistory(text)
  65.                             }
  66.                             "x²" -> {
  67.                                 strOut.value = (renderStr(strIn.value, kv).toDouble().pow(2)).toString()
  68.                                 addHistory(text)
  69.                             }
  70.                             "1/x" -> {
  71.                                 strOut.value = (renderStr(strIn.value, kv).toDouble().pow(-1)).toString()
  72.                                 addHistory(text)
  73.                             }
  74.                             else -> strIn.value = strIn.value.plus(text)
  75.                         }
  76.                     }
  77.                 }, c, r)
  78.                 i++
  79.             }
  80.         }
  81.     }
  82.  
  83.     private fun addHistory(operator:String) {
  84.         when(operator){
  85.             "="->{
  86.                 if (!strOut.value.isNullOrEmpty())
  87.                     history.value += strIn.value.plus("=").plus(strOut.value).plus("\n\n")
  88.             }
  89.             "1/x"->{
  90.                 if (!strOut.value.isNullOrEmpty())
  91.                     history.value +="1/(" +strIn.value.plus(")=").plus(strOut.value).plus("\n\n")
  92.             }
  93.             "x²"->{
  94.                 if (!strOut.value.isNullOrEmpty())
  95.                     history.value += "(" +strIn.value.plus(")²=").plus(strOut.value).plus("\n\n")
  96.             }
  97.             "√"->{
  98.                 if (!strOut.value.isNullOrEmpty())
  99.                     history.value +="sqrt(" + strIn.value.plus(")=").plus(strOut.value).plus("\n\n")
  100.             }
  101.         }
  102.  
  103.         clear()
  104.     }
  105.  
  106.     private fun clear() {
  107.         strOut.value = ""
  108.         strIn.value = ""
  109.     }
  110.  
  111.     private fun renderStr(inStr: String, kv: Kv): String {
  112.         var result = ""
  113.  
  114.         if (inStr.isNotEmpty()) {
  115.             try {
  116.                 result = engine.getTemplateByString("#($inStr)").renderToString(kv)
  117.  
  118.             } catch (e: Exception) {
  119.                 information("请输入正确的计算式子")
  120.             }
  121.  
  122.         } else {
  123.             result = ""
  124.         }
  125.         return result    
  126.     }
  127. }
  128.  
  129. class MyStyle : Stylesheet() {
  130.     init {
  131.         s(label, textField, button) {
  132.             fontSize = 20.px        }
  133.     }}


评论区

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

学习了,谢谢分享

热门分享

扫码入社