单击“创建”按钮时创建球的java fx

单击“创建”按钮时创建球的java fx,java,javafx-2,Java,Javafx 2,每当单击“创建”按钮时,我都会尝试创建球。我可以创建一个球,但由于某些原因,在反复单击“创建”按钮时无法创建多个球 感谢您的帮助 package week3; import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.control.Slider; import javafx.scene.control.Button; impo

每当单击“创建”按钮时,我都会尝试创建球。我可以创建一个球,但由于某些原因,在反复单击“创建”按钮时无法创建多个球

感谢您的帮助

package week3;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.TilePane;
import javafx.geometry.Orientation;
import javafx.geometry.Insets;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.DoubleProperty;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.util.Duration;



public class BounceBallControl1 extends Application {
        public final double radius = 10;
        private double x = radius, y = radius;
        private double dx = 1, dy = 1;
        private Circle circle = new Circle(x, y, radius);
        private Timeline animation;
@SuppressWarnings("restriction")
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
//BallPane ballPane = new BallPane();

Button btnCreate = new Button("Create");
Button btnDelete = new Button("Delete");
btnCreate.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
btnDelete.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
TilePane tileButtons = new TilePane(Orientation.HORIZONTAL);
tileButtons.setPadding(new Insets(5, 5, 5, 70));
tileButtons.setHgap(20.0);
tileButtons.getChildren().addAll(btnCreate, btnDelete);

Slider slSpeed = new Slider();
slSpeed.setMax(20);
//rateProperty().bind(slSpeed.valueProperty());

BorderPane pane = new BorderPane();
//pane.setCenter(ballPane);
pane.setTop(slSpeed);
pane.setBottom(tileButtons);

btnCreate.setOnAction(new EventHandler<ActionEvent>() {

    public void handle(ActionEvent event) {
        for(int i=0;i<=100;i++)
        circle.setFill(Color.TURQUOISE); // Set ball color
        pane.getChildren().add(circle); // Place a ball into this pane

        // Create an animation for moving the ball
        animation = new Timeline(
        new KeyFrame(Duration.millis(50), (e -> {
            // Check boundaries
            if (x < radius || x > pane.getWidth() - radius) {
            dx *= -1; // Change ball move direction
            }
            if (y < radius || y > pane.getHeight() - radius) {
            dy *= -1; // Change ball move direction
            }

            // Adjust ball position
            x += dx;
            y += dy;
            circle.setCenterX(x);
            circle.setCenterY(y);
            })));
        animation.setCycleCount(Timeline.INDEFINITE);
        animation.play(); // Start animation
    }
});



// Create a scene and place it in the stage
Scene scene = new Scene(pane, 250, 250);
primaryStage.setTitle("BounceBallSlider"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}

/*public DoubleProperty rateProperty() {
return animation.rateProperty();
}*/
}
套餐周3;
导入javafx.application.application;
导入javafx.stage.stage;
导入javafx.scene.scene;
导入javafx.scene.control.Slider;
导入javafx.scene.control.Button;
导入javafx.scene.layout.BorderPane;
导入javafx.scene.layout.TilePane;
导入javafx.geometry.Orientation;
导入javafx.geometry.Insets;
导入javafx.event.ActionEvent;
导入javafx.event.EventHandler;
导入javafx.animation.KeyFrame;
导入javafx.animation.Timeline;
导入javafx.beans.property.DoubleProperty;
导入javafx.scene.layout.Pane;
导入javafx.scene.paint.Color;
导入javafx.scene.shape.Circle;
导入javafx.util.Duration;
公共类BounceBallControl1扩展了应用程序{
公共最终双半径=10;
专用双x=半径,y=半径;
专用双dx=1,dy=1;
专用圆=新圆(x,y,半径);
私人时间轴动画;
@禁止警告(“限制”)
@重写//重写应用程序类中的start方法
公共无效开始(阶段primaryStage){
//BallPane BallPane=新的BallPane();
按钮btnCreate=新建按钮(“创建”);
按钮btnDelete=新按钮(“删除”);
btnCreate.setMaxSize(Double.MAX_值,Double.MAX_值);
btnDelete.setMaxSize(Double.MAX\u值,Double.MAX\u值);
TilePane tileButtons=新的TilePane(方向为水平);
tileButtons.设置填充(新插图(5,5,5,70));
tileButtons.setHgap(20.0);
tileButtons.getChildren().addAll(btnCreate,btnDelete);
滑块slSpeed=新滑块();
slSpeed.setMax(20);
//rateProperty().bind(slSpeed.valueProperty());
BorderPane=新的BorderPane();
//窗格。设置中心(球形窗格);
窗格。机顶盒(slSpeed);
窗格.镶嵌框(瓷砖按钮);
btnCreate.setOnAction(新的EventHandler(){
公共无效句柄(ActionEvent事件){
对于(int i=0;i{
//检查边界
if(xpane.getWidth()-radius){
dx*=-1;//更改球的移动方向
}
if(ypane.getHeight()-radius){
dy*=-1;//更改球的移动方向
}
//调整球位
x+=dx;
y+=dy;
圆。设置中心x(x);
圆。设置中心(y);
})));
animation.setCycleCount(Timeline.unfinite);
animation.play();//开始动画
}
});
//创建一个场景并将其放置在舞台上
场景=新场景(窗格,250250);
setTitle(“BounceBallSlider”);//设置阶段标题
primaryStage.setScene(场景);//将场景放置在舞台中
primaryStage.show();//显示阶段
}
/*公营物业差饷物业(){
返回animation.rateProperty();
}*/
}

每次单击“添加”按钮时,都会使用相同的circle实例。因此,每次将同一个圆添加到场景中时。每次尝试创建一个新的圆圈,就可以了