如何在 IntelliJ IDEA 中创建 JavaFX Maven 项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30488223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:43:01  来源:igfitidea点击:

How to create a JavaFX Maven project in IntelliJ IDEA?

javamavenintellij-ideajavafx

提问by fatCop

How can I open a JavaFX Maven project from scratch in IntelliJ IDEA? As there is no difference between a Java project and a JavaFx project, I want to open a dedicated JavaFX project (Selection JavaFX from create project Wizard) with Maven module included.

如何在 IntelliJ IDEA 中从头开始打开 ​​JavaFX Maven 项目?由于 Java 项目和 JavaFx 项目之间没有区别,我想打开一个包含 Maven 模块的专用 JavaFX 项目(从创建项目向导中选择 JavaFX)。

采纳答案by Menefee

Although dated I'm answering this because I had the same question recently and too many people told me to write my own pom file etc. While that is technically true, it can create more headaches if you aren't careful.

虽然过时了,但我正在回答这个问题,因为我最近有同样的问题,而且太多人告诉我编写自己的 pom 文件等。虽然这在技术上是正确的,但如果你不小心,它会造成更多的麻烦。

I recommend you:

我推荐你:

  1. Create a JavaFX project as you normally would.
  2. Make sure that project view (dropdown in project structure side tool window) is set to 'Project' or 'Packages' (otherwise option in 4th step will not be visible)
  3. Then once it is opened and ready right click on the project folder
  4. Go to "Add Framework Support...".
  5. Check the box for Maven and click "OK".
  1. 像往常一样创建 JavaFX 项目。
  2. 确保项目视图(项目结构侧工具窗口中的下拉菜单)设置为“项目”或“”(否则第 4 步中的选项将不可见)
  3. 然后一旦它打开并准备好右键单击项目文件夹
  4. 转到“添加框架支持...”。
  5. 选中 Maven 框,然后单击“确定”。

Let IntelliJ do the work for you at this point.

此时让 IntelliJ 为您完成工作。

As for editing FXML files link the IDEA to SceneBuilder. Hereis the official documentation. But basically:

至于编辑 FXML 文件,将 IDEA 链接到 SceneBuilder。 这里是官方文档。但基本上:

  1. Install Scene Builder.
  2. File -> Settings -> Languages & Frameworks -> JavaFX
  3. Point to the exe file.
  1. 安装场景生成器
  2. 文件 -> 设置 -> 语言和框架 -> JavaFX
  3. 指向exe文件。

To Use: Right click on the FXML file and select "Open In SceneBuilder" (way down at the bottom)

使用方法:右键单击 FXML 文件并选择“在 SceneBuilder 中打开”(在底部向下)

回答by Ernest

This steps worked for me (Is a different process): 1. Create a maven project. 2. In the generated pom.xml add the following:

这些步骤对我有用(是一个不同的过程): 1. 创建一个 Maven 项目。2. 在生成的 pom.xml 中添加以下内容:

`<build>
        <plugins>
            <plugin>
                <groupId>com.zenjava</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>8.7.0</version>
                <configuration>
                    <mainClass>your.package.with.mainclass</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>`

This adds a maven plugin for javafx (more info: https://github.com/javafx-maven-plugin/javafx-maven-plugin)

这为 javafx 添加了一个 maven 插件(更多信息:https: //github.com/javafx-maven-plugin/javafx-maven-plugin

  1. Create in your resources folder your sample.fxml
  2. Create your controller class for sample.fxml and link
  3. Enjoy!
  1. 在您的资源文件夹中创建您的 sample.fxml
  2. 为 sample.fxml 创建控制器类并链接
  3. 享受!

回答by SSpade

And after all, a program doesn't compiled. So this video helped me: Configuring Maven For Java FX

毕竟,程序不会编译。所以这个视频帮助了我:为 Java FX 配置 Maven

in words: we need to move sample.fxml to /resources folder. And in Main.java write:

换句话说:我们需要将 sample.fxml 移动到 /resources 文件夹。在 Main.java 中写:

FXMLLoader loader=new FXMLLoader(getClass().getResource("/sample.fxml"));

回答by Cong

It takes me a couple of hours to find the right solution. Many thanks to this video and the author: https://www.youtube.com/watch?v=qn2tbftFjno

我需要几个小时才能找到正确的解决方案。非常感谢这个视频和作者:https: //www.youtube.com/watch?v=qn2tbftFjno

So, following the above video, I would like to recommend:

所以,根据上面的视频,我想推荐:

  1. Download JavaFX SDK from https://gluonhq.com/products/javafx/
  2. Extract the zip file
  3. From the starting page of Intellij IDEA, select Configure/Structure for New Projects
  4. Select Platform Settings/Global Libraries
  5. Select the plus ("+") button/Java
  6. Find the "lib" folder inside your extracted zip file and select all of ".jar" files (except "src.zip"), and then OK
  7. Set a new name for the library, let's say "javafx-whatever-version", then Apply/OK
  8. Create a JavaFX project as normally.
  9. Select File/Project Structure...
  10. From Project Settings/Project, remember to select the suitable Project SDK and Project language level
  11. From Platform Settings/Global Libraries, right click on the library "javafx-whatever-version" (which you have already added), choose Add to modules.../OK
  12. Switch to Project Settings/Modules, choose Apply/OK
  13. Back to your project manager, right click on "src"/New/module-info.java
  14. Add new lines to the body of module-info.java:
  1. https://gluonhq.com/products/javafx/下载 JavaFX SDK
  2. 解压压缩文件
  3. 从 Intellij IDEA 的起始页面,选择 Configure/Structure for New Projects
  4. 选择平台设置/全局库
  5. 选择加号(“+”)按钮/Java
  6. 在解压后的 zip 文件中找到“lib”文件夹并选择所有“.jar”文件(“src.zip”除外),然后确定
  7. 为库设置一个新名称,比如说“javafx-whatever-version”,然后应用/确定
  8. 照常创建 JavaFX 项目。
  9. 选择文件/项目结构...
  10. 从项目设置/项目中,记得选择合适的项目 SDK 和项目语言级别
  11. 从平台设置/全局库中,右键单击库“javafx-whatever-version”(您已经添加),选择添加到模块.../确定
  12. 切换到项目设置/模块,选择应用/确定
  13. 回到您的项目经理,右键单击“src”/New/module-info.java
  14. 在 module-info.java 的正文中添加新行:
requires javafx.fxml; 
requires javafx.controls; 
opens sample;
  1. Run the project and enjoy!
  1. 运行项目并享受!