【问题标题】:Problem with webEngine JavaFX using with swing与 swing 一起使用的 webEngine JavaFX 问题
【发布时间】:2021-01-09 15:34:59
【问题描述】:

我正在尝试使用 JavaFX WebEnginge 加载浏览器以如下所示摆动

SwingUtilities.invokeLater(() -> InitGUI());
final JFXPanel jfxPanel = new JFXPanel();
panel.add(jfxPanel);

Platform.runLater(() -> {
    WebView webView = new WebView();
    jfxPanel.setScene(new Scene(webView));
    webView.getEngine().load("https://pl.wikipedia.org/wiki/London");
});

网站在我的 Swing 应用程序中显示,但是当我在加载的网站上移动鼠标时,IntelliJ 抛出

Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: 'void com.sun.javafx.embed.EmbeddedSceneInterface.mouseEvent(int, int, boolean, boolean, boolean, int, int, int, int, boolean, boolean, boolean, boolean, boolean)'
    at javafx.embed.swing.JFXPanel.sendMouseEventToFX(JFXPanel.java:426)
    at javafx.embed.swing.JFXPanel.processMouseEvent(JFXPanel.java:465)
    at java.desktop/java.awt.Component.processEvent(Component.java:6401)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4919)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEnterExit(Container.java:4691)
    at java.desktop/java.awt.LightweightDispatcher.trackMouseEnterExit(Container.java:4677)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4528)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4489)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2764)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

我应该在某处添加 mouseEventListeners 吗?整个 GUI 在我的 Main 类中,没有任何按钮,只有 JLabels 和 javaFX 的这个 webView。每次我移动鼠标/单击时,只会在此 webView 上发生错误。其他摆动组件不会抛出任何东西。

@编辑 我还没有发布第一行异常

Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: 'void com.sun.javafx.embed.EmbeddedSceneInterface.mouseEvent(int, int, boolean, boolean, boolean, int, int, int, int, boolean, boolean, boolean, boolean, boolean)'

【问题讨论】:

  • 确保在编译和运行时路径上都有所有必要的模块(控件、swing、fxml、web)。如果是这样并且仍然卡住,请提供 minimal reproducible example 来演示问题。无关:java命名约定,请
  • 对我来说很好。我在 Windows 10(64 位)上使用来自 OpenJFX 和 Eclipse 2020-09(4.17.0)的 Oracle JDK 15 和 JavaFX 15

标签: java swing javafx


【解决方案1】:

问题已解决

我已经添加了

--module-path /Users/<user>/javafx-sdk-15/lib --add-modules=javafx.controls,javafx.fxml,javafx.web,javafx.swing

在运行>编辑配置>虚拟机选项

【讨论】:

    【解决方案2】:

    添加模块并没有解决我的问题——我已经添加了它们。

    解决方案

    没有在 JavaFX Gradle Plugin 中指定 JavaFX 版本为我解决了这个问题。

    javafx {
        version = "11.0.1" // ← I removed this line
        modules = [ 'javafx.controls', ...
    }
    

    详情

    我试图使用CalendarFX;原来我尝试使用的版本 11.8.3 使用 JavaFX 13.0.1。

    一旦我升级或删除了我所包含的 JavaFX 版本,异常就消失了。

    完全异常

    Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: 'void com.sun.javafx.embed.EmbeddedSceneInterface.mouseEvent(int, int, boolean, boolean, boolean, int, int, int, int, boolean, boolean, boolean, boolean, boolean)'
        at javafx.swing/javafx.embed.swing.JFXPanel.sendMouseEventToFX(JFXPanel.java:426)
        at javafx.swing/javafx.embed.swing.JFXPanel.processMouseEvent(JFXPanel.java:465)
        at java.desktop/java.awt.Component.processEvent(Component.java:6400)
        at java.desktop/java.awt.Container.processEvent(Container.java:2263)
        at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5011)
        at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
        at java.desktop/java.awt.Component.dispatchEvent(Component.java:4843)
        at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
        at java.desktop/java.awt.LightweightDispatcher.retargetMouseEnterExit(Container.java:4698)
        at java.desktop/java.awt.LightweightDispatcher.trackMouseEnterExit(Container.java:4676)
        at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4527)
        at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
        at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
        at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772)
        at java.desktop/java.awt.Component.dispatchEvent(Component.java:4843)
        at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
        at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
        at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
        at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
        at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
        at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
        at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
        at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
    

    【讨论】:

      【解决方案3】:

      我正在使用 Maven 构建,我的解决方案也与依赖项的版本有关,但我没有删除版本,而是将每个 javafx 库升级到最新的可用版本。在此之前使用的是导致问题的旧版本,所以在pom.xml 中我编辑了&lt;version&gt; 标签。

      之前:

              <dependency>
                  <groupId>org.openjfx</groupId>
                  <artifactId>javafx-controls</artifactId>
                  <version>13</version>
              </dependency>
              <dependency>
                  <groupId>org.openjfx</groupId>
                  <artifactId>javafx-fxml</artifactId>
                  <version>13</version>
              </dependency>
              <dependency>
                  <groupId>org.openjfx</groupId>
                  <artifactId>javafx-swing</artifactId>
                  <version>11</version>
              </dependency>
              <dependency>
                  <groupId>org.openjfx</groupId>
                  <artifactId>javafx-base</artifactId>
                  <version>13</version>
              </dependency>
              <dependency>
                  <groupId>org.openjfx</groupId>
                  <artifactId>javafx-graphics</artifactId>
                  <version>13</version>
      

      之后:

              <dependency>
                  <groupId>org.openjfx</groupId>
                  <artifactId>javafx-controls</artifactId>
                  <version>17.0.1</version>
              </dependency>
              <dependency>
                  <groupId>org.openjfx</groupId>
                  <artifactId>javafx-fxml</artifactId>
                  <version>17.0.1</version>
              </dependency>
              <dependency>
                  <groupId>org.openjfx</groupId>
                  <artifactId>javafx-swing</artifactId>
                  <version>17.0.1</version>
              </dependency>
              <dependency>
                  <groupId>org.openjfx</groupId>
                  <artifactId>javafx-base</artifactId>
                  <version>17.0.1</version>
              </dependency>
              <dependency>
                  <groupId>org.openjfx</groupId>
                  <artifactId>javafx-graphics</artifactId>
                  <version>17.0.1</version>
              </dependency>
      

      【讨论】:

        猜你喜欢
        • 2018-07-19
        • 2014-06-11
        • 1970-01-01
        • 2018-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-01
        • 2013-07-07
        相关资源
        最近更新 更多