从Java到Kotlin
我正在尝试将一个用java编写的简单JavaFXCalc翻译成Kotlin代码。->java源代码 我在Kotlin翻译了这个:从Java到Kotlin,java,javafx,kotlin,Java,Javafx,Kotlin,我正在尝试将一个用java编写的简单JavaFXCalc翻译成Kotlin代码。->java源代码 我在Kotlin翻译了这个: import Model.Math import javafx.application.Application import javafx.beans.binding.Bindings import javafx.beans.property.* import javafx.event.ActionEvent import javafx.event.EventHand
import Model.Math
import javafx.application.Application
import javafx.beans.binding.Bindings
import javafx.beans.property.*
import javafx.event.ActionEvent
import javafx.event.EventHandler
import javafx.geometry.Pos
import javafx.scene.Scene
import javafx.scene.control.*
import javafx.scene.input.KeyEvent
import javafx.scene.layout.*
import javafx.stage.Stage
import javafx.stage.StageStyle
import java.util.HashMap
// A simple Kotlin, JavaFX application
class KotlinCode : Application() {
private val accelerators = HashMap<String, Button>()
private val stackValue = SimpleDoubleProperty()
private val value = SimpleDoubleProperty()
enum class Op {
NOOP, ADD, SUBTRACT, MULTIPLY, DIVIDE
}
private var curOp = Op.NOOP
private var stackOp = Op.NOOP
private val template = arrayOf(arrayOf("7", "8", "9", "/"), arrayOf("4", "5", "6", "*"), arrayOf("1", "2", "3", "-"), arrayOf("0", "c", "=", "+"))
override fun start(stage: Stage) {
val screen = createScreen()
val buttons = createButtons()
stage.title = "Calc"
stage.initStyle(StageStyle.UTILITY)
stage.isResizable = false
stage.scene = Scene(createLayout(screen, buttons))
stage.show()
}
private fun createLayout(screen: TextField, buttons: TilePane): VBox {
val layout = VBox(20.0)
layout.alignment = Pos.CENTER
layout.style = "-fx-background-color: chocolate; -fx-padding: 20; -fx-font-size: 20;"
layout.children.setAll(screen, buttons)
handleAccelerators(layout)
screen.prefWidthProperty().bind(buttons.widthProperty())
return layout
}
private fun handleAccelerators(layout: VBox) {
layout.addEventFilter(KeyEvent.KEY_PRESSED) { keyEvent ->
val activated = accelerators[keyEvent.text]
activated?.fire()
}
}
private fun createScreen(): TextField {
val screen = TextField()
screen.style = "-fx-background-color: aquamarine;"
screen.alignment = Pos.CENTER_RIGHT
screen.isEditable = false
screen.textProperty().bind(Bindings.format("%.0f", value))
return screen
}
private fun createButtons(): TilePane {
val buttons = TilePane()
buttons.vgap = 7.0
buttons.hgap = 7.0
buttons.prefColumns = template[0].size
for (r in template) {
for (s in r) {
buttons.children.add(createButton(s))
}
}
return buttons
}
private fun createButton(s: String): Button {
val button = makeStandardButton(s)
if (s.matches("[0-9]".toRegex())) {
makeNumericButton(s, button)
//this.javaCode.makeNumericButton(s, button, curOp, value, stackValue, stackOp)
} else {
val triggerOp = determineOperand(s)
if (triggerOp.get() != Op.NOOP) {
//this.javaCode.makeOperandButton(button, triggerOp, this.curOp)
makeOperandButton(button, triggerOp)
} else if ("c" == s) {
//this.javaCode.makeClearButton(button, value)
makeClearButton(button)
} else if ("=" == s) {
makeEqualsButton(button)
}
}
return button
}
private fun determineOperand(s: String): ObjectProperty<Op> {
val triggerOp = SimpleObjectProperty(Op.NOOP)
when (s) {
"+" -> triggerOp.set(Op.ADD)
"-" -> triggerOp.set(Op.SUBTRACT)
"*" -> triggerOp.set(Op.MULTIPLY)
"/" -> triggerOp.set(Op.DIVIDE)
}
return triggerOp
}
private fun makeStandardButton(s: String): Button {
val button = Button(s)
button.style = "-fx-base: beige;"
accelerators.put(s, button)
button.setMaxSize(java.lang.Double.MAX_VALUE, java.lang.Double.MAX_VALUE)
return button
}
private fun makeEqualsButton(button: Button) {
button.style = "-fx-base: ghostwhite;"
button.setOnAction { actionEvent ->
// On instancie notre objet Kotlin
val math = Math()
when (stackOp) {
Op.NOOP -> {
}
Op.ADD -> value.set(math.adition(stackValue.get(), value.get()))
Op.SUBTRACT -> value.set(math.substraction(stackValue.get(), value.get()))
Op.MULTIPLY -> value.set(math.multiplication(stackValue.get(), value.get()))
Op.DIVIDE -> value.set(math.division(stackValue.get(), value.get()))
}
}
}
private fun makeOperandButton(button: Button, triggerOp: ObjectProperty<Op>) {
button.onAction = EventHandler {
fun handle(actionEvent: ActionEvent) {
curOp = triggerOp.get()
}
}
}
private fun makeNumericButton(s: String, button: Button) {
button.onAction = EventHandler {
fun handle(actionEvent: ActionEvent) {
if (curOp == Op.NOOP) {
value.set(value.get() * 10 + Integer.parseInt(s))
} else {
stackValue.set(value.get())
value.set(java.lang.Double.parseDouble(s))
stackOp = curOp
curOp = Op.NOOP
}
}
}
}
private fun makeClearButton(button: Button) {
button.onAction = EventHandler {
fun handle(actionEvent: ActionEvent) {
value.set(0.0)
}
}
}
companion object {
@JvmStatic
fun main(args: Array<String>) {
launch(KotlinCode::class.java)
}
}
}
import Model.Math
导入javafx.application.application
导入javafx.beans.binding.Bindings
导入javafx.beans.property*
导入javafx.event.ActionEvent
导入javafx.event.EventHandler
导入javafx.geometry.Pos
导入javafx.scene.scene
导入javafx.scene.control*
导入javafx.scene.input.KeyEvent
导入javafx.scene.layout*
导入javafx.stage.stage
导入javafx.stage.StageStyle
导入java.util.HashMap
//一个简单的Kotlin、JavaFX应用程序
类KotlinCode:Application(){
私有val加速器=HashMap()
private val stackValue=SimpleDoubleProperty()
private val value=SimpleDoubleProperty()
枚举类Op{
NOOP,加,减,乘,除
}
私有变量curOp=Op.NOOP
私有变量stackOp=Op.NOOP
private val template=arrayOf(arrayOf(“7”、“8”、“9”和“/”),arrayOf(“4”、“5”、“6”和“*”),arrayOf(“1”、“2”、“3”和“-”),arrayOf(“0”、“c”和“=”、“+”)
覆盖乐趣开始(阶段:阶段){
val screen=createScreen()
val buttons=createButtons()
stage.title=“计算”
stage.initStyle(StageStyle.UTILITY)
stage.isResizable=false
stage.scene=场景(创建布局(屏幕、按钮))
舞台秀
}
private fun createLayout(屏幕:文本字段,按钮:TilePane):VBox{
val布局=VBox(20.0)
布局对齐=位置中心
layout.style=“-fx背景色:巧克力色;-fx填充:20;-fx字体大小:20;”
布局.children.setAll(屏幕、按钮)
手动加速器(布局)
screen.prefWidthProperty().bind(buttons.widthProperty())
返回布局
}
私人娱乐手持加速器(布局:VBox){
layout.addEventFilter(KeyEvent.KEY_按下){KeyEvent->
val激活=加速器[keyEvent.text]
激活?.fire()
}
}
private fun createScreen():TextField{
val screen=TextField()
screen.style=“-fx背景色:海蓝宝石;”
屏幕对齐=位置居中\右
screen.isEditable=false
screen.textProperty().bind(Bindings.format(“%.0f”,value))
返回屏幕
}
private fun createButtons():TilePane{
val按钮=TilePane()
按钮。vgap=7.0
buttons.hgap=7.0
buttons.prefColumns=模板[0]。大小
用于(模板中的r){
对于(右中的s){
按钮。子项。添加(createButton)
}
}
返回按钮
}
private fun createButton(s:String):按钮{
val按钮=生成标准按钮
如果(s.matches(“[0-9]”)。toRegex()){
makeNumericButton(s,按钮)
//makeNumericButton(s,button,curOp,value,stackValue,stackOp)
}否则{
val triggerOp=确定词和
if(triggerOp.get()!=Op.NOOP){
//this.javaCode.makeOperationButton(按钮、触发器、this.curOp)
MakeOperationButton(按钮,触发器)
}否则,如果(“c”==s){
//this.javaCode.makeClearButton(按钮,值)
makeClearButton(按钮)
}否则,如果(“=”==s){
MakequalButton(按钮)
}
}
返回按钮
}
private-fun-determineOperand(s:String):ObjectProperty{
val triggerOp=SimpleObjectProperty(Op.NOOP)
何时{
“+”->triggerOp.set(Op.ADD)
“-”->triggerOp.set(Op.SUBTRACT)
“*”->triggerOp.set(Op.MULTIPLY)
“/”->triggerOp.set(Op.DIVIDE)
}
返回触发器
}
私人娱乐制作标准按钮(s:String):按钮{
val按钮=按钮
button.style=“-fx基底:米色;”
加速器。放置(s,按钮)
setMaxSize(java.lang.Double.MAX_值,java.lang.Double.MAX_值)
返回按钮
}
私人娱乐MakequalsButton(按钮:button){
button.style=“-fx base:ghostwite;”
button.setOnAction{actionEvent->
//关于实例notre objet Kotlin
val math=math()
何时(stackOp){
Op.NOOP->{
}
Op.ADD->value.set(math.adion(stackValue.get(),value.get())
Op.SUBTRACT->value.set(math.subtraction(stackValue.get(),value.get())
Op.MULTIPLY->value.set(math.MULTIPLY(stackValue.get(),value.get()))
Op.DIVIDE->value.set(math.DIVIDE(stackValue.get(),value.get()))
}
}
}
私有按钮(按钮:按钮,触发器:对象属性){
button.onAction=EventHandler{
趣味手柄(actionEvent:actionEvent){
curOp=triggerOp.get()
}
}
}
private fun makeNumericButton(s:String,button:button){
button.onAction=EventHandler{
趣味手柄(actionEvent:actionEvent){
如果(curOp==Op.NOOP){
value.set(value.get()*10+Integer.parseInt)
}否则{
stackValue.set(value.get())
value.set(java.lang.Double.parseDouble)
stackOp=curOp
curOp=Op.NOOP
}
}
}
}
私人娱乐makeClearButton(按钮:button){
button.onAction=EventHandler{
趣味手柄(actionEvent:actionEvent){
值。设置(0.0)
}
}
}
伴星{
@JvmStatic
趣味主线(args:Array){
启动(KotlinCode::class.java)
}
}
}
现在,calc启动,但没有绑定到按钮的操作。有人有线索吗
我猜是这样的
private fun makeOperandButton(button: Button, triggerOp: ObjectProperty<Op>) {
button.onAction = EventHandler {
curOp = triggerOp.get()
}
}
private fun makeNumericButton(s: String, button: Button) {
button.onAction = EventHandler {
if (curOp == Op.NOOP) {
value.set(value.get() * 10 + Integer.parseInt(s))
} else {
stackValue.set(value.get())
value.set(java.lang.Double.parseDouble(s))
stackOp = curOp
curOp = Op.NOOP
}
}
}
private fun makeClearButton(button: Button) {
button.onAction = EventHandler {
value.set(0.0)
}
}