JavaFX |组类

Group类是JavaFX的一部分。组包含节点数。一个群体将承担其子女的集体界限,不能直接调整其规模。组类继承父类。

null

类的构造函数:

  1. 组() :构建一个新组。
  2. 组(集合) (儿童) :使用指定节点构造新组。
  3. 组(节点…c) :使用指定节点构造新组。

常用方法:

方法 解释
getChildren() 返回组的子级。
isAutoSizeChildren() 获取属性autoSizeChildren的值。
最小高度(双倍宽度) 返回节点在布局计算中使用的最小高度。
最小宽度(双高) 返回节点在布局计算中使用的最小宽度。
高度(双倍宽度) 组将首选高度定义为其布局边界的高度。
宽度(双倍高度) 组将首选宽度定义为其布局边界的宽度。
setAutoSizeChildren(布尔v) 设置属性autoSizeChildren的值。

下面的程序说明了Group class的用法:

  1. 创建组并将其添加到舞台的Java程序: 在这个程序中,我们将创建一个名为 标签 ,和一个名为 圆圈 .现在创建一个组名 并使用 getChildren()。添加() 作用创建场景并将组添加到场景中。将场景添加到舞台并显示舞台以查看最终结果。

    // Java Program to create a Group
    // and add it to the stage
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.canvas.*;
    import javafx.scene.web.*;
    import javafx.scene.Group;
    import javafx.scene.shape.*;
    public class Group_1 extends Application {
    // launch the application
    public void start(Stage stage)
    {
    try {
    // set title for the stage
    stage.setTitle( "Group" );
    // create a Group
    Group group = new Group();
    // create a label
    Label label = new Label( "this is Group example" );
    // add label to group
    group.getChildren().add(label);
    // circle
    Circle c = new Circle( 100 , 100 , 30 );
    // add Circle to Group
    group.getChildren().add(c);
    // create a scene
    Scene scene = new Scene(group, 400 , 300 );
    // set the scene
    stage.setScene(scene);
    stage.show();
    }
    catch (Exception e) {
    System.out.println(e.getMessage());
    }
    }
    // Main Method
    public static void main(String args[])
    {
    // launch the application
    launch(args);
    }
    }

    
    

    输出:

    图片[1]-JavaFX |组类-yiteyi-C++库

  2. Java程序创建一个组,将“自动调整大小”设置为true,并将其添加到后台: 在这个程序中,我们将创建一个名为 标签 还有一个叫 圆圈 .然后我们将创建一个组名 并使用 getChildren()。添加() 作用使用 setAutoSize() 作用创建场景并将组添加到场景中。将场景添加到舞台并显示舞台以查看最终结果。

    // Java Program to create a Group,
    // set auto resize to true
    // and add it to the stage
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.canvas.*;
    import javafx.scene.web.*;
    import javafx.scene.Group;
    import javafx.scene.shape.*;
    public class Group_2 extends Application {
    // launch the application
    public void start(Stage stage)
    {
    try {
    // set title for the stage
    stage.setTitle( "Group" );
    // create a Group
    Group group = new Group();
    // create a label
    Label label = new Label( "this is Group example" );
    // add label to group
    group.getChildren().add(label);
    // circle
    Circle c = new Circle( 50 , 50 , 30 );
    // set auto resize
    group.setAutoSizeChildren( true );
    // add Circle to Group
    group.getChildren().add(c);
    // create a scene
    Scene scene = new Scene(group, 400 , 300 );
    // set the scene
    stage.setScene(scene);
    stage.show();
    }
    catch (Exception e) {
    System.out.println(e.getMessage());
    }
    }
    // Main Method
    public static void main(String args[])
    {
    // launch the application
    launch(args);
    }
    }

    
    

    输出: 图片[2]-JavaFX |组类-yiteyi-C++库

注: 上述程序可能无法在联机IDE中运行。请使用脱机编译器。

参考: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/Group.html

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享