Java Reference
In-Depth Information
javafx.scene.control.Labeled
The getter and setter methods for property
values and a getter for property itself are provided
in the class, but omitted in the UML diagram for brevity.
javafx.scene.control.ButtonBase
-onAction: ObjectProperty<EventHandler
<ActionEvent>>
Defines a handler for handling a button's action.
javafx.scene.control.Button
+Button()
+Button(text: String)
+Button(text: String, graphic: Node)
Creates an empty button.
Creates a button with the specified text.
Creates a button with the specified text and graphic.
F
IGURE
16.5
ButtonBase
extends
Labeled
and defines common features for all buttons.
Listing 16.2 gives a program that uses the buttons to control the movement of a text, as
shown in FigureĀ 16.6.
L
ISTING
16.2
ButtonDemo.java
1
import
javafx.application.Application;
2
import
javafx.stage.Stage;
3
import
javafx.geometry.Pos;
4
import
javafx.scene.Scene;
5
import
javafx.scene.control.Button;
6
import
javafx.scene.image.ImageView;
7
import
javafx.scene.layout.BorderPane;
8
import
javafx.scene.layout.HBox;
9
import
javafx.scene.layout.Pane;
10
import
javafx.scene.text.Text;
11
12
public class
ButtonDemo
extends
Application {
13
protected
Text text =
new
Text(
50
,
50
,
"JavaFX Programming"
);
14
15
protected
BorderPane getPane() {
16 HBox paneForButtons =
new
HBox(
20
);
17 Button btLeft =
new
Button(
"Left"
,
18
new
ImageView(
"image/left.gif"
));
19 Button btRight =
new
Button(
"Right"
,
20
new
ImageView(
"image/right.gif"
));
21 paneForButtons.getChildren().addAll(btLeft, btRight);
22 paneForButtons.setAlignment(Pos.CENTER);
23 paneForButtons.setStyle(
"-fx-border-color: green"
);
24
25 BorderPane pane =
new
BorderPane();
26 pane.setBottom(paneForButtons);
27
28 Pane paneForText =
new
Pane();
29 paneForText.getChildren().add(text);
30 pane.setCenter(paneForText);
31
32
create a button
add buttons to pane
create a border pane
add buttons to the bottom
btLeft.setOnAction(e -> text.setX(text.getX() -
10
));
add an action handler
33
btRight.setOnAction(e -> text.setX(text.getX() +
10
));
34
35
return
pane;
return a pane
Search WWH ::
Custom Search