Javafx 简明教程
JavaFX - Environment
从 Java8 开始,JDK (Java Development Kit) 中包含 JavaFX 库。因此,要运行 JavaFX 应用程序,您只需要在您的系统中安装 Java8 或更高版本。
From Java8 onwards, the JDK (Java Development Kit) includes JavaFX library in it. Therefore, to run JavaFX applications, you simply need to install Java8 or later version in your system.
截至目前,JDK 的最新版本是 20.0.1。因此,本教程将适用于 JDK20 支持的 JavaFX 版本。
The latest version of JDK, as of today, is 20.0.1. So, this tutorial will work with JavaFX versions supported by JDK20.
此外,像 Eclipse 和 NetBeans 这样的 IDE 都支持 JavaFX。本篇教程将教您如何设置环境来运行 JavaFX 应用程序 locally 。
In addition to it, IDE’s like Eclipse and NetBeans provide support for JavaFX. This chapter teaches you how to set the environment to run JavaFX Applications locally.
Installing Java20
首先,您需要通过打开命令提示符并在其中键入 “java” 命令来验证您的系统中是否安装了 Java。
First of all, you will have to verify whether there is Java Installed in your system or not by opening the command prompt and typing the command “Java” in it.
如果您尚未在您的系统中安装 Java,命令提示符将显示以下屏幕截图中所示的消息。
If you haven’t installed Java in your system, the command prompt displays the message shown in the following screenshot.
您可以按照以下步骤安装 Java。
You can install Java by following the steps given below.
Step 1 - 访问 JavaSE Downloads 页面,单击 Download 按钮,如下图所示:
Step 1 − Visit the JavaSE Downloads Page, click on the JDK Download button as highlighted in the following screenshot
Step 2 - 点击高亮的链接,适用于 Windows 64 位操作系统的 Java20 开发工具包便会下载到您的系统中。
Step 2 − On clicking the highlighted link, the Java20 Development Kit suitable for Windows 64-bit Operating System will be downloaded onto your system.
Step 3 - 运行下载的二进制可执行文件以开始安装 JDK20。
Step 3 − Run the downloaded binary executable file to start the installation of JDK20.
Step 4 - 根据需要更改安装目录,否则保留默认目录并继续进行。
Step 4 − Change the installation directory if needed, else keep the default ones and proceed further.
Step 5 - 在选择目标文件夹并单击“下一步”后,JavaFX 安装过程将开始显示进度条。
Step 5 − On selecting the destination folder and clicking Next, the JavaFX installation process starts displaying the progress bar.
Step 6 - 单击“关闭”按钮(如以下屏幕截图所示)完成安装过程。
Step 6 − Finish the installation process by clicking the Close button as shown in the following screenshot.
Step 7 - 要验证是否正确安装了 Java,请转到命令提示符并执行 “java -version” 命令,如下所示:
Step 7 − To verify if the installation is done properly, go to the command prompt and execute the 'java -version' command as shown below −
Setting up the Path for Windows
安装 Java 后,您需要设置路径变量。假设您已在 C:\Program Files\java\jdk-20 目录中安装了 Java。
After installing Java, you need to set the path variables. Assume that you have installed Java in C:\Program Files\java\jdk-20 directory.
现在你可以执行以下步骤:
Now you can follow the steps that are given below −
-
Right-click on 'My Computer/This PC' and select 'Properties'.
-
Then, select 'Advanced System Settings'.
-
Click on the 'Environment Variables' button under the 'Advanced' tab.
-
Now, alter the 'Path' variable so that it also contains the path to the Java executable. For Example, if the path is currently set to 'C:\WINDOWS\SYSTEM32', then change your path to read 'C:\WINDOWS\SYSTEM32; C:\Program Files\java\jdk-20\bin'.
此外,单击“系统变量”下的“新建”创建一个新系统变量,如下所示。
Also, create a new system variable by clicking 'New' under System variables as shown below.
将新变量命名为“JAVA_HOME”,并将其指向 JDK 安装目录。
Name the new variable as "JAVA_HOME" and point it to the JDK installation directory.
Installing JavaFX SDK
现在,您必须安装 JavaFX SDK 以访问用于开发应用程序内容的命令行工具和技术。
Now, you have to install the JavaFX SDK to access the command-line tools and technologies to develop application content.
为使用 JavaFX SDK 运行程序,请按以下步骤执行操作:
In order to use the JavaFX SDK to run a program, follow the steps given below −
Step 1 – 从 JavaFX - Gluon 页面下载适用于您操作系统的合适的 JavaFX 运行时(这里,我们下载的是 JavaFX 20)。
Step 1 − Download an appropriate JavaFX runtime for your operating system (here, we are downloading JavaFX 20) from the JavaFX - Gluon page.
Step 2 – 将下载一个 .zip 文件夹,其中包含为应用程序开发内容所需的工具。将此文件夹解压缩到任何所需位置(如“C:/JavaFX”)。
Step 2 − A .zip folder will be downloaded containing tools required to develop content for applications. Unzip this folder to any desired location (say, "C:/JavaFX").
Step 3 – 现在,右键单击“我的电脑/此电脑”并选择“属性”。然后,选择“高级系统设置”。单击“高级”选项卡下的“环境变量”按钮。创建一个新环境变量,并将其命名为“PATH_TO_FX”,指向 JavaFX 运行时的 lib 目录。
Step 3 − Now, right-click on 'My Computer/This PC' and select 'Properties'. Then, select 'Advanced System Settings'. Click on the 'Environment Variables' button under the 'Advanced' tab. Create a new environment variable and name it "PATH_TO_FX" pointing to the lib directory of the JavaFX runtime.
现在,这将允许您使用下载的 JavaFX 运行时从命令行编译和运行 JavaFX 应用程序。
This will now allow you to compile and run JavaFX applications from the command line using the downloaded JavaFX runtime.