【发布时间】:2021-08-26 08:33:10
【问题描述】:
我有一个 Swing 应用程序,我想将 JavaFx 集成到它。 从 Eclipse 启动时应用程序运行良好。
这里是主类:
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 中定义:
<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 时它不起作用:
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 构建运行?
【问题讨论】:
-
FWIW,我无法复制这个。我尝试在 Mac 上使用 Java 16。我删除了 Windows 特定的
javafx-graphics依赖项,因为其他依赖项已经为 Mac 引入了正确的图形依赖项。我删除了未使用的 FXML 依赖项。我使用mvn install创建了jar,并使用java -jar hellofx-1.0-SNAPSHOT.jar运行了应用程序。除了警告之外,一切都运行良好。 -
显示的警告是
WARNING: Unsupported JavaFX configuration: classes were loaded from 'unnamed module @28c71909'。建议将 JavaFX 用作模块集,不在 jar 中着色,请参阅:https://bugs.openjdk.java.net/browse/JDK-8256362。该 JDK 问题是向 JavaFX 库(在 JavaFX 16 中完成)添加警告,以表明这不是推荐的运行配置,问题不是报告实际错误。跨度> -
顺便说一句:除非有充分的理由,否则不要混合使用 Swing 和 Java-FX。
JFrame的 Java-FX 等效项是Stage。
标签: java maven javafx noclassdeffounderror javafx-11