Java Reference
In-Depth Information
Listing 11-2.
The Nashorn Script for the HelloFX Application Stored in hellofx.js
// hellofx.js
var msg;
var sp;
function init() {
msg = new javafx.scene.control.Label("Hello JavaFX from Nashorn!");
msg.font = javafx.scene.text.Font.font("Helvetica", 18);
sp = new javafx.scene.layout.StackPane(msg);
}
function start(stage) {
stage.setTitle("Hello FX");
stage.setScene(new javafx.scene.Scene(sp, 300, 100));
stage.sizeToScene();
stage.show();
}
function stop() {
java.lang.System.out.println("Hello FX application is stopped.");
}
You are not required to have any of the
init()
,
start()
, and
stop()
functions in the
script. Listing 11-3 contains another version of the
HelloFX
application. The
init()
and
stop()
functions are not included. The code from the
init()
function has been moved to
global scope. The
stop()
method has been removed, so you will not see a message on the
standard output when the application is exited. Nashorn will execute the code in the global
scope first, and then call the
start()
method. The script is stored in the
hellofx2.js
file.
Running it displays the same window as shown in Figure
11-1
. You can run the script:
jjs -fx hellofx2.js
Listing 11-3.
Another Version of the HelloFX Application without init() and stop()
Methods
// hellofx2.js
var msg = new javafx.scene.control.Label("Hello JavaFX from Nashorn!");
msg.font = javafx.scene.text.Font.font("Helvetica", 18);
var sp = new javafx.scene.layout.StackPane(msg);
function start(stage) {
stage.setTitle("Hello FX");
stage.setScene(new javafx.scene.Scene(sp, 300, 100));
stage.sizeToScene();
stage.show();
}
Search WWH ::
Custom Search