VBox拖动闪烁

Posted

技术标签:

【中文标题】VBox拖动闪烁【英文标题】:VBox Dragging flickers 【发布时间】:2015-02-07 02:41:58 【问题描述】:

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

我创建了以下代码,现在我可以拖动舞台了。但是舞台是闪烁的,所以它不会停留在一个地方,它会在一些像素的半径范围内闪烁。我怎么解决这个问题?感谢您的帮助。

示例 - 代码:

        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.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);
//                  mouseKoordinates.set(new Point2D(x, y));
                
            
        );
    

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

【问题讨论】:

如我的回答中所述,您不应该使用mouseKoordinates.set(new Point2D(x, y));。只需将其删除。 @ItachiUchiha 但是它不起作用......你试过了吗......对我来说,当我用 mouseKoordinates.set(...) 删除该行时它不起作用 是的,它有效。你只需要从 setOnMouseDragged(...) 中删除它,而不是从 setOnMousePressed(...) @ItachiUchiha 我试了一下,但它不起作用,我的代码看起来像问题文本中的那个。请检查一下。谢谢。 【参考方案1】:

所以我修复了它,它不起作用,因为我们有错误的参考!以下代码完美运行!需要通过将鼠标按到 ScreenX/Y 值而不是 X/Y 值来设置鼠标坐标!设置新点也很重要!

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) 
                //Important to create the new Point with a Reference to the Screen!
                mouseKoordinates.set(new Point2D(event.getScreenX(), event
                        .getScreenY()));
                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) 
                    //getScreenX and not getX
                    double x = event.getScreenX();
                    double deltaX = x - mouseKoordinates.get().getX();
                    //getScreenY and not getY
                    double y = event.getScreenY();
                    double deltaY = y - mouseKoordinates.get().getY();
                    mainSt.setX(mainSt.getX() + deltaX);
                    mainSt.setY(mainSt.getY() + deltaY);
                    //VERY IMPORANT
                    mouseKoordinates.set(new Point2D(x, y));
                
            
        );
    

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

【讨论】:

以上是关于VBox拖动闪烁的主要内容,如果未能解决你的问题,请参考以下文章

可拖动的 NSWindow 闪烁

UIView锚点->拖动->旋转变换导致闪烁

在第一代 3.1.3 上拖动 uiScrollview 时,uiSlider 拇指图形“闪烁”

解决CListCtrl闪烁及水平滚动条不能跟踪拖动的问题(MFC)

制作我自己的“可拖动”功能

PanGestureRecognizer 在拖动过程中零星出现