个人博文小站:http://www.xby1993.net,全部文章均为同步发布。html
转载请注明做者,出处。java
自jdk7u6以后javafx已经嵌入在jre之中node
2 javafx UI设计工具JavaFX Scene Builder.android
Oracle支持的javafx额外UI库,如今只支持jdk8:controlsfxwindows
3 HelloWord解析:api
package helloworld; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class HelloWorld extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Hello World!"); Button btn = new Button(); btn.setText("Say 'Hello World'"); btn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Hello World!"); } }); StackPane root = new StackPane(); root.getChildren().add(btn); primaryStage.setScene(new Scene(root, 300, 250)); primaryStage.show(); } }
The main class for a JavaFX application extends the javafx.application.Application
class. The start()
method is the main entry point for all JavaFX applications.oracle
A JavaFX application defines the user interface container by means of a stage and a scene. The JavaFX Stage
class is the top-level JavaFX container. The JavaFX Scene
class is the container for all content. Example 1-1 creates the stage and scene and makes the scene visible in a given pixel size.app
In JavaFX, the content of the scene is represented as a hierarchical scene graph of nodes. In this example, the root node is a StackPane
object, which is a resizable layout node. This means that the root node's size tracks the scene's size and changes when the stage is resized by a user.less
The root node contains one child node, a button control with text, plus an event handler to print a message when the button is pressed.ide
The main()
method is not required for JavaFX applications when the JAR file for the application is created with the JavaFX Packager tool, which embeds the JavaFX Launcher in the JAR file. However, it is useful to include the main()
method so you can run JAR files that were created without the JavaFX Launcher, such as when using an IDE in which the JavaFX tools are not fully integrated. Also, Swing applications that embed JavaFX code require the main()
method.
总结一下Application是javafx程序的入口点,就是Main类要继承Application类,而后覆盖其start方法,而start方法用于展现stage舞台,stage舞台是一个相似于Swing中的JWindow的顶级容器,表明一个窗口。它用于容纳场景Scene,场景Scene是一个相似于Swing的JFrame的容器。可是倒是以树的形式组织的,每个子组件就是它的一个节点。其根节点通常是Pane面板如以上的:StackPane.它是一个根节点容器。能够容纳子节点。各子节点挂载在其上。
主要实现步骤:Stage(即start方法的参数,通常考系统传入)设置场景-场景Scene包装面板根节点-面板Pane根节点挂载子节点-子节点。
注意:根节点的大小是随着Scene自适应的。
main()方法并非必须有的,通常javafx程序是将javafx packager Tool嵌入到jar文件,
可是为了便于调试,仍是写出来为好。
public static void main(String[] args) { launch(args); }
从这个HelloWorld程序中能够看出事件处理机制与Swing相差不大。
Figure 1-1 Hello World Scene Graph
下面分析一下Application类:
Life-cycle:生命周期:
Application是一个javafx程序的入口类,是一个抽象类,必须子类化,它的start方法为抽象方法,必须覆盖
而init()和stop()方法则为空实现。
javafx运行时在程序启动时将会按顺序依次进行以下步骤:
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 on Platform
is true
Calls the stop()
method
参数:
Application parameters are available by calling the getParameters()
method from the init()
method, or any time after the init
method has been called.
Application能够经过getParameters()方法在init()方法内获取参数,或者在init()调用以后的任何方法中获取。
Threading线程:
关于:Launch Thread启动线程和Application线程的区别
Lacunch Thread启动线程是javafx运行时触发的,它会负责构造Application对象和调用Application对象的init()方法,这意味着主State主场景是在launcher Thread线程中被构造的,(由于它是init方法的参数)故而咱们能够不用管它,直接拿来用就能够了。可是launch Thread并非UI线程。它的工做也就只是上面所说的。
Application Thread:至关于Swing中的UI事件分派线程EDT:
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.
HostServices |
getHostServices() Gets the HostServices provider for this application. |
Application.Parameters |
getParameters() Retrieves the parameters for this Application, including any arguments passed on the command line and any parameters specified in a JNLP file for an applet or WebStart application. |
void |
init() The application initialization method. |
static void |
launch(java.lang.Class<? extends Application> appClass, java.lang.String... args) Launch a standalone application. |
static void |
launch(java.lang.String... args) Launch a standalone application. |
void |
notifyPreloader(Preloader.PreloaderNotification info) Notifies the preloader with an application-generated notification. |
abstract void |
start(Stage primaryStage) The main entry point for all JavaFX applications. |
void |
stop() This method is called when the application should stop, and provides a convenient place to prepare for application exit and destroy resources. |
The following example will illustrate a simple JavaFX application.
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(); } } 解析Stage类 Stage是Window类的子类,Window对象中有获取窗口焦点位置,设置监听窗口打开隐藏关闭显示事件的方法。有设置窗口到屏幕中央。等相关方法。 The JavaFX Stage class is the top level JavaFX container. The Additional Stage objects may be constructed by the application.and Stage的Style风格样式 A stage has one of the following styles: StageStyle.DECORATED - a stage with a solid white background and platform decorations. StageStyle.UNDECORATED - a stage with a solid white background and no decorations. StageStyle.TRANSPARENT - a stage with a transparent background and no decorations. StageStyle.UTILITY - a stage with a solid white background and minimal platform decorations. 构造器以下: Constructor and Description Stage()Creates a new instance of decorated Stage. Stage(StageStyle style)Creates a new instance of Stage. 显然能够经过构造器指定样式。
Stage的展示模式:
Modality
A stage has one of the following modalities:
Modality.NONE
- a stage that does not block any other window.
Modality.WINDOW_MODAL
- a stage that blocks input events from being delivered to all windows from its owner (parent) to its root. Its root is the closest ancestor window without an owner.
Modality.APPLICATION_MODAL
- a stage that blocks input events from being delivered to all windows from the same application, except for those from its child hierarchy.
相似于对话框形式的模式与非模式。
When a window is blocked by a modal stage its Z-order relative to its ancestors is preserved, and it receives no input events and no window activation events, but continues to animate and render normally. Note that showing a modal stage does not necessarily block the caller. The show()
method returns immediately regardless of the modality of the stage. Use theshowAndWait()
method if you need to block the caller until the modal stage is hidden (closed). The modality must be initialized before the stage is made visible.
模式形式的Stage会阻塞输入事件input event和窗口活动事件,可是并不阻塞画面的渲染和变化。
也不会阻塞它的调用者,调用者show()以后会当即返回。也能够采用阻塞形式showAndWait().可是要注意:模式形式的Stage必须在调用show以前完成全部初始化工做。
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.text.Text; import javafx.stage.Stage; public class HelloWorld extends Application { @Override public void start(Stage stage) { Scene scene = new Scene(new Group(new Text(25, 25, "Hello World!"))); stage.setTitle("Welcome to JavaFX!"); stage.setScene(scene); stage.sizeToScene(); stage.show(); } public static void main(String[] args) { Application.launch(args); } }
Scene解析:
public class Sceneextends java.lang.Object implements EventTarget
The JavaFX Scene
class is the container for all content in a scene graph. The background of the scene is filled as specified by the fill
property.
The application must specify the root Node
for the scene graph by setting the root
property. If a Group
is used as the root, the contents of the scene graph will be clipped by the scene's width and height and changes to the scene's size (if user resizes the stage) will not alter the layout of the scene graph. If a resizable node (layout Region
or Control
is set as the root, then the root's size will track the scene's size, causing the contents to be relayed out as necessary.
The scene's size may be initialized by the application during construction. If no size is specified, the scene will automatically compute its initial size based on the preferred size of its content.
Scene objects must be constructed and modified on the JavaFX Application Thread.
Scene中包含各种UI事件的设置监听器的方法。如mouse,key,drag,touch,swipe,zoom,scroll等。
Example:
import javafx.scene.*; import javafx.scene.paint.*; import javafx.scene.shape.*; Group root = new Group(); Scene s = new Scene(root, 300, 300, Color.BLACK); Rectangle r = new Rectangle(25,25,250,250); r.setFill(Color.BLUE); root.getChildren().add(r);
一个感受:与android类库确实有点类似。
参考:http://docs.oracle.com/javafx/2/get_started/hello_world.htm