如何将intellij中的普通java项目转换为JavaFx项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23421325/
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-13 22:54:41  来源:igfitidea点击:

How to convert a normal java project in intellij into a JavaFx project

javauser-interfaceintellij-ideajavafx-2

提问by Wizard

I am currently using a normal java project (gradle project) and want to convert this into a JavaFx project without having to reimport the existing sources into a new Javafx project. Is there any way to achieve this?

我目前正在使用一个普通的 java 项目(gradle 项目),并希望将其转换为 JavaFx 项目,而无需将现有源重新导入到新的 Javafx 项目中。有没有办法实现这一目标?

采纳答案by jewelsea

There is nothing to convert.

没有什么可以转换的。

I don't believe there is any difference in Idea between a "normal" Java project and a JavaFX project.

我认为“普通”Java 项目和 JavaFX 项目之间的 Idea 没有任何区别。

Sure, in Idea 12.x+ there is a wizard you can use to create a new JavaFX project. But I think that all it does is create a sample hello world application - after initial creation, the way the project works is not any different than any other Java project.

当然,在 Idea 12.x+ 中有一个向导可以用来创建新的 JavaFX 项目。但我认为它所做的只是创建一个示例 hello world 应用程序 - 在初始创建之后,该项目的工作方式与任何其他 Java 项目没有任何不同。

And this is how it should be, JavaFX is just Java. Oracle doesn't differentiate between JavaFX and Java in their distribution, and neither should IDEs in their project types.

这就是它应该的样子,JavaFX 就是 Java。Oracle 在其发行版中不区分 JavaFX 和 Java,IDE 也不应在其项目类型中区分。

I believe, in this case, even if I'm wrong, that I am right enough that it doesn't really matter if I'm wrong.

我相信,在这种情况下,即使我错了,我也是对的,我是否错了并不重要。

Update

更新

So I was wrong enough that it matters :-)

所以我错了,这很重要:-)

Using Idea 13.1.4, if I create a new project using File | New Project | Java, there are the following resource settings (File | Settings | Compiler):

使用 Idea 13.1.4,如果我使用 创建一个新项目File | New Project | Java,则有以下资源设置 ( File | Settings | Compiler):

?*.properties;?*.xml;?*.gif;?*.png;?*.jpeg;?*.jpg;?*.html;?*.dtd;?*.tld;?*.ftl

That is, the resources for the project are just set to copy only specific file types. So you could modify it to get the additional file types required in some JavaFX projects by adding resource copying support for fxml and css; i.e, appending ;?*.fxml;?*.css.

也就是说,项目的资源只是设置为仅复制特定的文件类型。因此,您可以通过添加对 fxml 和 css 的资源复制支持来修改它以获得某些 JavaFX 项目所需的其他文件类型;即,附加;?*.fxml;?*.css.

The interesting part is that if you create a new project using File | New Project | JavaFX, there are the following resource settings:

有趣的是,如果您使用 新建项目File | New Project | JavaFX,则有以下资源设置:

!?*.java;!?*.form;!?*.class;!?*.groovy;!?*.scala;!?*.flex;!?*.kt;!?*.clj

Essentially, it's copying everything which not a source file, a kind of file blacklist set rather than a file whitelist set as is used by other project creation templates. Really weird... Anyway, the resource sets are user configurable, so you can modify them as you see fit and once you do so, you shouldn't have any issues (I believe, but I've been wrong before ;-)

本质上,它复制了不是源文件的所有内容,是一种文件黑名单集,而不是其他项目创建模板所使用的文件白名单集。真的很奇怪......无论如何,资源集是用户可配置的,因此您可以根据需要修改它们,一旦这样做,您就不应该有任何问题(我相信,但我以前错了;-)

Suggestion - Use 3rd party build tools in conjunction with your IDE

建议 - 将 3rd 方构建工具与您的 IDE 结合使用

You might be better off basing your build off of a 3rd party tool such as Gradle or Maven. Idea works really well with both these external build tools (and others). The advantages of using a 3rd party tool is:

您最好基于第 3 方工具(例如 Gradle 或 Maven)进行构建。 Idea 适用于这些外部构建工具(和其他工具)。使用第 3 方工具的优点是:

  1. Projects with those tools follow a convention where resources are placed in a specific resource folder and everything in that folder is treated as a resource to be packaged in your build output. So there is less confusion there.
  2. The resultant projects are more portable and easier to use by other developers who may not be using Idea.
  3. The projects can be built easily by continuous integration build systems like Jenkins.
  4. Both Gradle and Maven have JavaFX specific plugins which provide additional support for packaging JavaFX applications.
  1. 使用这些工具的项目遵循一个约定,其中资源放置在特定的资源文件夹中,该文件夹中的所有内容都被视为要打包在构建输出中的资源。所以那里的混乱较少。
  2. 由此产生的项目更易于移植,也更易于其他可能不使用 Idea 的开发人员使用。
  3. 这些项目可以通过像 Jenkins 这样的持续集成构建系统轻松构建。
  4. Gradle 和 Maven 都有 JavaFX 特定的插件,为打包 JavaFX 应用程序提供额外的支持。

Of course, the disadvantage of using a 3rd party build tool, is the complexity (and many quirks) of learning them as sometimes they can be quite the beast. So I think it's a bit of a tradeoff - small, personal exploratory projects don't need them, larger projects or projects you intend to share with others benefit from using such tools.

当然,使用 3rd 方构建工具的缺点是学习它们的复杂性(和许多怪癖),因为有时它们可​​能是野兽。所以我认为这有点权衡 - 小型的个人探索性项目不需要它们,大型项目或您打算与他人共享的项目可以从使用此类工具中受益。

回答by Noah Ternullo

One solution is to generate your code using Scene Builder, and then integrate it into your existing IntelliJ project. While it should be possible to import this as a module, you can also simply move the code to where it needs to be (making appropriate corrections to package statements, etc). If you notice that the javafx imports aren't recognized within your project, try this solution:

一种解决方案是使用 Scene Builder 生成您的代码,然后将其集成到您现有的 IntelliJ 项目中。虽然应该可以将其作为模块导入,但您也可以简单地将代码移动到需要的位置(对包语句进行适当的更正等)。如果您发现项目中无法识别 javafx 导入,请尝试以下解决方案:

First ensure that you've set up IntelliJ in general for JavaFX by following the intial steps in this tutorial. Note: Below I mention 1.8, but this should work with 1.7+. I recommend 1.8+.

首先确保您按照本教程中的初始步骤为 JavaFX 设置了 IntelliJ 。注意:下面我提到了 1.8,但这应该适用于 1.7+。我推荐 1.8+。

Second ensure that the existing project settings allow for javaFX use

其次确保现有项目设置允许使用 javaFX

  1. Open Project Structure
  2. Click on Project under the project settings pane
  3. Ensure that the Project SDK is 1.8+.
  1. 开放项目结构
  2. 单击项目设置窗格下的项目
  3. 确保项目 SDK 为 1.8+。

Third Ensure that the modules are configured for a JDK supporting JavaFX

第三确保为支持 JavaFX 的 JDK 配置模块

  1. Open Project Structure
  2. Click on Modules under the project settings pane
  3. Ensure that the module SDK being used supports javaFX, Again I use JDK 1.8 but it should work with 1.7.
  1. 开放项目结构
  2. 单击项目设置窗格下的模块
  3. 确保正在使用的模块 SDK 支持 javaFX,我再次使用 JDK 1.8,但它应该可以与 1.7 一起使用。

Also, please be aware that if you have a maven project, you may need to put your fxml in a particular location, "/main/resources/". Take a look at this.

另外,请注意,如果您有一个 Maven 项目,您可能需要将您的 fxml 放在特定位置,“/main/resources/”。看看这个。

That worked for me, and I hope it helps you.

这对我有用,我希望它可以帮助你。

回答by Martin Pfeffer

Please keep in mind that your source(s) folder(s) has to be set correctly. See screenshot to get an idea what I mean..

请记住,您的源文件夹必须正确设置。查看屏幕截图以了解我的意思..

enter image description here

enter image description here

回答by shakram02

I followed those steps

我按照这些步骤

  • Create a new module in your existing intellij project (A)
  • Create a new JavaFx project elsewhere
  • Copy the code from the JavaFx project, namely Controller.java,Main.java and sample.fxmlinto your module in the (A) project under the src directory as in A/user-interface/src
  • 在现有的 Intellij 项目中创建一个新模块 (A)
  • 在别处创建一个新的 JavaFx 项目
  • 从JavaFx项目中复制代码,即Controller.java,Main.java and sample.fxml到src目录下(A)项目中的模块中,如A/user-interface/src

enter image description here

enter image description here

  • You'll find an error in sample.fxmlsince it references the controller class as sample.Controllerchange it to 'Controller' only. it worked this way (as I removed the samplepackage folder
  • 你会发现一个错误,sample.fxml因为它引用了控制器类,因为它只sample.Controller更改为“控制器”。它以这种方式工作(因为我删除了sample包文件夹

Fixing sample.fxml

Fixing sample.fxml

  • In (A) open the settings menu File | Project Structure | Modules
  • Select the user-interfacemodule from the list on the left side of the settings window
  • Switch to the dependenciestab
  • Add your old non-gui module as a module dependency using the green +mark on the right-hand side
  • Now you can use the classes in the non-gui module, enjoy!
  • 在(A)中打开设置菜单 File | Project Structure | Modules
  • user-interface从设置窗口左侧的列表中选择模块
  • 切换到dependencies选项卡
  • 使用+右侧的绿色标记将旧的非 gui 模块添加为模块依赖项
  • 现在您可以使用非 gui 模块中的类了,尽情享受吧!

note: you can skip the add module dependency and let intellij do it for you, just type a name of a class in the old module, alt+enter(the light bulb magic) -> add module depenency

注意:您可以跳过添加模块依赖项并让 Intellij 为您完成,只需在旧模块中键入一个类的名称,alt+enter(灯泡魔法)->add module depenency

Module dependency window

Module dependency window

回答by Rainb

So after lots of googling I finally found it out myself!

所以经过大量的谷歌搜索后,我终于自己找到了!

If you can create new Javafx projects in Intellijd and it works perfectly, but when you open a normal java application that happens to use javafx doesn't work, (because java just doesn't want to resolve the javafx references)

如果您可以在 Intellijd 中创建新的 Javafx 项目并且它可以完美运行,但是当您打开一个碰巧使用 javafx 的普通 java 应用程序时,它不起作用,(因为 java 只是不想解析 javafx 引用)

what works for me is this:You must add the "jre/lib/ext/jfxrt.jar"file to the module classpath that is located inside IDEA on my windows machine this is C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.3\jre64\lib\ext\jfxrt.jar.

对我有用的是:您必须将"jre/lib/ext/jfxrt.jar"文件添加到位于我的 Windows 机器上 IDEA 内的模块类路径中,这是C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.3\jre64\lib\ext\jfxrt.jar.

And now it finally builds!

现在它终于构建了!