Javafx 简明教程

JavaFX - Installation Using Eclipse

Eclipse 是 Java 编程中使用的集成开发环境 (IDE)。它包含一个工作区和一个可扩展的插件系统,以便自定义环境。你还可以将 JavaFX 与 Eclipse 一起使用。

Eclipse is an integrated development environment (IDE) used in Java programming. It contains a workspace and an extensible plug-in system in order to customize the environment. You can also use JavaFX with Eclipse.

本章解释了在 Eclipse IDE 中创建 JavaFX 应用程序的步骤。我们将使用 JavaFX SDK 和 Eclipse 的最新版本。

This chapter explains the steps to create a JavaFX application in Eclipse IDE. We will be using the latest versions of both JavaFX SDK and Eclipse.

Installing JavaFX in Eclipse

在 Eclipse IDE 中有一个名为 e(fx)clipse 的插件来支持 JavaFX。你可以使用以下步骤在 Eclipse 中设置 JavaFX。首先,确保你的系统中安装了 Eclipse。如果没有,请在你的系统中下载并安装 Eclipse。

A plug-in named e(fx)clipse is available in Eclipse IDE to support JavaFX. You can use the following steps to set JavaFX in Eclipse. First of all, make sure that you have Eclipse in your system. If not, download and install Eclipse in your system.

要安装 Eclipse IDE,请按以下步骤操作:

To install Eclipse IDE, follow the given steps below −

Step 1 - 访问 eclipse.org 网站。

Step 1 − Visit the eclipse.org website.

  1. Click the download button for the file named eclipse-inst-jre-win64.exe to be downloaded.

  2. Run the file downloaded for the eclipse installation to begin.

Step 2 - 选择下图中的高亮显示选项开始安装。

Step 2 − Select the highlighted option in the image below for the installation to begin.

eclipse installation1

Step 3 - 点击安装按钮安装所选 Eclipse IDE 版本。

Step 3 − Click on the install button to install the chosen Eclipse IDE version.

eclipse installation2

Step 4 - 此步骤开始安装 Eclipse IDE,可能需要一段时间。

Step 4 − This step starts the installation of Eclipse IDE and it may take a while.

Step 5 - 最后,启动应用程序查看欢迎页。

Step 5 − Lastly, launch the application to see the welcome page.

eclipse installation4

Installing JavaFX Plug-In

安装完 Eclipse 后,按照以下步骤在你的系统中安装 e(fx)clipse 插件。

Once Eclipse is installed, follow the steps given below to install e(fx)clipse plug-in in your system.

Step 1 - 打开 Eclipse 中的 Help 菜单,并选择 Eclipse Marketplace 选项,如下图所示。

Step 1 − Open the Help menu in Eclipse and select Eclipse Marketplace option as shown below.

eclipse javafx

点击后,它将显示包含所有插件的新窗口,如下面的屏幕截图所示。

Upon clicking, it will display the new window containing all the plug-ins, as shown in the following screenshot.

Step 2 - 在 Find 文本框中,使用 “fx” 关键字搜索 e(fx)clipse 插件。

Step 2 − In the Find text box, search for e(fx)clipse plug-in using the 'fx' keyword.

efxclipse

Step 3 − 检索后,单击 Install 按钮以在 Eclipse 中安装 JavaFX 插件。

Step 3 − Once retrieved, click the Install button to install JavaFX plug-in in Eclipse.

Step 4 − 在下一个窗口中,您将看到 JavaFX 的许可协议,您必须同意该协议。选择“我同意”,然后单击“完成”。

Step 4 − In the next window, you will see a license agreement for JavaFX that you have to agree to. Select "I agree" and click "finish".

efxclipse license

Step 5 − 一旦安装了插件,Eclipse 将需要重新启动。因此,单击如下所示的重启选项。

Step 5 − Once the plug-in is installed, the eclipse will need to be restarted. So, click the restart option as shown below.

eclipse restart

Step 6 − 您必须通过指向 JavaFX 的安装目录来设置首选项。为此,在“窗口”菜单中,单击“首选项”,如下所示。

Step 6 − You will have to set the preferences by pointing to the installation directory of JavaFX. For that, in the "Window" menu, click on "Preferences" as shown below.

eclipse preferences

Step 7 − 单击“JavaFX”选项并指向 JavaFX SDK 的“lib”目录,如屏幕截图所示。如果您安装了“Scenebuilder”,则还可以指向 Scenebuilder 的安装目录,如下图所示。

Step 7 − Click on "JavaFX" option and point to the "lib" directory of JavaFX SDK as shown in the screenshot. If you have "Scenebuilder" installed, you can point to the installation directory of Scenebuilder as well, like shown.

eclipse preferences1

Step 8 − 现在 JavaFX 首选项已设置,您可以准备创建一个新的 JavaFX 项目。转到“文件 → 新建 → 其他”。

Step 8 − Now that JavaFX preferences are set, you are ready to create a new JavaFX project. Go to "File → New → Other".

eclipse project

Step 9 − 然后,您将获得一个窗口,您可以在其中看到 Eclipse 提供的一系列向导,用于创建项目。展开 JavaFX 向导,选择 JavaFX Project 并单击 Next 按钮,如以下屏幕截图所示。

Step 9 − Then, you will get a window where you can see a list of wizards provided by Eclipse to create a project. Expand the JavaFX wizard, select JavaFX Project and click the Next button as shown in the following screenshot.

new project select wizard

Step 10 − 单击 Next ,将打开一个“新建项目向导”。在这里,您可以键入所需的项目名称并单击 Finish

Step 10 − On clicking Next, a New Project Wizard opens. Here, you can type the required project name and click Finish.

project name and click finish

Step 11 −单击 Finish 后,将使用给定的名称(FirstJavaFXProjectt)创建一个应用程序。一个 module_info.java 文件也将自动创建,该文件需要被删除以在运行项目时避免错误。

Step 11 − On clicking Finish, an application is created with the given name (FirstJavaFXProjectt). A module_info.java file is automatically created as well, which needs to be deleted in order to avoid errors while running our project.

delete module info

Step 12 − 在名为 application 的子包中,将创建一个名为 Main.java 的程序,如下所示。在执行此文件之前,我们必须添加 VM 参数。为此,请转到“运行”符号并单击“运行配置”,如下面的屏幕截图所示。

Step 12 − In the sub-package named application, a program with the name Main.java is created as shown below. Before executing this file, we must add VM arguments. For that, go to the "Run" symbol and click "Run configurations" as shown in the screenshot below.

javafx run configurations

Step 13 − 在“参数”选项卡中,添加以下 VM 参数 −

Step 13 − In the "Arguments" tab, add the following VM arguments −

In Windows

--module-path "\path\to\javafx-sdk-20\lib" --add-modules javafx.controls,javafx.fxml

In Linux

--module-path /path/to/javafx-sdk-20/lib --add-modules javafx.controls,javafx.fxml

请看下面的屏幕截图 −

Look at the screenshot below −

eclipse vm arguments

添加后,单击“应用”和“关闭”。

Once added, click "Apply" and "Close".

Step 14 − 现在,在执行 Main.java 文件时,会弹出一个新窗口,其中包含给定的标签。

Step 14 − Now, upon executing the Main.java file, a new window pops up containing the given label.

eclipse output

Note − 我们将在后面的章节中学习代码。

Note − We will learn about the code in further chapters.