Source of ButtonDemo1.java


  1: import javafx.application.Application;
  2: import javafx.scene.Scene;
  3: import javafx.stage.Stage;
  4: import javafx.scene.layout.VBox;
  5: import javafx.scene.control.Button;
  6: 
  7: /**
  8: Simple demonstration of programming buttons in a JavaFX application.
  9: This version outputs a message when clicked.
 10: */
 11: public class ButtonDemo1 extends Application
 12: {
 13:    public static void main(String[] args)
 14:    {
 15:       launch(args);
 16:    }
 17: 
 18:    @Override
 19:    public void start(Stage primaryStage) throws Exception
 20:    {
 21:           VBox root = new VBox();
 22:       Button btnSunny;
 23:       Button btnCloudy;
 24:           btnSunny = new Button("Sunny");
 25:           btnCloudy = new Button("Cloudy");
 26: 
 27:           // Create an event object to handle the button click.
 28:           // The "handle" method in HandleButtonClick will be
 29:           // invoked when the button is clicked.
 30:           HandleButtonClick clickEvent = new HandleButtonClick();
 31:           btnSunny.setOnAction(clickEvent);
 32: 
 33:           // We can also create the HandleButtonClick object without
 34:           // a named reference by creating it inside the call to setOnAction
 35:           btnCloudy.setOnAction(new HandleButtonClick("It is cloudy."));
 36: 
 37:           root.getChildren().add(btnSunny);
 38:           root.getChildren().add(btnCloudy);
 39: 
 40:              Scene scene = new Scene(root, 300, 100);
 41:       primaryStage.setTitle("Button Event Handling Demo");
 42:       primaryStage.setScene(scene);
 43:       primaryStage.show();
 44:    }
 45: }