Java Reference
In-Depth Information
Listing 14.12 gives a program that demonstrates
HBox
and
VBox
. The program places two
buttons in an
HBox
and five labels in a
VBox
, as shown in Figure 14.24.
L
ISTING
14.13
ShowHBoxVBox.java
1
import
javafx.application.Application;
2
import
javafx.geometry.Insets;
3
import
javafx.scene.Scene;
4
import
javafx.scene.control.Button;
5
import
javafx.scene.control.Label;
6
import
javafx.scene.layout.BorderPane;
7
import
javafx.scene.layout.HBox;
8
import
javafx.scene.layout.VBox;
9
import
javafx.stage.Stage;
10
import
javafx.scene.image.Image;
11
import
javafx.scene.image.ImageView;
12
13
public class
ShowHBoxVBox
extends
Application {
14 @Override
// Override the start method in the Application class
15
public void
start(Stage primaryStage) {
16
// Create a border pane
17
BorderPane pane =
new
BorderPane();
create a border pane
18
19
// Place nodes in the pane
20
pane.setTop(getHBox());
add an
HBox
to top
add a
VBox
to left
21
pane.setLeft(getVBox());
22
23
// Create a scene and place it in the stage
24 Scene scene =
new
Scene(pane);
25 primaryStage.setTitle(
"
ShowHBoxVBox
"
);
// Set the stage title
26 primaryStage.setScene(scene);
// Place the scene in the stage
27 primaryStage.show();
// Display the stage
28 }
29
30
private
HBox getHBox() {
31 HBox hBox =
new
HBox(
15
);
32 hBox.setPadding(
new
Insets(
15
,
15
,
15
,
15
));
33 hBox.setStyle(
"
-fx-background-color: gold
"
);
34 hBox.getChildren().add(
new
Button(
"Computer Science"
));
35 hBox.getChildren().add(
new
Button(
"Chemistry"
));
36 ImageView imageView =
new
ImageView(
new
Image(
"image/us.gif"
));
37
create a scene
display stage
getHBox
add buttons to
HBox
hBox.getChildren().add(imageView);
38
return
hBox;
return an
HBox
39 }
40
41
private
VBox getVBox() {
42 VBox vBox =
new
VBox(
15
);
43 vBox.setPadding(
new
Insets(
15
,
5
,
5
,
5
));
44
getVBox
vBox.getChildren().add(
new
Label(
"Courses"
));
add a label
45
46 Label[] courses = {
new
Label(
"CSCI 1301"
),
new
Label(
"CSCI 1302"
),
47
new
Label(
"CSCI 2410"
),
new
Label(
"CSCI 3720"
)};
48
49
for
(Label course: courses) {
50 VBox.setMargin(course,
new
Insets(
0
,
0
,
0
,
15
));
51
set margin
add a label
vBox.getChildren().add(course);
52 }
53
54
return
vBox;
return
vBox
55 }
56 }
Search WWH ::
Custom Search