public class ButtonDemo3 extends Application
1: import javafx.application.Application;
2: import javafx.scene.Scene;
3: import javafx.stage.Stage;
4: import javafx.scene.text.Font;
5: import javafx.scene.layout.VBox;
6: import javafx.scene.control.Button;
7: import javafx.event.ActionEvent;
8: import javafx.event.EventHandler;
9: import javafx.scene.control.Label;
10:
11: /**
12: Event handling with an anonymous inner class.
13: */
14: public class ButtonDemo3 extends Application
15: {
16: public static void main(String[] args)
17: {
18: launch(args);
19: }
20:
21: @Override
22: public void start(Stage primaryStage) throws Exception
23: {
24: VBox root = new VBox();
25: Button btnSunny;
26: Button btnCloudy;
27: Label lblMessage;
28: btnSunny = new Button("Sunny");
29: btnCloudy = new Button("Cloudy");
30: lblMessage = new Label("Click a button.");
31:
32: // Create an anonymous inner class to handle btnSunny
33: btnSunny.setOnAction(new EventHandler<ActionEvent>()
34: {
35: @Override
36: public void handle(ActionEvent event)
37: {
38: lblMessage.setText("It is sunny!");
39: }
40: }
41: );
42: // Create an anonymous inner class to handle btnCloudy
43: btnCloudy.setOnAction(new EventHandler<ActionEvent>()
44: {
45: @Override
46: public void handle(ActionEvent event)
47: {
48: lblMessage.setText("It is cloudy!");
49: }
50: }
51: );
52:
53: root.getChildren().add(btnSunny);
54: root.getChildren().add(btnCloudy);
55: root.getChildren().add(lblMessage);
56:
57: Scene scene = new Scene(root, 300, 100);
58: primaryStage.setTitle("Button Demo 3");
59: primaryStage.setScene(scene);
60: primaryStage.show();
61: }
62: }