Preliminary understanding of JavaFX
Introduction to JavaFX
When we mention Java's graphical interface library, we usually hear swing or older AWT, including many books, which are also introduced above. Many schools and training classes also teach these two technologies. But in fact, both technologies have been out of date for a long time. Although swing is not difficult to learn, it is actually difficult to write an interface with it. Because its interface and code are not separated, when writing, the code must be full of a large number of coordinates, which is extremely difficult to modify. Microsoft's WPF is a better one in this regard. It can only be said that whoever uses it knows.
Of course, although writing client-side graphical programs is a weakness of Java, Java has not given up its efforts in this regard. JavaFX introduced today is the latest technology of Java in writing graphical interface programs. If you plan to use java to write graphical interface programs without historical burden, JavaFX is highly recommended.
This is the Oracle official website about JavaFX resources and documents.
This is the official sample program. We can refer to the JavaFX section to learn how to use it. The following is one of the fractal JavaFX programs. Click the numbers above to enter different micro displays. I feel that I have a feeling of looking at the virus micro world, which is very shocking.
How to install
As long as you install the latest version of JDK 8, you can use the JavaFX library. If it is not installed, start the installation as soon as possible.
Get started quickly
First program
Create a new project, then write the following classes, and then compile and run to see the results. There is no need to explain this procedure. If you have learned swing and other graphical interface frameworks, it should be very easy to understand this code. Of course, since JavaFX is new, I also use a new feature of Java 8 - lambda expressions.
package yitian.javafxsample; import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class HelloJavaFX extends Application { @Override public void start(Stage primaryStage) throws Exception { Button btn = new Button(); btn.setText("你好啊,世界"); btn.setOnAction(event -> { System.out.println("你好,世界!"); } ); StackPane root = new StackPane(); root.getChildren().add(btn); Scene scene = new Scene(root,300,250); primaryStage.setTitle("Hello World!"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } }