如何从hbox中删除形状



我正在尝试创建一个程序,当按钮单击它时,它会显示脸。例如,"微笑"按钮将显示笑脸,"思考"按钮将显示思考的脸。问题是,当选择一个形状时,如何删除其他两个形状?

我尝试了以下方法:

frownButton.setOnAction(e-> group.getChildren.remove(mouthThink, mouthSmile));

但是有一个错误,我不确定如何解决这个问题。

public class ChangingFace extends Application {
    @Override
        public void start(Stage stage)     
         { 
             // create and configure the main circle for the face        
             Circle face = new Circle(125, 125, 80);          
             face.setFill(Color.YELLOW);         
             face.setStroke(Color.RED);  
             // create and configure the circle for the right eye         
             Circle rightEye = new Circle(86, 100, 10);         
             rightEye.setFill(Color.YELLOW);         
             rightEye.setStroke(Color.BLUE); 
             // create and configure the circle for the left eye         
             Circle leftEye = new Circle(162, 100, 10);         
             leftEye.setFill(Color.YELLOW);         
             leftEye.setStroke(Color.BLUE);   
             // create and configure a smiling mouth (this is how it will start) 
                         ///     
             Arc mouthSmile = new Arc(125, 150, 45, 35, 0, -180);          
             mouthSmile.setFill(Color.YELLOW);         
             mouthSmile.setStroke(Color.BLUE);         
             mouthSmile.setType(ArcType.OPEN); 
             Arc mouthFrown = new Arc(125, 150, 45, 35, 0, 180);          
             mouthFrown.setFill(Color.YELLOW);         
             mouthFrown.setStroke(Color.BLUE);         
             mouthFrown.setType(ArcType.OPEN);
             Line mouthThink = new Line(125,150,225,150);
             mouthThink.setFill(Color.YELLOW);         
             mouthThink.setStroke(Color.BLUE); 
             // create and configure the text        
             Text caption = new Text(68, 240, "Changing Face");         
             caption.setFill(Color.BLUE);         
             caption.setFont(Font.font ("Verdana", 15));
             // create a group that holds all the features           
             Group group = new Group(face, rightEye, leftEye,caption, mouthSmile, mouthThink, mouthFrown); 
             // create a button that will make the face smile         
             Button smileButton = new Button("Smile"); 
             // create a button that will make the face frown         
             Button frownButton = new Button("Frown"); 
             // create a button that will make the face think 
             Button thinkButton = new Button("Think");
             // create and configure a horizontal container to hold the buttons    
             HBox buttonBox = new HBox(20);         
             buttonBox.setAlignment(Pos.CENTER); 
             //add the buttons to the horizontal container       
             buttonBox.getChildren().addAll(smileButton,thinkButton, frownButton);
             // create and configure a vertical container to hold the button box and the face group         
             VBox root = new VBox(10); 
             root.setBackground(Background.EMPTY);         
             root.setAlignment(Pos.CENTER); 
             //add the button box and the face group to the vertical container         
             root.getChildren().addAll(buttonBox, group);
             // create and configure a new scene         
             Scene scene = new Scene(root, 250, 275, Color.YELLOW);
             // supply the code that is executed when the smile button is pressed  
             smileButton.setOnAction(e-> group.getChildren.remove(mouthThink, mouthFrown));
             // supply the code that is executed when the frown button is pressed   
             frownButton.setOnAction(e-> group.getChildren.remove(mouthThink, mouthSmile)); 
             // supply the code that is executed when the think button is pressed
             thinkButton.setOnAction(e-> group.getChildren.remove(mouthThink, mouthSmile));
             // add the scene to the stage, then set the title        
             stage.setScene(scene);         
             stage.setTitle("Changing Face");  
             // show the stage         
             stage.show();             
    }
    public static void main (String[] args) {
        launch(args); 
    }
}

您至少有 2 个问题会阻止您的代码编译。

首先,当您调用group.getChildren时,您缺少括号。这是一个语法错误。

其次,remove()方法接受一个对象或一个索引范围。相反,您需要使用 removeAll() .

更正后的语句如下所示:

smileButton.setOnAction(e-> group.getChildren().removeAll(mouthThink, mouthFrown));

话虽如此,我相信您会发现该应用程序的行为不符合您的预期,并且可能需要完全重新考虑您的设计。

相关内容

  • 没有找到相关文章

最新更新