我刚刚在上周开始使用JavaFx,并且在尝试在窗格中设置圆圈以响应按钮事件处理程序时出现问题。我的按钮设置有左,右,上,下的名称,当按下时应将圆圈移动到窗格内。我的问题是我无法让圈子响应我的事件处理程序。我看到另一个教程,包括按键移动圆圈,我正在尝试类似的东西,但用按钮代替。让我走向正确方向的任何帮助都将非常感谢。
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);
}
}
答案 0 :(得分:1)
第一个问题是你正在移动CirclePane
,但它不是你场景的一部分。移除您在pane
方法中创建的circle
和start(...)
,然后将circlePane
放在BorderPane
的中心位置。
第二个问题是StackPane
会使圆圈居中,调整其坐标系以使圆圈移动时保持居中。因此,将CirclePane
设为Pane
的子类,而不是StackPane
。或者,您可以致电circle.setManaged(false);
以阻止StackPane
定位circle
。