如何在 SWT Java 8 中使用 FXCanvas

rho*_*ell 7 java swt javafx maven

我正在尝试使用 FXCanvas 将 JavaFX 集成到 SWT 应用程序中。作为参考,我正在关注这个 oracle 指南

在我的 IDE 中,这段代码显示错误

/* Create an FXCanvas */
final FXCanvas fxCanvas = new FXCanvas(shell, SWT.NONE) {

    @Override
    public Point computeSize(int wHint, int hHint, boolean changed) {
        getScene().getWindow().sizeToScene();
        int width = (int) getScene().getWidth();
        int height = (int) getScene().getHeight();
        return new Point(width, height);
    }
};
Run Code Online (Sandbox Code Playgroud)

错误:

(是的,我导入了正确的 Point 类:)org.eclipse.swt.graphics.Point

'computeSize(int, int, boolean)' in 'Anonymous class derived from javafx.embed.swt.FXCanvas' clashes with 'computeSize(int, int, boolean)' in 'javafx.embed.swt.FXCanvas'; attempting to use incompatible return type
Run Code Online (Sandbox Code Playgroud)

但是,我不认为这是根本原因......因为当我尝试构建项目(maven)时,我收到此错误:

package javafx.embed.swt does not exist. 我相信这是真正的问题。

因此,在做了一些研究之后,我检查了一些事情,首先,jfxswt jar 看起来应该可以访问:

在此处输入图片说明

如果我打开 JAR,您可以看到 FXCanvas 就在那里: 在此处输入图片说明

我什至尝试将 JAR 作为库手动添加到我的模块中,但仍然不起作用。

这是我的 pom.xml,(我有意将某些信息匿名化)

我还将提到我在一个没有任何 maven 依赖项的新项目中尝试了这个,并且只是手动添加了 swt 和诸如库之类的库,但出现了相同的错误。

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId></groupId>
        <artifactId></artifactId>
        <version></version>
    </parent>

    <artifactId></artifactId>
    <version>2.0.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name></name>
    <description></description>

    <dependencies>
        <dependency>
            <groupId>pentaho-kettle</groupId>
            <artifactId>kettle-engine</artifactId>
        </dependency>
        <dependency>
            <groupId>pentaho-kettle</groupId>
            <artifactId>kettle-core</artifactId>
        </dependency>
        <dependency>
            <groupId>pentaho-kettle</groupId>
            <artifactId>kettle-ui-swt</artifactId>
        </dependency>
        <dependency>
            <groupId>org.eclipse.swt</groupId>
            <artifactId>org.eclipse.swt.gtk.linux.x86_64</artifactId>
        </dependency>
        <dependency>
            <groupId>org.eclipse</groupId>
            <artifactId>jface</artifactId>
            <version>3.3.0-I20070606-0010</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
    </dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)

我错过了什么,有什么想法吗?

小智 1

您需要将该jfxswt.jar文件添加到类路径中以进行编译和执行。

这可以通过使用系统范围来完成,因为该 jar 文件是该jre/lib目录下 JDK 的一部分。

<dependency>
    <!-- GroupId/ArtifactId/Version doesn't matter unless it clashes. -->
    <groupId>jfxswt</groupId>
    <artifactId>jfxswt</artifactId>
    <version>1.8</version>
    <scope>system</scope>
    <systemPath>${java.home}/lib/jfxswt.jar</systemPath>
</dependency>
Run Code Online (Sandbox Code Playgroud)

这将指示 maven 将其添加jre/lib/jfxswt.jar到类路径中。如果有人使用不同的 JDK(在其他地方有该 jar),这可能会导致问题,但对于 Java 8 来说应该没问题。

jfxswt.jar执行应用程序时,必须将其添加到类路径中。

在maven中你可以使用exec插件:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <goals>
                <goal>java</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <mainClass>twobuttons.TwoButtons</mainClass>
        <additionalClasspathElements>
            <!-- The run environment must configure the system jar. -->
            <element>${java.home}/lib/jfxswt.jar</element>
        </additionalClasspathElements>
    </configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)

注意事项

maven 中不推荐使用系统范围,而是将文件安装到存储库。上面的解决方案目前工作正常。

不幸的是,该内容jfxswt.jar可以是某些 SWT 库的一部分,我对 SWT 不熟悉。如果您可以在 Maven 存储库中找到该 jar,那么只需包含它即可,而不会扰乱系统范围。

twobuttons.TwoButtons课程来自您提到的教程。