如何从hbox移除形状?

时间:2019-06-17 03:18:00

标签: java javafx event-handling vbox 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); 

    }

}

1 个答案:

答案 0 :(得分:3)

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

首先,当您调用group.getChildren时,会丢失括号。这是语法错误。

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

更正后的语句如下:

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

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