Java Reference
In-Depth Information
58
59 HBox hBox =
new
HBox(
10
);
60 hBox.getChildren().addAll(btPlayPause,
61
new
Label(
"Select a nation: "
), cboNation);
62 hBox.setAlignment(Pos.CENTER);
63
64
// Create a pane to hold nodes
65 BorderPane pane =
new
BorderPane();
66 pane.setCenter(imageView);
67 pane.setBottom(hBox);
68
69
// Create a scene and place it in the stage
70 Scene scene =
new
Scene(pane,
350
,
270
);
71 primaryStage.setTitle(
"FlagAnthem"
);
// Set the stage title
72 primaryStage.setScene(scene);
// Place the scene in the stage
73 primaryStage.show();
// Display the stage
74 }
75 }
The program loads the image and audio from the Internet (lines 29-33). A play/pause button is
created to control the playing of the audio (line 35). When the button is clicked, if the button's
current text is
>
(line 37), its text is changed to
||
(line 38) and the player is paused (line 39);
If the button's current text is
||
, it is changed to
>
(line 41) and the player is paused (line 42).
An image view is created to display a flag image (line 46). A combo box is created for
selecting a nation (line 47-49). When a new country name in the combo box is selected, the
current audio is stopped (line 53) and the newly selected nation's image is displayed (line 55)
and the new anthem is played (line 56).
JavaFX also provides the
AudioClip
class for creating auto clips. An
AudioClip
object can be created using
new AudioClip(URL)
. An audio clip stores the audio in
memory.
AudioClip
is more efficient for playing a small audio clip in the program than
using
MediaPlayer
.
AudioClip
has the similar methods as in the
MediaPlayer
class.
16.43
✓
✓
In Listing 16.15, which code sets the initial image icon and which code plays the
audio?
Check
Point
16.44
In Listing 16.15, what does the program do when a new nation is selected in the
combo box?
C
HAPTER
S
UMMARY
1.
The abstract
Labeled
class is the base class for
Label
,
Button
,
CheckBox
, and
RadioButton
. It defines properties
alignment
,
contentDisplay
,
text
,
graphic
,
graphicTextGap
,
textFill
,
underline
, and
wrapText
.
2.
The abstract
ButtonBase
class is the base class for
Button
,
CheckBox
, and
RadioButton
. It defines the
onAction
property for specifying a handler for action events.
3.
The abstract
TextInputContorl
class is the base class for
TextField
and
TextArea
.
It defines the properties
text
and
editable
.
4.
A
TextField
fires an action event when clicking the Enter key with the text field
focused. A
TextArea
is often used for editing a multiline text.
5.
ComboBox<T>
and
ListView<T>
are generic classes for storing elements of type
T
.
The elements in a combo box or a list view are stored in an observable list.
Search WWH ::
Custom Search