节点移动 - JavaFX

时间:2015-03-22 19:13:07

标签: java javafx

我的编程大部分工作。圆圈似乎只能在一个小区域内移动。然后它击中了边界。为什么是这样?我需要将circlePane放大吗?

MoveTheCircle:

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.input.KeyCode;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

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

    public void start(Stage primaryStage) {

        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().add(btLeft);
        hBox.getChildren().add(btRight);
        hBox.getChildren().add(btUp);
        hBox.getChildren().add(btDown);

        btLeft.setOnAction(e -> circlePane.left());
        btRight.setOnAction(e -> circlePane.right());
        btUp.setOnAction(e -> circlePane.up());
        btDown.setOnAction(e -> circlePane.down());

        circlePane.setOnMouseClicked(e -> {
            if (e.getButton() == MouseButton.PRIMARY) {
                circlePane.left();
            } else if (e.getButton() == MouseButton.PRIMARY) {
                circlePane.right();
            } else if (e.getButton() == MouseButton.PRIMARY) {
                circlePane.up();
            } else if (e.getButton() == MouseButton.PRIMARY) {
                circlePane.down();
            }
        });

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

        Scene scene = new Scene(borderPane, 350, 250);
        primaryStage.setTitle("Move The Ball"); 
        primaryStage.setScene(scene); 
        primaryStage.show();

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

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

        public void left() {
            circlePane.getTranslateX();
            circlePane.setTranslateX(-10);
        }

        public void right() {
            circlePane.getTranslateX();
            circlePane.setTranslateX(+10);
        }

        public void up() {
            circlePane.getTranslateY();
            circlePane.setTranslateY(-10);
        }

        public void down() {
            circlePane.getTranslateY();
            circlePane.setTranslateY(+10);
        }
    }
     public static void main(String[] args) {
            launch(args);
          }
}

感谢任何和所有帮助。

1 个答案:

答案 0 :(得分:1)

你需要

public void left() {
    circle.setTranslateX(circle.getTranslateX()-10);
}

而不是

public void left() {
    circle.getTranslateX();
    circle.setTranslateX(-10);
}

同样适用于所有其他方法。