【问题标题】:How to install javafx to Eclipse如何将 javafx 安装到 Eclipse
【发布时间】:2020-02-22 08:54:52
【问题描述】:

首先,我是 Eclipse 和 Java 的新手。 所以,我的问题是,我无法将 javafx 安装到我的 Eclipse 中。 我安装了什么: https://www.oracle.com/java/technologies/javase-jdk13-downloads.html(Windows x64 安装程序) https://www.java.com/de/download/win10.jsp(下载按钮)

我已经尝试过的: 我按照本指南介绍了如何安装 Maven 和 javafx: https://www.vogella.com/tutorials/EclipseMaven/article.html 对于 POM.xml 文件,我使用了此链接中的代码: https://search.maven.org/remotecontent?filepath=org/openjfx/javafx/15-ea+1/javafx-15-ea+1.pom

现在,在创建一个新的 Maven 项目并尝试导入(例如 javafx.scene.shape.Rectangle)后,我收到此错误: (导入jfxrt.jar后) 错误:缺少 JavaFX 运行时组件,需要运行此应用程序

我想运行的示例代码来自我的朋友:

    package vierGewinnt;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Game extends Application{

    public static void main(String[] args){
        launch(args);
    }

    public void start(Stage primaryStage){
        Field field = new Field();

        Button newGame = new Button("Neues Spiel");
        PositionColumn pos = new PositionColumn();



        VBox root = new VBox();
        root.getChildren().addAll(field, pos, newGame);
        Scene scene = new Scene(root);
        scene.addEventHandler(KeyEvent.KEY_RELEASED, new EventHandler<KeyEvent>(){
            @Override
            public void handle(KeyEvent event) {
                /*if(field.victory()){
                    Stage secondaryStage = new Stage();
                    HBox viBox = new HBox();
                    Scene viScene = new Scene(viBox);
                    secondaryStage.setScene(viScene);
                    secondaryStage.show();
                }*/
                if (event instanceof KeyEvent)
                    field.setStone(event);
            }
        });

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

(还有其他类可以使用,这不是问题)

所以,在这一点上,我不知道下一步该怎么做,或者如何让 javafx 以不同的方式工作

【问题讨论】:

标签: eclipse javafx installation


【解决方案1】:

您使用的 JDK 版本 13 不再包含 JavaFX。所以你需要手动将JavaFX添加到你的项目中。

这很容易,因为您使用的是 Maven 项目。所以你需要做的就是:

  • 打开项目的pom.xml 文件(在项目的顶级目录中)
  • 将 javafx 依赖项添加到您的 maven 依赖项中
  • (也许)刷新你的 maven 项目(Alt + F5)让 Eclipse 注意到变化

要将 maven 依赖项添加到您的项目中,您只需将其添加到 pom.xml 文件中(位于标签 &lt;dependencies&gt;&lt;/dependencies&gt; 之间:

<dependency>
    <groupId>org.openjfx</groupId>
    <artifactId>javafx</artifactId>
    <version>11</version>
    <type>pom</type>
</dependency>


编辑

回答您关于启动应用程序时发生的异常的第二个问题:

StackTrace 说:

引起:java.lang.IllegalAccessException:类 com.sun.javafx.application.LauncherImpl(在模块 javafx.graphics 中) 无法访问类 vierGewinnt.Game(在模块中 org.openjfx.archetype_simple) 因为模块 org.openjfx.archetype_simple 不导出 vierGewinnt 到模块 javafx.graphics

所以问题在于类 vierGewinnt.Game 无法创建,因为它无法访问。那是因为它没有导出。如果您添加导出,它应该可以工作。

【讨论】:

    【解决方案2】:

    当我从 @Tobias 添加依赖项时,出现以下错误:

    应用程序构造函数中的异常 线程“主”java.lang.reflect.InvocationTargetException 中的异常 在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.base/java.lang.reflect.Method.invoke(Method.java:567) 在 java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051) 引起:java.lang.RuntimeException:无法构造应用程序实例:类vierGewinnt.Game 在 javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:890) 在 javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195) 在 java.base/java.lang.Thread.run(Thread.java:830) 原因:java.lang.IllegalAccessException:com.sun.javafx.application.LauncherImpl 类(在模块 javafx.graphics 中)无法访问类 vierGewinnt.Game(在模块 org.openjfx.archetype_simple 中),因为模块 org.openjfx.archetype_simple 没有将 vierGewinnt 导出到模块 javafx.graphics 在 java.base/jdk.internal.reflect.Reflection.newIllegalAccessException(Reflection.java:376) 在 java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:642) 在 java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:490) 在 java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481) 在 javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:802) 在 javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455) 在 javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428) 在 java.base/java.security.AccessController.doPrivileged(AccessController.java:391) 在 javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427) 在 javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96) 在 javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(本机方法) 在 javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174) ... 1 更多

    但是,如果我想运行它,自动创建的“App.java”可以工作:

    package org.openjfx.archetype_simple;
    
    import javafx.application.Application;
    import javafx.fxml.FXMLLoader;
    import javafx.scene.Parent;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    
    import java.io.IOException;
    
    /**
     * JavaFX App
     */
    public class App extends Application {
    
    private static Scene scene;
    
    @Override
    public void start(Stage stage) throws IOException {
        scene = new Scene(loadFXML("primary"), 640, 480);
        stage.setScene(scene);
        stage.show();
    }
    
    static void setRoot(String fxml) throws IOException {
        scene.setRoot(loadFXML(fxml));
    }
    
    private static Parent loadFXML(String fxml) throws IOException {
        FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource(fxml + ".fxml"));
        return fxmlLoader.load();
    }
    
    public static void main(String[] args) {
        launch();
    }
    
    }
    

    我的其他项目位于不同的包中。这可能是问题所在,它在另一个包中吗?

    【讨论】:

    • 这看起来不像是一个答案,是吗?如果没有,请编辑您的问题并将其添加为附加详细信息。如果是这样,请说明它是如何解决问题的(并且不要在其中添加问题,这有点令人困惑:)
    • 就像@kleopatra 所说,最好将此问题作为问题而不是作为答案发布(您可以编辑您的问题以添加此内容)。但是,我编辑了我以前的答案也回答了这个问题。希望这能修复错误。
    猜你喜欢
    • 1970-01-01
    • 2021-02-16
    • 1970-01-01
    • 2014-11-09
    • 2013-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多