java根据用户输入显示多个动画形状(JavaFX)
我正在编写一个代码,获取用户输入的整数,它将获得窗格周围随机大小和位置的圆圈数。例如,如果用户想要5个圆,则输入5,将显示5个圆并设置动画。程序的动画部分工作正常。问题是,一旦我将圆实现为for循环,它们现在就不会出现(因此我可以根据用户的输入有多个圆。每当我按下按钮显示动画圆时,只有一个圆显示出来,然后IDE底部的框会给我随机错误。我不确定如何解决此问题
这是我的代码:
private class buttonEvent implements EventHandler<ActionEvent> {
@Override
public void handle(ActionEvent e) {
String countCircles = numOfCircles.getText();
int circleCount = Integer.parseInt(countCircles);
String countDuration = duration.getText();
int speed = Integer.parseInt(countDuration);
double widthRand = ((Math.random() * (800 - 0)) + 0);
double lengthRand = ((Math.random() * (600 - 0)) + 0);
double radiusRand = Math.floor(Math.random() * (100 - 10 + 1) + 10);
Circle circle = new Circle(widthRand, lengthRand, radiusRand);
circle.setFill(Color.color(Math.random(), Math.random(), Math.random()));
ScaleTransition scaleTr = new ScaleTransition();
scaleTr.setDuration(Duration.millis(speed));
scaleTr.setFromX(1);
scaleTr.setFromY(1);
scaleTr.setToX(0.001);
scaleTr.setToY(0.001);
scaleTr.setCycleCount(3);
scaleTr.setAutoReverse(true);
group = new GroupLayout.Group(circle);
scaleTr.setNode(group);
scaleTr.play();
for (int i = 0; i < circleCount; i++) {
display.getChildren().addAll(group);
}
}
}
# 1 楼答案
异常为“java.lang.IllegalArgumentException:子项:添加的重复子项:父项=Pane@...”。这是因为您在一次又一次地添加同一组。按以下方式修改代码可以解决此问题: