簡體   English   中英

如何在 SWT 中使用 FXCanvas Java 8

[英]How to use FXCanvas with SWT Java 8

我正在嘗試使用 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);
    }
};

錯誤:

(是的,我導入了正確的 Point class: 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

但是,我不認為這是根本原因...因為當我嘗試構建項目 ( 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>

我錯過了什么,有什么想法嗎?

您需要將jfxswt.jar文件添加到類路徑中以進行編譯和執行。

這可以通過使用系統 scope 來完成,因為 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>

注意事項

系統 scope 在 maven 中被棄用,支持將文件安裝到存儲庫。 上面的解決方案目前工作正常。

jfxswt.jar的內容可能是某些 SWT 庫的一部分,不幸的是我不熟悉 SWT。 如果你能在 Maven 回購中找到 jar 而不是只包含它而不是弄亂系統 scope。

twobuttons.TwoButtons class 來自您提到的教程。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM