gpt4 book ai didi

java - 如何从 hbox 中删除形状?

转载 作者:行者123 更新时间:2023-12-01 19:38:13 26 4
gpt4 key购买 nike

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

我尝试过以下方法:

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));
<小时/>

话虽这么说,我相信您会发现应用程序的行为与您认为应该的不同,并且可能需要完全重新考虑您的设计。

关于java - 如何从 hbox 中删除形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56624328/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com