Dear Friends,
It’s my 3rd day in Kotlin study. I need to create a GUI with Java Fx and Kotlin. I used below code in intellij IDEA Community edition.
import javafx.application.Application
import javafx.geometry.Insets
import javafx.scene.Scene
import javafx.scene.control.Button
import javafx.scene.control.Label
import javafx.scene.control.TextField
import javafx.scene.layout.VBox
import javafx.stage.Stage
class CalculatorApp : Application() {
override fun start(primaryStage: Stage) {
val firstNumberField = TextField()
val secondNumberField = TextField()
val calculateButton = Button("Calculate")
val resultLabel = Label()
calculateButton.setOnAction {
val firstNumber = firstNumberField.text.toDoubleOrNull() ?: 0.0
val secondNumber = secondNumberField.text.toDoubleOrNull() ?: 0.0
val sum = firstNumber + secondNumber
resultLabel.text = "The sum of $firstNumber and $secondNumber is $sum"
}
val vbox = VBox(10.0)
vbox.padding = Insets(20.0)
vbox.children.addAll(
Label("Enter the first number:"),
firstNumberField,
Label("Enter the second number:"),
secondNumberField,
calculateButton,
resultLabel
)
val scene = Scene(vbox, 300.0, 250.0)
primaryStage.title = "Calculator App"
primaryStage.scene = scene
primaryStage.show()
}
}
fun main() {
Application.launch(CalculatorApp::class.java)
}
But I got a number of errors (25 nos) as
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt
Kotlin: Unresolved reference: javafx
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:2:8
Kotlin: Unresolved reference: javafx
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:3:8
Kotlin: Unresolved reference: javafx
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:4:8
Kotlin: Unresolved reference: javafx
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:5:8
Kotlin: Unresolved reference: javafx
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:6:8
Kotlin: Unresolved reference: javafx
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:7:8
Kotlin: Unresolved reference: javafx
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:8:8
Kotlin: Unresolved reference: javafx
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:10:23
Kotlin: Unresolved reference: Application
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:11:5
Kotlin: ‘start’ overrides nothing
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:11:38
Kotlin: Unresolved reference: Stage
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:12:32
Kotlin: Unresolved reference: TextField
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:13:33
Kotlin: Unresolved reference: TextField
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:14:31
Kotlin: Unresolved reference: Button
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:15:27
Kotlin: Unresolved reference: Label
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:21:25
Kotlin: Variable expected
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:24:20
Kotlin: Unresolved reference: VBox
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:25:14
Kotlin: Variable expected
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:25:24
Kotlin: Unresolved reference: Insets
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:27:13
Kotlin: Unresolved reference: Label
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:29:13
Kotlin: Unresolved reference: Label
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:35:21
Kotlin: Unresolved reference: Scene
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:37:22
Kotlin: Variable expected
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:38:22
Kotlin: Variable expected
D:\Kotlinplayintellig\HelloWorld\src\main\kotlin\Seventhone.kt:44:5
Kotlin: Unresolved reference: Application
This occur after I download JavaFx and include in Intellij IDEA community Edition .
I followed upto 3 steps (3 not included) in solution to add JavaFX …Link
Please help with a solution
Thanks
Anes P A