public abstract class Application
extends java.lang.Object
Life-cycle
The entry point for JavaFX applications is the Application class. (程序的入口点)The JavaFX runtime does the following, in order, whenever an application is launched:
- Constructs an instance of the specified Application class
- Calls the
init()
method - Calls the
start(javafx.stage.Stage)
method - Waits for the application to finish, which happens when either of the following occur:
- the application calls
Platform.exit()
- the last window has been closed and the
implicitExit
attribute onPlatform
is true
- the application calls
- Calls the
stop()
method
Note that the start
method is abstract and must be overridden. The init
and stop
methods have concrete(具体) implementations that do nothing.
Parameters
Application parameters are available by calling the getParameters()
method from the init()
method, or any time after the init
method has been called.
Threading
JavaFX creates an application thread for running the application start method, processing input events, and running animation(动画) timelines(时间轴). Creation of JavaFX Scene
and Stage
objects as well as modification(修改) of scene graph operations to live objects (those objects already attached to a scene) must be done on the JavaFX application thread.
The Application constructor and init
method are called on the launcher(启动器) thread, not on the JavaFX Application Thread. This means that an application must not construct a Scene
or a Stage
in either the constructor or in the init
method. An application may construct other JavaFX objects in the init
method.
Example
The following example will illustrate(演示) a simple JavaFX application.
package appliction;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class MyApp extends Application {
public void start(Stage stage) {
Circle circ = new Circle(40, 40, 30);
Group root = new Group(circ);
Scene scene = new Scene(root, 400, 300);
stage.setTitle("My JavaFX Application");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
The above example will produce the following: