Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javafx.application.Application;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.layout.AnchorPane;
- import javafx.scene.layout.Pane;
- import javafx.scene.shape.Line;
- import javafx.scene.shape.Rectangle;
- import javafx.stage.Stage;
- public class App extends Application {
- @Override
- public void start(Stage stage) throws Exception {
- Pane pane = new AnchorPane();
- mountDemo(pane);
- stage.setScene(new Scene(pane));
- stage.setMaximized(true);
- stage.show();
- }
- private static void mountDemo(Pane pane) {
- Button buttonAdd = new Button("Add");
- Button buttonRemove = new Button("Remove");
- buttonRemove.setLayoutX(50);
- Line line = new Line(100, 100, 400, 100);
- Rectangle rectangle = new Rectangle(5, 5);
- pane.getChildren().add(buttonAdd);
- pane.getChildren().add(buttonRemove);
- buttonAdd.setOnMouseClicked((event)->pane.getChildren().add(line));
- buttonRemove.setOnMouseClicked((event)->pane.getChildren().remove(line));
- line.parentProperty().addListener((observable, oldParent, newParent)->{
- if(newParent != null)
- ((Pane)newParent).getChildren().add(rectangle);
- else
- ((Pane)oldParent).getChildren().remove(rectangle);
- });
- }
- public static void main(String[] args) {
- launch(args);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement