JavaFX Group

104 篇文章 5 订阅

The JavaFX Group component is a container component which applies no special layout to its children. All child components (nodes) are positioned at 0,0 . A JavaFX Group component is typically used to apply some effect or transformation to a set of controls as a whole - as a group. If you need some layout to the children inside the Group, nest them inside layout components and add the layout components to the Group. The JavaFX Group component is represented by the class javafx.scene.Group .

Creating a Group

You create a JavaFX Group instance via its constructor. Here is a JavaFX Group instantiation example:

Group group = new Group();

Adding Components to a Group

You can add components to a JavaFX Group by obtaining its list of children and adding the children to that list. Here is an example of adding children to a JavaFX Group:

Button button1 = new Button("Button Number 1");
Button button2 = new Button("Button 2");

Group group = new Group();

group.getChildren().add(button1);
group.getChildren().add(button2);

Adding a Group to the Scene Graph

To make a JavaFX Group instance visible it must be added to the JavaFX scene graph. That means adding the Group instance to a Scene object or adding the Group instance to a layout component which is then added to a Scene object.

Here is an example of adding a JavaFX Group instance to the JavaFX scene graph:

package com.jenkov.javafx.layouts;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;

public class GroupExperiments extends Application  {

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("HBox Experiment 1");

        Button button1 = new Button("Button Number 1");
        Button button2 = new Button("Button 2");

        Group group = new Group();

        group.getChildren().add(button1);
        group.getChildren().add(button2);

        Scene scene = new Scene(group, 200, 100);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

The application resulting from running the above code would look similar to this:

As you can see, the two buttons are positioned on top of each other, because both buttons are positioned at 0,0 inside the Group component.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值