A sample Scala/JavaFX application
As a brief note to self, here’s an example JavaFX application written using Scala 2:
package pizzastore
import javafx.application.Application
import javafx.event.ActionEvent
import javafx.event.EventHandler
import javafx.geometry.{Insets, Pos}
import javafx.scene.Scene
import javafx.scene.control.{Button, Label, Separator}
import javafx.scene.layout._
import javafx.scene.paint.Color
import javafx.scene.text.Font
import javafx.stage.Stage
object MainWindow {
def main(args: Array[String]) {
Application.launch(classOf[MainWindow], args: _*)
}
}
class MainWindow extends Application {
override def start(stage: Stage) {
val borderPane = new MainBorderPane
val scene = new Scene(borderPane, 600, 400)
scene.getStylesheets.add(getClass.getResource("pizza.css").toExternalForm)
stage.setScene(scene)
stage.setTitle("Al’s Pizza")
stage.show
}
}
class MainBorderPane extends BorderPane {
// others need to be able to get this to add an action to it
val placeOrderButton = new Button
val alsPizzaLabel = new Label("Al’s Pizza")
//label.setId("fancytext") //css approach
alsPizzaLabel.setFont(new Font("Arial", 50))
alsPizzaLabel.setTextFill(Color.TOMATO)
placeOrderButton.setText("Start An Order")
placeOrderButton.setOnAction((e: ActionEvent) => {
println("Hello World!")
})
placeOrderButton.setFont(new Font("Arial", 24))
// a spacer to push the visible elements up a little
val spacer = new Region
spacer.setPrefHeight(40)
val vbox = new VBox
vbox.setSpacing(40)
vbox.setAlignment(Pos.CENTER)
vbox.getChildren.addAll(alsPizzaLabel, placeOrderButton, spacer)
this.setCenter(vbox)
}
