Pay attention to the textOrigin
method.
package proscalafx.ch04.reversi.examples
import scalafx.Includes._
import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.geometry.VPos
import scalafx.scene.Scene
import scalafx.scene.text.{Font, FontWeight, Text}
object CenterUsingBind extends JFXApp {
val text = new Text("SCALA") {
// origin of the local coordinate system for the text
// x-axis is fixed at the left edige of the text
// y-asix is customizable
// textOrigin = VPos.Top
textOrigin = VPos.Center
font = Font.font(null, FontWeight.Bold, 18)
}
stage = new PrimaryStage {
scene = new Scene(400, 100) {
content = text
}
}
// if textOrigin = VPos.Top, then
// text.layoutX <== (stage.scene.width - text.prefWidth(-1)) / 2
// text.layoutY <== (stage.scene.height - text.prefHeight(-1)) / 2
// if textOrigin = VPos.Center, then
text.layoutX <== (stage.scene.width - text.prefWidth(-1)) / 2
text.layoutY <== stage.scene.height / 2
}