简体   繁体   English

Javafx - On intelliJ with gradle error: package javafx.fxml does not exist import javafx.fxml.FXML

[英]Javafx - On intelliJ with gradle error: package javafx.fxml does not exist import javafx.fxml.FXML

I don't understand why I have always the same error:我不明白为什么我总是有同样的错误:

image图片

when I try to add library (fx java) on my strcuture project.当我尝试在我的结构项目中添加库(fx java)时。

My build.gradle:我的 build.gradle:

plugins {
    id 'java'
    id 'application'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}


application {
mainClassName = 'Launcher'
}

adding dependencies this way is bad.以这种方式添加依赖项是不好的。 you're using gradle so let it do the work.你正在使用 gradle 所以让它完成工作。 once gradle is resolved all dependencies it will cached each on your system.一旦 gradle 解决了所有依赖项,它将缓存在您的系统上。 so you don't need to add external libraries with a folder(lib) for each project.所以您不需要为每个项目添加带有文件夹(lib)的外部库。 gradle will resolve that for you from it's local cache. gradle 将从它的本地缓存中为您解决这个问题。 so get rid of that lib folder and replace your build.gradle file contents with this所以摆脱那个lib文件夹并用这个替换你的build.gradle文件内容

plugins {
    id 'java'
    id 'application'
    id 'org.openjfx.javafxplugin' version '0.0.9'
}

application {
    //Note: Check package name. replace it with right one if it's wrong
    mainClassName='home.Launcher'
}

javafx {
    version='11.0.2'
    modules=['javafx.controls', 'javafx.fxml']
}

repositories {
    mavenCentral()
}

also you're imported packages from awt not from javafx.你也是从awt而不是从javafx导入的包。 replace those eg: java.awt.event.ActionEvent to javafx.event.ActionEvent替换那些例如: java.awt.event.ActionEvent 到 javafx.event.ActionEvent

Happy coding:)快乐编码:)

There isn't enough to go on to fully answer this, you will find you will get help quicker if you follow the rules about posting questions, including the most important, being a https://stackoverflow.com/help/minimal-reproducible-example go 不足以完全回答这个问题,如果您遵守发布问题的规则,您会发现您会更快地获得帮助,包括最重要的问题,成为https://stackoverflow.com/help/minimal-reproducible -例子

We don't know what version of gradle or javafx you are using.我们不知道您使用的是什么版本的 gradle 或 javafx。

I'm guessing you don't have the JavaFX library on the classpath or in the project libraries...我猜你在类路径或项目库中没有 JavaFX 库......

Post stacktraces as code, not as an image link, no one wants to click on links.将堆栈跟踪作为代码发布,而不是图像链接,没有人愿意点击链接。

But from the Stacktrace, it can't find package javafx.fxml.但是从 Stacktrace 中找不到 package javafx.fxml。

I find it easiest when using gradle, to use the javafx plugin.我发现使用 gradle 时使用 javafx 插件最容易。 When using the plugin, mainClassName goes in the javafx function, if you read my coments within the javafx function, you will see how to set the JAVAFX_HOME and PATH_TO_FX system variables, you will need to edit this gradle file obviously to match your setup. When using the plugin, mainClassName goes in the javafx function, if you read my coments within the javafx function, you will see how to set the JAVAFX_HOME and PATH_TO_FX system variables, you will need to edit this gradle file obviously to match your setup.

Also, mainClassName is the full package with class name, so I assume in your case it should be org.example.Launcher此外, mainClassName是完整的 package 与 class 名称,所以我假设在你的情况下它应该是org.example.Launcher

plugins {
    id 'org.openjfx.javafxplugin' version '0.0.9'
}


apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'application'


group 'org.example'
version '1.0-SNAPSHOT'

javafx {
    version = "14.0.2.1"

    /*******************************************************************************************************************
     *
     *   Set an environment variable pointing to the location of the JavaFX SDK!
     *   JAVAFX_HOME is the sdk directory
     *   PATH_TO_FX  is the sdk/lib directory
     *
     *   ie: for Windows : Open an Administrator Command Prompt
     *      To Permanently set an environment variable for the current user:
     *             C:\> setx JAVAFX_HOME "C:\bin\Java\javafx-sdk-14.0.1"
     *             C:\> setx PATH_TO_FX "C:\bin\Java\javafx-sdk-14.0.1/lib"
     *
     *      Or, alternatively to Permanently set global environment variable (for all users):
     *             C:\> setx /M JAVAFX_HOME "C:\bin\Java\javafx-sdk-14.0.1"
     *             C:\> setx /M PATH_TO_FX "C:\bin\Java\javafx-sdk-14.0.1/lib"
     *
     *   ie: for MacOS  : Open a Terminal
     *     If using bash:
     *             echo 'export JAVAFX_HOME=/Library/Java/JavaVirtualMachines/javafx-sdk-14.0.1' >> ~/.bash_profile
     *             echo 'export PATH_TO_FX=/Library/Java/JavaVirtualMachines/javafx-sdk-14.0.1/lib' >> ~/.bash_profile
     *
     *     If using zsh:
     *             echo 'export JAVAFX_HOME=/Library/Java/JavaVirtualMachines/javafx-sdk-14.0.1' >> ~/.zshrc
     *             echo 'export PATH_TO_FX=/Library/Java/JavaVirtualMachines/javafx-sdk-14.0.1/lib' >> ~/.zshrc
     *
     *******************************************************************************************************************/

    sdk = System.getenv('JAVAFX_HOME')
    if (sdk == null || sdk.isBlank()) {
        throw new InvalidUserDataException("JAVAFX_HOME environment variable is not set. It must be set to root folder where JAVAFX SDK is located")
    }
    modules = ['javafx.base', 'javafx.graphics', 'javafx.controls', 'javafx.fxml', 'javafx.web', 'javafx.swing']
    mainClassName = 'org.example.Launcher'
}

application {
    applicationDefaultJvmArgs = [
            "--add-opens=javafx.graphics/com.sun.javafx.css=ALL-UNNAMED",
            "--add-opens=javafx.graphics/com.sun.prism=ALL-UNNAMED",
            "--add-opens=javafx.graphics/com.sun.prism.sw=ALL-UNNAMED"
    ]
}

repositories {
    mavenCentral()
}


Hope this helps, and gets you pointed in the right direction... It really helps if you read the docs for JavaFX including how to set it up for IntelliJ and Gradle... all the info is really in there, and if more people actually read it, we wouldn't have to keep answering these kinids of questions, We have all read it, that's what it's there for!希望这会有所帮助,并让您指出正确的方向...如果您阅读 JavaFX 的文档,包括如何为 IntelliJ 和 Gradle 设置它,这真的很有帮助...所有信息都在那里,如果更多人实际阅读它,我们不必一直回答这些问题,我们都阅读了它,这就是它的用途!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM