当前位置: 首页 > 知识库问答 >
问题:

JavaFX-使用按钮setOnAction删除形状

蒙洛华
2023-03-14

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

虽然生成形状的逻辑正在工作,但实现root.get孩子()。删除(形状);似乎不起作用?

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

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

共有1个答案

田向荣
2023-03-14

只需将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);     
    }
}
 类似资料:
  • 我正在使用JavaFX构建一个calander/planner应用程序。该应用程序包括一个单一的GridPane与35(7x5)VBox的。在这些VBox中有任务按钮(在下面实现)。当我右键单击任务框时,它将把文本变成灰色,当我左键单击TsskButton时,我希望它删除按钮。我已经知道的事情。 AnchorPaneNode(扩展VBox)没有静态getChildren()方法 我无法为窗格创建单

  • 问题内容: 我正在尝试学习JavaFX,并且已经编写了下面显示的代码,但是我似乎在使用以下代码行时遇到麻烦: 在强调setOnAction的位置并显示此错误: 我究竟做错了什么? 问题答案: 您已经导入了awt事件监听器,只需更改此行代码 有了这个 你也可以像这样使用lambda表达式

  • 我正在尝试学习JavaFX,我已经编写了下面所示的代码,但这行代码似乎有问题: 其中它在setOnAction下划线,并打印此错误: 我做错了什么?

  • 我不是JavaFX的资深程序员,想请教一下。如何在JavaFX中为按钮制作或设置不同的形状?默认情况下,按钮具有这些圆角矩形形状。我想做一个按钮形状像下面的图片,一个L形按钮。 我已经谷歌了这个话题很多次,但仍然没有找到解决办法。 你能帮助我吗?

  • 如何在JavaFX中创建每行都有删除按钮和删除按钮操作?

  • 我正在用javaFX在netbeans中编写一个程序视图中有几个按钮和一些坏按钮(比如炸弹是扫雷舰),我试图在按下坏按钮时冻结程序,但我找不到如何操作 谢谢