package fx.com;
import javafx.application.Application;
import javafx.collections.ListChangeListener;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
Button b1 = new Button("b1");
b1.setLayoutX(100);
b1.setLayoutY(100);
b1.setPrefWidth(50);
b1.setPrefHeight(50);
Button b2 = new Button("b2");
b2.setLayoutX(200);
b2.setLayoutY(100);
b2.setPrefWidth(50);
b2.setPrefHeight(50);
Button b3 = new Button("b3");
b3.setLayoutX(300);
b3.setLayoutY(100);
b3.setPrefWidth(50);
b3.setPrefHeight(50);
Group group = new Group();
// group.getChildren().add(b1);
// group.getChildren().add(b2);
// group.getChildren().add(b3);
group.getChildren().addAll(b1,b2,b3);
group.getChildren().addListener((ListChangeListener<Node>) c -> System.out.println("当前子组件数量="+c.getList().size()));
b1.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Button b4 = new Button("b4");
group.getChildren().add(b4);
}
});
Object[] objects = group.getChildren().toArray();
for (Object o : objects){
Button button = (Button) o;
button.setPrefHeight(20);
button.setPrefWidth(40);
}
Scene scene = new Scene(group);
primaryStage.setScene(scene);
primaryStage.setHeight(600);
primaryStage.setWidth(800);
primaryStage.show();
primaryStage.centerOnScreen();
}
}