Java Reference
In-Depth Information
Beans. As we discussed in Section 25.5.2, the
Application
subclass is the starting point
for a JavaFX app. The
main
method calls class
Application
's
static
launch
method (line
26) to begin executing the app. This method, in turn, causes the JavaFX runtime to create
an object of the
TipCalculator
class and calls its
start
method.
1
// Fig. 25.17: TipCalculator.java
2
// Main app class that loads and displays the Tip Calculator's GUI
3
import
javafx.application.Application;
4
import
javafx.fxml.FXMLLoader;
5
import
javafx.scene.Parent;
6
import
javafx.scene.Scene;
7
import
javafx.stage.Stage;
8
9
public class
TipCalculator
extends
Application
10
{
11
@Override
12
public void
start(Stage stage)
throws
Exception
13
{
14
Parent root =
FXMLLoader.load(getClass().getResource(
"TipCalculator.fxml"
));
15
16
17
Scene scene =
new
Scene(root);
// attach scene graph to scene
stage.setTitle(
"Tip Calculator"
);
// displayed in window's title bar
stage.setScene(scene);
// attach scene to stage
stage.show();
// display the stage
18
19
20
21
}
22
23
public static void
main(String[] args)
24
{
25
// create a TipCalculator object and call its start method
26
launch(args);
27
}
28
}
Fig. 25.17
|
Main app class that loads and displays the
Tip Calculator
's GUI.
Overridden
Application
Method
start
Method
start
(lines 11-21) creates the GUI, attaches it to a
Scene
and places it on the
Stage
that method
start
receives as an argument. Lines 14-15 use class
FXMLLoader
's
static
method
load
to create the GUI's scene graph. This method:
• t s a
Parent
(package
javafx.scene
) reference to the scene graph's root
node (the
GridPane
in this example).
• Creates an object of the controller class.
• Initializes the controller's instance variables for the components that are manip-
ulated programmatically.
• Registers the event handlers for any events specified in the FXML.
We discuss the initialization of the controller's instance variables and the registration of
the event handlers in Section 25.5.5.