Advertisement
Guest User

JavaFX

a guest
Aug 15th, 2015
531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.45 KB | None | 0 0
  1. import javafx.application.Application;
  2. import javafx.scene.Scene;
  3. import javafx.scene.control.Button;
  4. import javafx.scene.layout.AnchorPane;
  5. import javafx.scene.layout.Pane;
  6. import javafx.scene.shape.Line;
  7. import javafx.scene.shape.Rectangle;
  8. import javafx.stage.Stage;
  9.  
  10. public class App extends Application {
  11.    
  12.     @Override
  13.     public void start(Stage stage) throws Exception {
  14.         Pane pane = new AnchorPane();
  15.        
  16.         mountDemo(pane);
  17.        
  18.         stage.setScene(new Scene(pane));
  19.         stage.setMaximized(true);
  20.         stage.show();
  21.     }
  22.  
  23.     private static void mountDemo(Pane pane) {
  24.         Button buttonAdd = new Button("Add");
  25.         Button buttonRemove = new Button("Remove");
  26.         buttonRemove.setLayoutX(50);
  27.        
  28.         Line line = new Line(100, 100, 400, 100);
  29.         Rectangle rectangle = new Rectangle(5, 5);
  30.  
  31.         pane.getChildren().add(buttonAdd);
  32.         pane.getChildren().add(buttonRemove);
  33.        
  34.         buttonAdd.setOnMouseClicked((event)->pane.getChildren().add(line));
  35.         buttonRemove.setOnMouseClicked((event)->pane.getChildren().remove(line));
  36.  
  37.         line.parentProperty().addListener((observable, oldParent, newParent)->{
  38.             if(newParent != null)
  39.                 ((Pane)newParent).getChildren().add(rectangle);
  40.             else
  41.                 ((Pane)oldParent).getChildren().remove(rectangle);
  42.         });    
  43.     }
  44.    
  45.     public static void main(String[] args) {
  46.         launch(args);
  47.     }
  48.    
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement