• 学习JavaFX(一):初步认识GUI设计


    JavaFX v.s. Java
    JavaFX:声明式、静态类型的脚本语言;它具有一等函数、声明的语法、列表推导,以及基于依赖关系的增量式求值等特征;JavaFX 脚本式语言特别适用于Java2D swing GUI组件,它允许简单地创建图形界面;在JavaFX中可以直接调用Java的算术、逻辑运算符,instance等操作符,还可以显式调用Java类库和方法;使用JavaFX可以轻松的编写跨平台的富客户端应用程序

    Java:老派的编程语言,不解释;同样的在Java中可以使用ScriptEngineManager这个类显式调用JavaFX;总之,JavaFX、Java二者的混合使用非常爽

    JavaFX的基本程序结构
    javafx.application.Application类
    javafx.application.Application类定义了编程JavaFX的基本要素

    每个JavaFX程序都定义在一个extends了javafx.application.Application类的类中

    javafx.application.Application是JavaFX程序的入口类,自定义类继承该类,并覆盖该类的start()方法,它是JavaFX的入口方法

    javafx.stage.Stage类、javafx.scene.Scene类、基本布局(layout)
    好吧……我承认这一块写得有点乱(思路清晰就行 :-)

    javafx.stage.Stage类继承自javafx.stage.Window类,是JavaFX的顶层容器,代表一个舞台,当JavaFX程序被启动时,一个Stage就被自动创建,并通过参数传递给start()方法

    @Override
    public void start(Stage primaryStage){
    // code goes here...
    }


    以下文字翻译自JavaFX官方文档:

    风格(Style):一个stage有且仅有以下一种风格,1)StageStyle.DECORATED:有装饰的实心白色,2)StageStyle.UNDECORATED:无装饰的实心白色,3)StageStyle.TRANSPARENT:无装饰的透明舞台,4)StageStyle.UTILITY:有着朴素装饰的实心白色

    所有者(Owner):一个stage可以选择性的选择一个window作为其所有者,通过initOwner(..)方法指定所属窗口对象

    表现方式(Modality):一个stage可以有以下的表现方式:1)Modality.NONE:不遮挡别的stage,2)Modality.WINDOW_MODAL:stage阻挡所有传入其所有者的window的输入事件,3)Modality.APPLICATION_MODAL:stage阻挡所有来自同一个application的输入事件

    总的来说java.stage.Stage相当于Swing的JWindow,是容纳java.scene.Scene的容器

    javafx.scene.Scene直接继承自java.lang.Object类,这是它的原型声明:

    @DefaultProperty(value="root")
    public class Scene extends Object implements EventTarget

    除了javafx.stage.Stage容器外,javafx.scene.Scene容器算是很高层次的容器了——事实上javafx.scene.Scene相当于Swing的JFrame,我们在Jframe这个框架/场景中添加新的元素和内容

    javafx.scene.Scene是一个类似于Swing的JFrame的容器;但是javafx.scene.Scene却是以树的形式组织的,每一个子组件就是它的一个节点;其根节点一般是Pane面板(如StackPane、BorderPane,就是一个根节点容器,可以容纳子节点,各子节点挂载在其上)

    javafx.scene.Scene类是依附于javafx.stage.Stage类存在,是场景的意思;场景可以添加控件和其它用户接口结点——通过这种方法创建应用程序的用户界面;一个Stage必须至少有一个Scene

    javafx.scene.Scene类的构造方法如下所列:

    /** Creates a Scene for a specific root Node. */
    Scene(Parent root)
    
    /** Creates a Scene for a specific root Node with a specific size. */
    Scene(Parent root, double width, double height)
    
    /** Construct a scene consisting of a root, with a dimension of width and height, and specifies whether a depth buffer is created for a scene. */
    Scene(Parent root, double width, double height, boolean depthBuffer)
    
    /** Construct a scene consisting of a root, with a dimension of width and height, specifies whether a depth buffer is created for this scene and specifies whether scene anti-aliasing is requested. */
    Scene(Parent root, double width, double height, boolean depthBuffer, SceneAntialising antiAliasing)
    
    /** Creates a Scene for a specific root Node with a specific size and fill. */
    Scene(Parent root, double width, double height, Paint fill)
    
    /** Creates a Scene for a specific root Node with a fill. */
    Scene(Parent root, Paint fill)

    举个小例子:

    BorderPane root = new BorderPane();
    Scene scene = new Scene(root, 400, 400);


    这里Parent是一个抽象类,全程是javafx.scene.Parent

    java.lang.Object
    javafx.scene.Node // 抽象类
    javafx.scene.Parent // 抽象类

    BorderPane,看命名后缀属于xxPane,在英文中pane是玻璃窗的意思

    public class BorderPane extends Pane
    
    java.lang.Object
    javafx.scene.Node
    javafx.scene.Parent
    javafx.scene.layout.Region
    javafx.scene.layout.Pane
    javafx.scene.layout.BorderPane


    javafx.scene.layout包里面是一系列控制布局的组件

    你的第一个JavaFX程序

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javfx.stage.Stage;
    
    public class MyJavaFX extends Application{
    
    @Override // Override the start method in the Application class
    public void start(Stage primaryStage){
    // Create a scene and place a button in the scene
    Button bOK = new Button("OK");
    Scene scene = new Scene(bOK, 200, 250);
    primaryStage.setTitle("MyJavaFX"); // Set the stage title
    primaryStage.setScene(scene); // Place the scene in the stage
    primaryStage.show(); // Display the stage
    }
    
    /**
    * The main method is only needed for the IDE with limited
    * JavFX support. Not needed for running from the command line.
    */
    public static void main(String[] args){
    Application.launch(args);
    }

    回忆前面讲过的知识,让我们把思路再次理清……

    start()方法
    我们继承了Application类后就要覆写start(Stage primaryStage)方法

    @Override
    public void start(Stage primaryStage){
    // code goes here...
    }

    start()方法传入的Stage对象在JavaFX程序加载时被加载(JavaFX的Stage是顶层容器)

    main()方法
    既然不是控制台应用(而是GUI应用),就不需要main()方法

    不过智能的IDE通常会帮你建好main()函数,呵呵

    launch()方法
    看到main()方法中的launch()方法了么?那是用来启动整个Application的(少了它可不行!)

    不过,如果当你的JavaFX程序少了main()函数,没写launch()方法,万能的JVM会帮你自动调用launch()方法,来启动整个应用程序


    在luanch()方法被调用后,程序跑起来了,JVM会用默认无参数构造方法构造一个Application子类(就是当前继承了Application类的类)对象实例,并调用start()方法

    statr()方法会自动将用户接口(UI)放到场景(scene)中


    前面讲到,Stage是JavaFX的顶层容器,很自然的想到如果构造多个Stage对象,能达到多窗口的效果(就是这样!)

    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.stage.Stage;
    
    public class MutipleStageDemo extends Application{
    
    @Override
    public void start(Stage primaryStage){
    // Create a scene and place a button in the scene
    Scene scene = new Scene(new Button("OK"), 200, 250);
    primaryStage.setTitle("MyJavaFX"); // Set the stage title
    primaryStage.setScene(scene); // Place the scene in the stage
    primaryStage.show(); // Display the stage
    
    Stage stage = new Stage(); // Create a new stage
    stage.setTitle("Second Stage"); // Set the stage title
    // Set a scene with a button in the stage
    stage.setScene(new Scene(new Button("New Stage"), 200, 250));
    stage.show();
    }
    }

    ————————————————
    版权声明:本文为CSDN博主「鸾林居士」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/abc_12366/java/article/details/79966055

  • 相关阅读:
    程序的本质复杂性和元语言抽象
    编译器词法消歧设计
    关于分工的思考 (Thoughts on Division of Labor)
    数据即代码
    类型的本质和函数式实现
    二叉树迭代器算法
    kvm虚拟化存储管理
    kvm网络虚拟化管理
    kvm虚拟机的四种网络模式
    Linux 中交换空间 (swap)应该分多大才好?
  • 原文地址:https://www.cnblogs.com/yuluoxingkong/p/13175918.html
Copyright © 2020-2023  润新知