JavaFX - 使用按钮 setOnAction 删除形状

JavaFX - Remove Shape With Button setOnAction

我正在尝试创建一个应用程序,用户在其中输入多条边,并在单击按钮后从场景中删除任何当前形状并生成具有指定边数的形状。

虽然生成形状的逻辑有效,但实施 root.getChildren().remove(shape); 似乎无效?

public class genShape extends Application 
{
    @Override    
    public void start(Stage stage) 
    {       
        // configure text fields for input         
        TextField shapeNoSidesField = new TextField();         
        shapeNoSidesField.setMaxWidth(23);         
        
        // configure label fields        
        Label shapeNoSidesLabel = new Label("Enter Desired Number Of Shape Sides:");         
        shapeNoSidesLabel.setTextFill(Color.RED);         
        shapeNoSidesLabel.setFont(Font.font("Arial", 10));   

        // configure generate shape button         
        Button generateShapeButton = new Button();         
        generateShapeButton.setText("Generate Shape");
        
        // configure an HBox input components                       
        HBox inputComponents = new HBox(40);
        inputComponents.setAlignment(Pos.CENTER);        
        inputComponents.getChildren().addAll(shapeNoSidesLabel, shapeNoSidesField);  
        
        // configure vbox
        VBox root = new VBox(50);         
        root.setAlignment(Pos.CENTER);        
        root.getChildren().addAll(inputComponents, generateShapeButton); 
        
        
        // Generate shape based on user Integral input. Perform input checks.
        generateShapeButton.setOnAction( e ->                     
        {   
            Shape shape = null;
            root.getChildren().remove(shape);
            // Perform input checks using helper variables. Assert no of sides field not null.
            boolean shapeFieldIsEmpty = shapeNoSidesField.getText().isEmpty();
            if(shapeFieldIsEmpty)        
            {                               
                System.out.println("Number of Sides Field Cannot Be Empty. Please Enter a Number Between 3 and 5 Inclusive.");
                display.setText("Number of Sides Field Cannot Be Empty. Please Enter a Number Between 3 and 5 Inclusive.");
            }
            else 
            {
                // Retrieve user required number of sides
                int numberOfSides = Integer.parseInt(shapeNoSidesField.getText());
                switch(numberOfSides) 
                {
                    // User enters shape requirement of three sides (Polygon object)
                    case 3:
                        shape = new Polygon();
                        ((Polygon) shape).getPoints().addAll(new Double[]
                                {
                                    100., 70.,
                                    100., 250.,
                                    400., 250.
                                });
                        root.getChildren().add(shape);
                        break;
                    case 4:
                        shape = new Rectangle();
                        ((Rectangle) shape).setX(150f);
                        ((Rectangle) shape).setY(75.0f); 
                        ((Rectangle) shape).setWidth(300.0f); 
                        ((Rectangle) shape).setHeight(150.0f);
                        root.getChildren().add(shape);
                        break;
                    default:
                        break;
                }
      
            }                                                                        
        }                                     
    ); 
        
        // create a new scene and add it to the stage         
        Scene scene = new Scene(root, 500, 500);         
        stage.setScene(scene);         
        stage.setTitle("Shapes");        
        stage.show();             
        }
    public static void main(String[] args) 
    {
        // TODO Auto-generated method stub
        launch(args);     
    }
}

非常感谢任何帮助,谢谢!

只需将shape设为实例变量即可。另请注意,不需要所有丑陋的向下转换。

public class GenShape extends Application 
{

    private Shape shape ;

    @Override    
    public void start(Stage stage) 
    {       
        // configure text fields for input         
        TextField shapeNoSidesField = new TextField();         
        shapeNoSidesField.setMaxWidth(23);         
        
        // configure label fields        
        Label shapeNoSidesLabel = new Label("Enter Desired Number Of Shape Sides:");         
        shapeNoSidesLabel.setTextFill(Color.RED);         
        shapeNoSidesLabel.setFont(Font.font("Arial", 10));   

        // configure generate shape button         
        Button generateShapeButton = new Button();         
        generateShapeButton.setText("Generate Shape");
        
        // configure an HBox input components                       
        HBox inputComponents = new HBox(40);
        inputComponents.setAlignment(Pos.CENTER);        
        inputComponents.getChildren().addAll(shapeNoSidesLabel, shapeNoSidesField);  
        
        // configure vbox
        VBox root = new VBox(50);         
        root.setAlignment(Pos.CENTER);        
        root.getChildren().addAll(inputComponents, generateShapeButton); 
        
        
        // Generate shape based on user Integral input. Perform input checks.
        generateShapeButton.setOnAction( e ->                     
        {   
            if (shape != null) {
                root.getChildren().remove(shape);
            }

            // Perform input checks using helper variables. Assert no of sides field not null.
            boolean shapeFieldIsEmpty = shapeNoSidesField.getText().isEmpty();
            if(shapeFieldIsEmpty)        
            {                               
                System.out.println("Number of Sides Field Cannot Be Empty. Please Enter a Number Between 3 and 5 Inclusive.");
                display.setText("Number of Sides Field Cannot Be Empty. Please Enter a Number Between 3 and 5 Inclusive.");
            }
            else 
            {
                // Retrieve user required number of sides
                int numberOfSides = Integer.parseInt(shapeNoSidesField.getText());
                switch(numberOfSides) 
                {
                    // User enters shape requirement of three sides (Polygon object)
                    case 3:
                        Polygon poly = new Polygon();
                        poly.getPoints().addAll(new Double[]
                                {
                                    100., 70.,
                                    100., 250.,
                                    400., 250.
                                });
                        root.getChildren().add(poly);
                        this.shape = poly ;
                        break;
                    case 4:
                        Rectangle rect = new Rectangle();
                        rect.setX(150f);
                        rect.setY(75.0f); 
                        rect.setWidth(300.0f); 
                        rect.setHeight(150.0f);
                        root.getChildren().add(rect);
                        this.shape = rect ;
                        break;
                    default:
                        break;
                }
      
            }                                                                        
        }                                     
    ); 
        
        // create a new scene and add it to the stage         
        Scene scene = new Scene(root, 500, 500);         
        stage.setScene(scene);         
        stage.setTitle("Shapes");        
        stage.show();             
        }
    public static void main(String[] args) 
    {
        // TODO Auto-generated method stub
        launch(args);     
    }
}