JavaFx2学习笔记day01

来源:互联网 发布:淘宝服装拍摄平铺 编辑:程序博客网 时间:2024/05/16 15:47

  工作需要就开始学习javaFX2,之前的1.0和1.1已经被oracle抛弃了。先看代码

  

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();    }}

如何看stage,scene和stackPane呢。先看官网上怎么说

  • The main class for a JavaFX application extends the javafx.application.Applicationclass. The start() method is the main entry point for all JavaFX applications.

  • 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.

  • 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.

  • 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.

  • 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.


1.Stage(舞台,窗口)

    上面说stage是javafx的顶级容器,也就是说,这个是所有对象的容器,是最顶层的实现。按照我自己的理解就是stage代表了整个窗口、舞台。

2.Scene(场景)

    scene的解释是:它是其他所有content的容器。我自己的理解:scene是一个内容显示器,就是实际上显示内容的窗口,如果说stage代表了一个整个窗口,那么scene就   是里面的黑色显示部分(不包含蓝色的边框,在黑色的区域可以添加自己的组件),它可以包含根节点即root node


3.nodes

 看看官网的说法

The individual items held within the JavaFX scene graph are known as nodes. Each node is classified as either a branch node (meaning that it can have children), or a leaf node (meaning that it cannot have children). The first node in the tree is always called the root node, and it never has a parent. See a general inheritance diagram in Figure 1.

Description of Figure 1 follows

也就是说,跟节点是没有parent的,是顶层的节点,一般称为root node,它可以包含leaf node(没有孩子)和branch node(包含子孙),接下来

The JavaFX API defines a number of classes that can act as root, branch or leaf nodes. When substituted with actual class names, this same figure might resemble that shown in Figure 2 in a real application.

Figure 2 Specific Root, Branch, and Leaf Classes

Description of Figure 2 follows
Description of "Figure 2 Specific Root, Branch, and Leaf Classes"

In Figure 2, a Group object acts as the root node. The Circle and Rectangle objects are leaf nodes, because they do not (and cannot) have children. The Region object (which defines an area of the screen with children than can be styled using CSS) is a branch node that contains two more leaf nodes (Text and ImageView). Scene graphs can become much larger than this, but the basic organization — that is, the way in which parent nodes contain child nodes — is a pattern that repeats in all applications.

也就是javafx定义了很多的类,可以当这个根节点(除了上面的stackPane,Group也可以当根节点,代码如下),他们所有的组织方式都是这种树形结构。

package scenegraphdemo; import javafx.application.Application;import javafx.scene.Group;import javafx.scene.Scene;import javafx.scene.paint.Color;import javafx.scene.shape.Rectangle;import javafx.stage.Stage; public class Main extends Application {    @Override   public void start(Stage stage) {       Group root = new Group();       Scene scene = new Scene(root, 500, 500, Color.BLACK);        Rectangle r = new Rectangle(25,25,250,250);       r.setFill(Color.BLUE);       root.getChildren().add(r);        stage.setTitle("JavaFX Scene Graph Demo");       stage.setScene(scene);       stage.show();   }    public static void main(String[] args) {       launch(args);   }}

然后是node,parent,scene

The javafx.scene package defines more than a dozen classes, but three in particular are most important when it comes to learning how the API is structured:

  • Node: The abstract base class for all scene graph nodes.(所有图形节点的基类)

  • Parent: The abstract base class for all branch nodes. (This class directly extends Node).(所有有分支节点的基类)

  • Scene: The base container class for all content in the scene graph.(所有可以显示图形内容场景的基类)

第一段代码的关系图(可以看出stage,scene,root的关系)Description of Figure 1-1 follows

最后的建议是:

    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.

 如果是在swing中引用javafx那么必须main函数

如果是用javafx打包工具时可以不用实现main函数

如果有main函数的时候最好不要修改,就按照原来代码生成的形式

public class HelloWorld extends Application {    public static void main(String[] args) {        launch(args);    }

参考:http://docs.oracle.com/javafx/2/get_started/hello_world.htm

            http://docs.oracle.com/javafx/2/scenegraph/jfxpub-scenegraph.htm



0 0
原创粉丝点击