setOnMouseDragged无法在浏览器视图上运行

时间:2016-02-07 21:21:11

标签: java javafx drag jxbrowser

我正在使用JxBrowser引擎在java中编写一个简单的应用程序,但我一开始就陷入困境。在我的代码中,有一个未修饰的stage,我想让它可拖动。为此,搜索并找到以下链接:

How to drag undecorated window

因此,我在mousePressed上设置了MouseDraggedstackPane事件,但只有mousePressed个事件被触发,mouseDragged事件无法被触发。知道问题是什么吗?

提前致谢。

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.javafx.BrowserView;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

private static double xOffset = 0;
private static double yOffset = 0;

public class Main extends Application {

    public static void main(String[] args) {

        launch(args);

    }

    @Override
    public void start(Stage primaryStage) {

        Platform.setImplicitExit(false);

        Browser browser = new Browser();

        BrowserView browserView = new BrowserView(browser);

        StackPane pane = new StackPane();

        pane.getChildren().add(browserView);

        Scene scene = new Scene(pane, 380, 500);

        primaryStage.initStyle(StageStyle.UNDECORATED);

        primaryStage.setScene(scene);

        pane.setOnMousePressed(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {

                System.out.println("mouse pressed");

                xOffset = primaryStage.getX() - event.getScreenX();

                yOffset = primaryStage.getY() - event.getScreenY();

            }

        });

        pane.setOnMouseDragged(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {

                System.out.println("mouse dragged");

                primaryStage.setX(event.getScreenX() + xOffset);

                primaryStage.setY(event.getScreenY() + yOffset);

            }

        });

        primaryStage.show();

    }

}

2 个答案:

答案 0 :(得分:2)

由于jxbrowser需要许可证我无法测试...所以我用Label替换它并且它工作正常..所以我的猜测是你试图通过点击浏览器本身而不是StackPane来拖动..尝试单击舞台的角落,或者将堆栈窗格添加到VBox并将其设置为它。然后尝试单击角落..如果单击浏览器,则浏览器的鼠标事件将被触发..

证明: enter image description here

正确的代码

package RezRem;

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.javafx.BrowserView;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;


public class Main extends Application {

private static double xOffset = 0;
private static double yOffset = 0;
public static void main(String[] args) {

    launch(args);

}

@Override
public void start(Stage primaryStage) {

    Platform.setImplicitExit(false);


    Browser browser = new Browser();

    BrowserView browserView = new BrowserView(browser);
    StackPane pane = new StackPane();

    pane.getChildren().add(browserView);
    pane.setPadding(new Insets(10,10,10,10));
    Scene scene = new Scene(pane, 380, 500);

    primaryStage.initStyle(StageStyle.UNDECORATED);

    primaryStage.setScene(scene);

    pane.setOnMousePressed(new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent event) {

            System.out.println("mouse pressed");

            xOffset = primaryStage.getX() - event.getScreenX();

            yOffset = primaryStage.getY() - event.getScreenY();

        }

    });

    pane.setOnMouseDragged(new EventHandler<MouseEvent>() {

        @Override
        public void handle(MouseEvent event) {

            System.out.println("mouse dragged");

            primaryStage.setX(event.getScreenX() + xOffset);

            primaryStage.setY(event.getScreenY() + yOffset);

        }

    });
    browser.loadURL("http://www.google.com");
    primaryStage.show();

}

}

<强>原因

jxbrowser延伸到边缘,因此堆栈窗格不在顶部,两侧都不可见,因此鼠标监听器永远不会通过设置堆栈窗格的填充来触发,所有四边都有10px的间隙,如果点击,触发鼠标事件,从而解决问题..

答案 1 :(得分:0)

我遇到了同样的问题。我最终通过在setMouseEventsHandler上使用BrowserView方法解决了这个问题,该方法似乎收到了您需要的所有事件(尽管似乎没有收到MouseEvent.MOUSE_DRAGGED事件,除非将它们重命名为MouseEvent.MOUSE_MOVED个事件)。

Rectangle[] dragIncludeRects = ...; // The area which is draggable (ie the title bar)
Rectangle[] dragExcludeRects = ...; // Exclusions (ie a close button on the title bar)
BrowserView popupView = ...;
JDialog popupFrame = ...; // could also be JFrame
Point dragOffset = null;
popupView.setMouseEventsHandler(new InputEventsHandler<MouseEvent>() {
  public boolean handle(MouseEvent event) {
    switch(event.getID()) {
      case MouseEvent.MOUSE_PRESSED:
        if (
          dragIncludeRects.exists((rect) => rect.contains(event.getPoint())) &&
          !dragExcludeRects.exists((rect) => rect.contains(event.getPoint()))
        ) {
          dragOffset = SwingUtilities.convertPoint(
              popupView, event.getPoint(), popupFrame);
        } else {
          dragOffset = null;
        }
        break;
      case MouseEvent.MOUSE_RELEASED:
        dragOffset = null;
        break;
      case MouseEvent.MOUSE_MOVED:
        if (dragOffset != null) {
          // Note I tried using the position from the event but it doesn't work well
          val position = MouseInfo.getPointerInfo().getLocation();
          popupFrame.setLocation(position.x - offset.x, position.y - offset.y);
        }
    }
    return false;
  }
})