Java Reference
In-Depth Information
L
ISTING
15.3
ControlCircle.java
1
import
javafx.application.Application;
2
import
javafx.event.ActionEvent;
3
import
javafx.event.EventHandler;
4
import
javafx.geometry.Pos;
5
import
javafx.scene.Scene;
6
import
javafx.scene.control.Button;
7
import
javafx.scene.layout.StackPane;
8
import
javafx.scene.layout.HBox;
9
import
javafx.scene.layout.BorderPane;
10
import
javafx.scene.paint.Color;
11
import
javafx.scene.shape.Circle;
12
import
javafx.stage.Stage;
13
14
public class
ControlCircle
extends
Application {
15
VideoNote
Handler and its registration
private
CirclePane circlePane =
new
CirclePane();
16
17 @Override
// Override the start method in the Application class
18
public void
start(Stage primaryStage) {
19
// Hold two buttons in an HBox
20 HBox hBox =
new
HBox();
21 hBox.setSpacing(
10
);
22 hBox.setAlignment(Pos.CENTER);
23 Button btEnlarge =
new
Button(
"Enlarge"
);
24 Button btShrink =
new
Button(
"Shrink"
);
25 hBox.getChildren().add(btEnlarge);
26 hBox.getChildren().add(btShrink);
27
28
// Create and register the handler
29
btEnlarge.setOnAction(
new
EnlargeHandler());
create/register handler
30
31 BorderPane borderPane =
new
BorderPane();
32 borderPane.setCenter(circlePane);
33 borderPane.setBottom(hBox);
34 BorderPane.setAlignment(hBox, Pos.CENTER);
35
36
// Create a scene and place it in the stage
37 Scene scene =
new
Scene(borderPane,
200
,
150
);
38 primaryStage.setTitle(
"ControlCircle"
);
// Set the stage title
39 primaryStage.setScene(scene);
// Place the scene in the stage
40 primaryStage.show();
// Display the stage
41 }
42
43
class
EnlargeHandler
implements
EventHandler<ActionEvent> {
44 @Override
// Override the handle method
45
handler class
public void
handle(ActionEvent e) {
46
circlePane.enlarge();
47 }
48 }
49 }
50
51
class
CirclePane
extends
StackPane {
52
CirclePane
class
private
Circle circle =
new
Circle(
50
);
53
54
public
CirclePane() {
55 getChildren().add(circle);
56 circle.setStroke(Color.BLACK);
57 circle.setFill(Color.WHITE);
58 }
Search WWH ::
Custom Search