【问题标题】:Dragging Function does not work correctly! JavaFX VBox Dragging拖动功能无法正常工作! JavaFX VBox 拖动
【发布时间】:2015-02-06 01:00:45
【问题描述】:

我在代码中创建了一个新阶段,并且需要该阶段未装饰!在这种情况下,新创建的舞台失去了拖动功能。

我创建了以下代码,现在我可以拖动舞台,但处理不正确,舞台跟随鼠标的方式不正确。

希望你能告诉我我的错误并修正我的错误。

感谢您的帮助。

示例 - 代码:

import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Point2D;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.MenuBar;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;


public class DragStuff  extends Application{

    VBox mainContainer;
    Stage mainSt;

    @Override
    public void start(Stage mainStage) throws Exception {
        // TODO Auto-generated method stub
        mainStage.initStyle(StageStyle.UNDECORATED);
        mainSt = mainStage;
        mainContainer = new VBox();
        mainContainer.setStyle("-fx-background-color:red");


        Label headlineInformation = new Label("Testing");
        headlineInformation.getStyleClass().addAll("popup-label-name");
        headlineInformation.setMaxWidth(Double.MAX_VALUE);

        Button closeButton = new Button("X");
        closeButton.setVisible(true);
        closeButton.getStyleClass().addAll("popup-button",
            "popup-button-color");

        HBox headContainer = new HBox();
        HBox.setHgrow(headlineInformation, Priority.ALWAYS);
        headContainer.setPadding(new Insets(5, 5, 5, 5));
        headContainer.getChildren().addAll(headlineInformation, closeButton);



        Pane pane = new Pane();
        pane.getChildren().addAll(new Label("Text                                                                                      Stuff"));

        mainContainer.getChildren().addAll(headContainer,pane);

        Scene sc = new Scene(mainContainer);
        mainStage.setScene(sc);
        mainStage.show();

        dragHandling();

    }

    public void dragHandling()
    {
        final ObjectProperty<Point2D> mouseKoordinates = new SimpleObjectProperty<>();
        mainContainer.setOnMousePressed(new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent event)
            {
                mouseKoordinates.set(new Point2D(event.getSceneX(), event.getSceneY()));
                mainContainer.getScene().setCursor(Cursor.HAND);
            };
        });

        mainContainer.setOnMouseReleased(new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(final MouseEvent arg0)
            {
                mouseKoordinates.set(null);
                mainContainer.getScene().setCursor(Cursor.DEFAULT);
            }
        });

        mainContainer.setOnMouseDragged(new EventHandler<MouseEvent>()
        {
            @Override
            public void handle(MouseEvent event)
            {
                if (mouseKoordinates.get() != null)
                {
                    double x = event.getScreenX();
                    double deltaX = x - mouseKoordinates.get().getX();
                    double y = event.getScreenY();
                    double deltaY = y - mouseKoordinates.get().getY();
                    mainSt.setX(mainSt.getX() + deltaX);
                    mainSt.setY(mainSt.getY() + deltaY);
                    mouseKoordinates.set(new Point2D(x, y));
                }
            }
        });
    }

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

【问题讨论】:

    标签: java javafx draggable stage vbox


    【解决方案1】:

    您不得在代码中使用getScreenX()getScreenY(),因为它们会返回绝对位置。代替他们尝试使用getX()getY()

    另外,我不明白你为什么还要使用mouseKoordinates.set(new Point2D(x, y));

    试试下面的代码

    mainContainer.setOnMouseDragged(new EventHandler<MouseEvent>()
    {
        @Override
        public void handle(MouseEvent event)
        {
            if (mouseKoordinates.get() != null)
            {
                double x = event.getX();
                double deltaX = x - mouseKoordinates.get().getX();
                double y = event.getY();
                double deltaY = y - mouseKoordinates.get().getY();
                mainSt.setX(mainSt.getX() + deltaX);
                mainSt.setY(mainSt.getY() + deltaY);
            }
        }
    });
    

    【讨论】:

    • 您好 Itachi,如果我尝试您的示例,它比我的示例效果更好,但是它会闪烁,舞台不是固定在一个地方,它会在其中两个之间闪烁。我该如何解决?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多