【问题标题】:How to use FXCanvas with SWT Java 8如何在 SWT Java 8 中使用 FXCanvas
【发布时间】:2020-12-02 21:18:06
【问题描述】:

我正在尝试使用 FXCanvas 将 JavaFX 集成到 SWT 应用程序中。供参考,我关注this oracle guide

在我的 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);
    }
};

错误:

(是的,我已经导入了正确的 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

但是,我认为这不是根本原因...因为当我尝试构建项目 (ma​​ven) 时出现此错误: p>

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>

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

【问题讨论】:

  • 你能分享你的pom.xml吗? (我假设它是一个 Maven 项目,因为你用它来标记它。)
  • @Gebezs 在原始帖子中添加了 pom 和更多信息。
  • pom.xml 定义的 java 版本是什么?它是在父级中定义的吗?我正在寻找 maven.compiler.targetmaven.compiler.source 值以及 maven-compiler-plugin 配置。因为这些类应该包含在 jdk 中,并且零依赖模块也会发生同样的错误,这是我的最佳猜测。 mvn help:effective-pom 应该输出合并的配置。
  • @Gebezs &lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt; &lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt; 两者都设置为 1.8,我的 jdk 也是如此

标签: java maven javafx swt


【解决方案1】:

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

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

<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>

这将指示 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>

注意事项

系统范围在 maven 中已弃用,有利于将文件安装到存储库。上述解决方案目前运行良好。

jfxswt.jar 的内容可以是一些 SWT 库的一部分,遗憾的是我不熟悉 SWT。如果您可以在 Maven 存储库中找到该 jar,而不仅仅是包含它而不是弄乱系统范围。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-02
    • 2017-09-07
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    相关资源
    最近更新 更多