Java Reference
In-Depth Information
Stage
Scene
Button
(a)
(b)
F
IGURE
14.2
(a) Stage is a window for displaying a scene that contains nodes. (b) Multiple
stages can be displayed in a JavaFX program.
L
ISTING
14.2
MultipleStageDemo.java
1
import
javafx.application.Application;
2
import
javafx.scene.Scene;
3
import
javafx.scene.control.Button;
4
import
javafx.stage.Stage;
5
6
public class
MultipleStageDemo
extends
Application {
7 @Override
// Override the start method in the Application class
8
public void
start(Stage primaryStage) {
9
// Create a scene and place a button in the scene
10 Scene scene =
new
Scene(
new
Button(
"
OK
"
),
200
,
250
);
11 primaryStage.setTitle(
"
MyJavaFX
"
);
// Set the stage title
12 primaryStage.setScene(scene);
// Place the scene in the stage
13
primary stage in start
primaryStage.show();
// Display the stage
display primary stage
14
15 Stage stage =
new
Stage();
// Create a new stage
16 stage.setTitle(
"
Second Stage
"
);
// Set the stage title
17
// Set a scene with a button in the stage
18 stage.setScene(
new
Scene(
new
Button(
"
New Stage
"
),
100
,
100
));
19
create second stage
display second stage
stage.show();
// Display the stage
20 }
21 }
main method omitted
Note that the main method is omitted in the listing since it is identical for every JavaFX
application. From now on, we will not list the
main
method in our JavaFX source code for
brevity.
By default, the user can resize the stage. To prevent the user from resizing the stage, invoke
stage.setResizable(false)
.
main method omitted
prevent stage resizing
14.3
✓
✓
How do you define a JavaFX main class? What is the signature of the
start
method?
What is a stage? What is a primary stage? Is a primary stage automatically created?
How do you display a stage? Can you prevent the user from resizing the stage? Can you
replace
Application.launch(args)
by
launch(args)
in line 22 in Listing 14.1?
Check
Point
14.4
Show the output of the following JavaFX program.
import
javafx.application.Application;
import
javafx.stage.Stage;
public class
Test
extends
Application {
public
Test() {
System.out.println(
"
Test constructor is invoked
"
);
}
Search WWH ::
Custom Search