javafx官方文档学习之一Application与Stage,Scene初探


我的个人站点:http://www.xby1993.net

转载请注明地址。作者:浮沉雄鹰。

javafx已经嵌入在jre之中


2 javafx UI设计工具JavaFX Scene Builder.


Oracle支持的javafx额外UI库,现在只支持jdk8:controlsfx



3 HelloWord解析:

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.

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

总结一下Application是javafx程序的入口点,就是Main类要继承Application类,然后覆盖其start方法,而start方法用于展示stage舞台,state舞台是一个类似于Swing中的JWindow的顶级容器,代表一个窗口。它用于容纳场景Scene,场景Scene是一个类似于Swing的JFrame的容器。但是却是以树的形式组织的,每一个子组件就是它的一个节点。其根节点一般是Pane面板如以上的:StackPane.它是一个根节点容器。可以容纳子节点。各子节点挂载在其上。

主要实现步骤:State(即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

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值