用于移动形状的 JavaFx 按钮事件处理程序

标签 java button javafx event-handling geometry

我上周刚刚启动 JavaFx,在尝试在 Pane 中设置圆圈以响应按钮事件处理程序时遇到问题。我设置了一些按钮,其名称为左、右、上、下,按下这些按钮时应该将圆圈移动到 Pane 内。我的问题是我根本无法让圆圈响应我的事件处理程序。我看到了另一个教程,其中包含按键来移动圆圈,我正在尝试类似的东西,但用按钮代替。任何帮助我走向正确方向的帮助将非常感谢。

package movingball;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class MovingBall extends Application{
  private CirclePane circlePane = new CirclePane();


@Override
public void start(Stage primaryStage) {
    StackPane pane = new StackPane();
    Circle circle = new Circle(50);
    circle.setStroke(Color.BLACK);
    circle.setFill(Color.WHITE);
    pane.getChildren().add(circle);

    HBox hBox = new HBox();
    hBox.setSpacing(10);
    hBox.setAlignment(Pos.CENTER);
    Button btLeft = new Button("Left");
    Button btRight = new Button("Right");
    Button btUp = new Button("Up");
    Button btDown = new Button("Down");
    hBox.getChildren().addAll(btLeft, btRight, btUp, btDown);

    btLeft.setOnAction(new LeftHandler());
    btRight.setOnAction(new RightHandler());
    btUp.setOnAction(new UpHandler());
    btDown.setOnAction(new DownHandler());


    BorderPane borderPane = new BorderPane();
    borderPane.setCenter(pane);
    borderPane.setBottom(hBox);
    BorderPane.setAlignment(hBox, Pos.CENTER);

    // Create a scene and place it in the stage
    Scene scene = new Scene(borderPane, 500, 350);
    primaryStage.setTitle("Move the Ball"); // Set the stage title
    primaryStage.setScene(scene); // Place the scene in the stage
    primaryStage.show(); // Display the stage
  }



class LeftHandler implements EventHandler<ActionEvent> {

    @Override
    public void handle(ActionEvent event) {
        circlePane.left();

    }

}

class RightHandler implements EventHandler<ActionEvent> {

    @Override
    public void handle(ActionEvent event) {
        circlePane.right();

    }

}

class UpHandler implements EventHandler<ActionEvent> {

    @Override
    public void handle(ActionEvent event) {
        circlePane.up();

    }

}

class DownHandler implements EventHandler<ActionEvent> {

    @Override
    public void handle(ActionEvent event) {
        circlePane.down();

    }

}

class CirclePane extends StackPane {
    private Circle circle = new Circle(50);

    public CirclePane() {
        getChildren().add(circle);
        circle.setStroke(Color.BLACK);
        circle.setFill(Color.WHITE);
    }

    public void left() {
        circle.setCenterX(circle.getCenterX() - 10);
    }

    public void right() {
        circle.setCenterX(circle.getCenterX() + 10);
    }

    public void up() {
        circle.setCenterY(circle.getCenterY() - 10);
    }

    public void down() {
        circle.setCenterY(circle.getCenterY() + 10);
    }
}


  public static void main(String[] args) {
    launch(args);
  }

}

最佳答案

第一个问题是您正在移动 CirclePane,但它不是场景的一部分。删除您在 start(...) 方法中创建的 panecircle,并将 circlePane 放入改为 BorderPane 的中心。

第二个问题是,StackPane 将以圆为中心,调整其坐标系以在圆移动时保持其居中。因此,使 CirclePane 成为 Pane 的子类,而不是 StackPane。或者,您可以调用circle.setManaged(false);来防止StackPane定位

关于用于移动形状的 JavaFx 按钮事件处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29199582/

相关文章:

swift - 使用按钮 swift 自定义单元格序列

javascript - onclick 时更改按钮文本

JavaFX 两个 TableView 之间的单选

eclipse - 单击堆栈跟踪时出现 "Source not found"

JavaFX 8 在 Outlook 中创建新邮件

java - servlet 容器是否应该为每个传入请求创建新的 javax.servlet.http.HttpServlet 实例?

java.lang.RuntimeException : Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to Activity

java - 沿鼠标光标方向画线

animation - SwiftUI - 按钮在 View 的左上角不起作用

java - Java REST 客户端接收和保存 excel 响应(字节数组)时出错