javafx官方文檔學習之一Application與Stage,Scene初探

個人博文小站: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.Applicationclass. 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運行時在程序啓動時將會按順序依次進行以下步驟:

  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


參數:

    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

相關文章
相關標籤/搜索