1

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

如何拖动未装饰的窗口

所以我设置mousePressedMouseDragged事件,stackPane但只有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();

    }

}
4

2 回答 2

2

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

证明: 在此处输入图像描述

正确的代码

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 的间隙,如果单击,则会触发鼠标事件,从而解决问题..

于 2016-02-17T19:35:13.340 回答
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;
  }
})
于 2019-03-07T04:50:50.790 回答