关于Group组件的简单介绍
- 首先,Group组件自己并不是一个“布局”类的组件,它只是一个容器,没有提供调解内部组件展示位置的方法,也就是说,当我们将多个组件(比如button)放在group中时,他们(根据流式规则?)会全部挤在窗口的左上角,当然,假如我们没有设置组件的大小的话,那么大小也是默认的。
- 假如我们想要调解组件的位置,可以使用组件的方法来调解,比如Button的setLayoutX()和setLayoutY()
- 涉及到的类
1.Group类,一个容器,可以装一些组件
- 涉及到的方法(今后以后无参构造方法,不在介绍,默认已经使用)
1.getChildren().add() 将组件添加进入它的孩子列表
- Group group=new Group();
- group.getChildren().add();
复制代码 2.getChildren().addAll() 将多个组件一并添加进入group中(这里以button为例)- group.getChildren().addAll(b1,b2,b3)
复制代码 比如下面的例子- /*这里我们只粘贴了application的start方法,启动类不在展示,从此以后也是如此*/
- @Override
- public void start(Stage primaryStage) throws Exception {
- Button button1=new Button();
- //Button的方法preHeight(),可以改变按钮大小
- button1.prefHeight(20);
- button1.prefWidth(40);
- Button button2=new Button();
- Button button3=new Button();
- Group root=new Group();
- root.getChildren().add(button1);
- root.getChildren().add(button2);
- root.getChildren().add(button3);
- Scene scene=new Scene(root);
- primaryStage.setScene(scene);
- primaryStage.setTitle("JavaFx");
- primaryStage.setWidth(800);
- primaryStage.setHeight(800);
- primaryStage.setOpacity(0.7);
- primaryStage.show();
- }
复制代码
我们使用Button的方法,调解位置之后- import java.net.URL;
- import javafx.application.Application;
- import javafx.application.HostServices;
- import javafx.scene.Cursor;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.image.Image;
- import javafx.stage.Modality;
- import javafx.stage.Stage;
- import javafx.stage.StageStyle;
- public class Lanch extends Application {
- @Override
- public void start(Stage primaryStage) throws Exception {
- Button button1=new Button("b1");
- button1.setPrefWidth(50);
- button1.setPrefHeight(25);
- button1.setLayoutX(0);
- button1.setLayoutY(0);
- Button button2=new Button("b2");
- button2.setPrefWidth(50);
- button2.setPrefHeight(25);
- button2.setLayoutX(60);
- button2.setLayoutY(0);
- Button button3=new Button("b3");
- button3.setPrefWidth(50);
- button3.setPrefHeight(25);
- button3.setLayoutX(120);
- button3.setLayoutY(0);
-
- Group root=new Group();
- root.getChildren().add(button1);
- root.getChildren().add(button2);
- root.getChildren().add(button3);
- Scene scene=new Scene(root);
- primaryStage.setScene(scene);
- primaryStage.setTitle("JavaFx");
- primaryStage.setWidth(800);
- primaryStage.setHeight(800);
- primaryStage.show();
- }
- }
复制代码
我们发现位置已经被调解好了
3.getChildren().clear()清理部署在它上面的所有组件,getChildren().remove(int index)按苏索引清除掉某个组件
我们实现点击b1,b3消散的逻辑- @Override
- public void start(Stage primaryStage) throws Exception {
-
- Button button1=new Button("b1");
- button1.setPrefWidth(50);
- button1.setPrefHeight(25);
- button1.setLayoutX(0);
- button1.setLayoutY(0);
-
- Button button2=new Button("b2");
- button2.setPrefWidth(50);
- button2.setPrefHeight(25);
- button2.setLayoutX(60);
- button2.setLayoutY(0);
- Button button3=new Button("b3");
- button3.setPrefWidth(50);
- button3.setPrefHeight(25);
- button3.setLayoutX(120);
- button3.setLayoutY(0);
- Group root=new Group();
- root.getChildren().add(button1);
- root.getChildren().add(button2);
- root.getChildren().add(button3);
-
- /*
- * 实现一个点击b1,b3消失的逻辑
- */
- button1.setOnMouseClicked(e->{
- root.getChildren().remove(2);
- });//使用lamda表达式简化匿名类的书写
-
- Scene scene=new Scene(root);
- primaryStage.setScene(scene);
- primaryStage.setTitle("JavaFx");
- primaryStage.setWidth(800);
- primaryStage.setHeight(800);
- primaryStage.show();
- }
复制代码 点击按钮前

点击按钮后

特别注意:我们假如修改group的一些属性,那么它的子组件的对应属性也会跟着改变
一些代码- [/code][i]last but not least[/i]:以下是JavaFx文档中Group类文档*
- [url=https://openjfx.cn/javadoc/22/javafx.graphics/javafx/scene/Group.html]https://openjfx.cn/javadoc/22/javafx.graphics/javafx/scene/Group.html[/url]
- 4.[i]contains(double localX, double localY)[/i]是Group的爷爷类中实现的方法,功能是判断点(localX,localY)上是否是子组件的左上角点坐标
- 代码如下
- [code]System.out.println(root.contains(0,0));
复制代码
5.getChildren().toArray()将子组件以数组的形式拿出来
代码如下- /*
- * 将Group中的子组件拿出来
- */
- Object[] object=root.getChildren().toArray();
- for(Object o:object){
- Button button=(Button)(o);
- button.setPrefHeight(75);
- }
复制代码
6.设置一个监听事件,监听group中的组件数目改变
[code] root.getChildren().addListener(new ListChangeListener() { @Override public void onChanged(Change |