Home >Java >javaTutorial >How to use JavaFX to build graphical interface applications in Java 9
How to use JavaFX to build graphical interface applications in Java 9
JavaFX is a graphical interface toolkit for building rich client applications on the Java platform. It provides rich graphics and multimedia functions to help developers create dynamic and interactive user interfaces. This article will introduce how to use JavaFX to build graphical interface applications in Java 9, and will come with some code examples.
Before you begin, you need to ensure that Java Development Kit (JDK) 9 and JavaFX are properly installed. If these tools are not installed yet, please complete the installation first.
Step 1: Create the basic structure of a JavaFX application
First, create a new Java project. Then, create a new Java class in the root directory of the project and name it MainApp. Next, copy the following code into the MainApp class:
import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.stage.Stage; public class MainApp extends Application { @Override public void start(Stage primaryStage) { primaryStage.setTitle("JavaFX Application"); Button btn = new Button(); btn.setText("Click Me!"); btn.setOnAction(e -> System.out.println("Hello, JavaFX!")); Scene scene = new Scene(btn, 300, 200); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }
In this example, we create a window that contains a button. When the button is clicked, it outputs a simple message to the console.
Step 2: Run the JavaFX application
To run this JavaFX application, we need to create a run configuration file in the root directory of the project. Create a file named module-info.java in your project root directory and copy the following code into it.
module JavaFXDemo { requires javafx.fxml; requires javafx.controls; opens sample; }
Then, configure the run configuration in the IDE. Specific configuration steps may vary depending on the IDE. You need to set the following Java startup parameters:
--module-path /path/to/javafx-sdk-11/lib --add-modules=javafx.controls,javafx.fxml
Please replace /path/to/javafx-sdk-11
according to your own installation location.
Finally, run the MainApp class. You will see a window with a button open on your screen.
Step 3: Explore the JavaFX Library
JavaFX provides many components and features that can be used to create graphical interface applications. You can use these components to build a variety of user interfaces. The following are some commonly used JavaFX components:
You can learn more about the use of these components through the JavaFX documentation and sample code. Additionally, there are many third-party libraries and tools available to enhance the functionality of JavaFX.
Summary:
This article introduces how to use JavaFX to build graphical interface applications in Java 9. We show how to create a simple JavaFX application through examples and introduce readers to some commonly used JavaFX components. I hope this article will help you learn JavaFX!
The above is the detailed content of How to use JavaFX to build graphical interface applications in Java 9. For more information, please follow other related articles on the PHP Chinese website!