Monty374

JFX 7.1

Dec 6th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. package handleevent;
  2.  
  3. import javafx.application.Application;
  4. import javafx.geometry.Pos;
  5. import javafx.scene.Scene;
  6. import javafx.scene.control.Button;
  7. import javafx.scene.layout.HBox;
  8. import javafx.stage.Stage;
  9. import javafx.event.ActionEvent;
  10. import javafx.event.EventHandler;
  11.  
  12. public class HandleEvent extends Application {
  13. @Override // Override the start method in the Application class
  14. public void start(Stage primaryStage) {
  15. // Create a pane and set its properties
  16. HBox pane = new HBox(10);
  17. pane.setAlignment(Pos.CENTER);
  18. Button btOK = new Button("OK");
  19. Button btCancel = new Button("Cancel");
  20. OKHandlerClass handler1 = new OKHandlerClass();
  21. btOK.setOnAction(handler1);
  22. CancelHandlerClass handler2 = new CancelHandlerClass();
  23. btCancel.setOnAction(handler2);
  24. pane.getChildren().addAll(btOK, btCancel);
  25.  
  26. // Create a scene and place it in the stage
  27. Scene scene = new Scene(pane);
  28. primaryStage.setTitle("HandleEvent"); // Set the stage title
  29. primaryStage.setScene(scene); // Place the scene in the stage
  30. primaryStage.show(); // Display the stage
  31. }
  32.  
  33. /**
  34. * The main method is only needed for the IDE with limited
  35. * JavaFX support. Not needed for running from the command line.
  36. */
  37. public static void main(String[] args) {
  38. launch(args);
  39. }
  40. }
  41.  
  42. class OKHandlerClass implements EventHandler<ActionEvent> {
  43. @Override
  44. public void handle(ActionEvent e) {
  45. System.out.println("OK button clicked");
  46. }
  47. }
  48.  
  49. class CancelHandlerClass implements EventHandler<ActionEvent> {
  50. @Override
  51. public void handle(ActionEvent e) {
  52. System.out.println("Cancel button clicked");
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment