JavaFX vs Swing and AWT
Swing and AWT are replaced by the JavaFX platform for developing rich Internet applications. When Java was introduced, the GUI classes were bundled in a library known as the Abstract Windows Toolkit (AWT). AWT is fine for developing simple graphical user interfaces, but not for developing comprehensive GUI projects. In addition, AWT is prone to platform-specific bugs. The AWT user-interface components were replaced by a more robust, versatile, and flexible library known as Swing components. Swing components are painted directly on canvases using Java code. Swing components depend less on the target platform and use less of the native GUI resource. With the release of Java 8, Swing is replaced by a completely new GUI platform known as JavaFX. Java的图形用户界面程序的开发方式历经了AWT、Swing和JavaFX三个阶段。
A JavaFX Application
一个JavaFX程序的基本结构如下,其主类必须继承于Application抽象类,并重写实现该类的start抽象方法,start方法执行图形用户界面的初始化操作。main函数对于由命令行启动的JavaFX程序不是必须的,若由IDE启动,则需要完善main函数,并在其中调用Application的静态方法launch(args)。
public class MyGUIApp extends Application{
@Override
public void start(Stage primaryStage){
//Interface initialization
}
public static void main(String[] args){
Application.launch(args);
}
}
An Example

在GUIClient程序中,可以观察到三个图形界面要素:Stage、Scene和Button。可以将JavaFX的图形界面构造和话剧舞台进行类比,Stage是图形界面的窗口(window)对应于话剧的舞台,Scene类似于话剧中的场景,场景中具体摆放了各种道具,例如上面的例子中Scene上面摆放了一个Button。一个应用程序可以同时打开多个窗口,即创建多个Stage。
Application Life-cycle
1. Constructs an instance of the specified Application class
2. Calls the init() method
3. Calls the start(javafx.stage.Stage) method
4. 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 on Platform is true
5. Calls the stop() method

