all the previous technologies.
There are many books written on JavaFX and we aren't going to be exhaustive in our coverage of
the technology. Rather we will make notes on the core capabilities with illustrative examples where
necessary.
The Hello World app
To create a trivial JavaFX application, we can follow the instructions in this recipe to get us up and
running.
1. Create a Java project
2. Add jfxrt.jar to the project classpath. This JAR can be found in the lib folder of a
modern Java JRE.
3. Create a Java class that implements main
4. Modify the class to contain:
public class YourClassName extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("<YourFXMLFile>.fxml"));
Scene scene = new Scene(root, 600, 400);
stage.setScene(scene);
stage.show();
}
}
5. Using Scene Builder, create an FXML file and save it in the same folder as your Java class.
The JavaFX High Level Architecture
Let us start with the model. In JavaFX we are in the business of building Applications and JavaFX
provides a class called Application which we must extend. This allows us to implement a
method called "start(Stage)" which is called when the application is ready to start. Think of
"start()" as the entry point into the application.
If the "start()" is the callback to say that the application has started, how is the application launched
in the first place?
We are familiar with the special method called "static void main(String args[])" that
can be an entry point into a Java application. Application contains a static method called
"launch(String [])" that can be used to launch it. As such, the simplest JavaFX application
will look as follows:
class MyApp extends Application {
public void start(Stage stage) {
}
public static void main(String args[]) {
launch(args);
}
}
We can then do work in the "start()" method to bring up the rest of our environment.
This now takes us to the next topic of consideration, namely the "Stage".
Page 254
Kommentare zu diesen Handbüchern