繁体   English   中英

单击创建按钮时,java fx创建球

[英]java fx creating balls when create button is clicked

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

任何帮助表示赞赏。

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();
}*/
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM