簡體   English   中英

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);     
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM