首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在使用Maven构建的Swing应用程序中运行JavaFx时缺少SwingInterOpUtils

在使用Maven构建的Swing应用程序中运行JavaFx时缺少SwingInterOpUtils
EN

Stack Overflow用户
提问于 2021-08-26 08:33:10
回答 1查看 256关注 0票数 1

我有一个Swing应用程序,我想将JavaFx集成到它中。当从Eclipse启动时,应用程序运行良好。

下面是主类:

代码语言:javascript
运行
复制
package org.openjfx.hellofx;

import java.awt.BorderLayout;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javax.swing.JFrame;

public class AppRun {

   public static void main(String[] args) {
      JFrame f = new JFrame("TEST");
      f.setSize(400, 400);;
      f.getContentPane().setLayout(new BorderLayout());
      JFXPanel panel = new JFXPanel();
      Scene s = new Scene(new javafx.scene.control.Label("JavaFx"));
      panel.setScene(s);
      f.getContentPane().add(panel);
      f.setVisible(true);
   }
}

必要的依赖关系在Maven pom.xml中定义:

代码语言:javascript
运行
复制
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.openjfx</groupId>
    <artifactId>hellofx</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.release>11</maven.compiler.release>
        <javafx.version>16</javafx.version>
        <javafx.maven.plugin.version>0.0.6</javafx.maven.plugin.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>${javafx.version}</version>
        </dependency>
                <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-graphics</artifactId>
            <version>${javafx.version}</version>
            <classifier>win</classifier>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-swing</artifactId>
            <version>${javafx.version}</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>${javafx.version}</version>
        </dependency>
        <dependency>
      <groupId>org.openjfx</groupId>
      <artifactId>javafx-base</artifactId>
      <version>${javafx.version}</version>
    </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <release>${maven.compiler.release}</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>${javafx.maven.plugin.version}</version>
                <configuration>
                    <mainClass>org.openjfx.hellofx.App</mainClass>
                </configuration>
            </plugin>

            <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>3.2.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer
                  implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>org.openjfx.hellofx.AppRun</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
        </plugins>
    </build>
</project>

但当我构建并启动一个Fat Jar时,它不起作用:

代码语言:javascript
运行
复制
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: jdk/swing/interop/SwingInterOpUtils
        at com.sun.javafx.embed.swing.newimpl.JFXPanelInteropN.isUngrabEvent(JFXPanelInteropN.java:40)
        at javafx.embed.swing.JFXPanel.lambda$new$6(JFXPanel.java:834)
        at java.desktop/java.awt.Toolkit$SelectiveAWTEventListener.eventDispatched(Unknown Source)
        at java.desktop/java.awt.Toolkit.notifyAWTEventListeners(Unknown Source)
        at java.desktop/java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.desktop/java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.desktop/java.awt.Window.dispatchEventImpl(Unknown Source)

缺少的文件'SwingInterOpUtils‘在模块'jdk.unsupported.dekstop’中。而且这不是标准JRE的一部分。那么,有没有可能使用Maven构建来运行应用程序呢?

EN

回答 1

Stack Overflow用户

发布于 2021-08-26 09:19:57

我不知道您是否正在创建一个模块化的应用程序,但我认为您应该考虑使用javafx-maven-plugin而不是maven-shade-plugin来生成您的应用程序的自定义本机映像。您已经包含了插件,只需运行mvn clean javafx:jlink即可。查看插件的GitHub自述文件以了解配置详细信息:https://github.com/openjfx/javafx-maven-plugin

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68935160

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档