Я пытаюсь создать программу, когда нажата кнопка, она покажет лицо.Например, кнопка Smile покажет улыбающееся лицо, кнопка Think покажет мыслящее лицо.Проблема в том, как я могу удалить две другие фигуры, когда выбрана одна фигура?
Я пробовал следующее:
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);
}
}