stackpane将所有子空间全部堆叠起来,先get的children在下面,后get的在上面,虽然不显示但是依然存在,可以用下面的foreach方法循环打印控件名称来确定,最后还用了lambda表达式,不会的可以用foreach。
package sample;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import java.util.function.Consumer;
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");
Button b2 = new Button("b2");
Button b3 = new Button("b3");
Button b4 = new Button("b4");
Button b5 = new Button("b5");
Button b6 = new Button("b6");
Button b7 = new Button("b7");
Button b8 = new Button("b8");
StackPane stkp = new StackPane();
stkp.setStyle("-fx-background-color: dodgerblue");
stkp.getChildren().addAll(b1, b2, b3, b4, b5);///累加在上
//三个重要方法::内边距、外边距、对齐方式
stkp.setPadding(new Insets(10));
stkp.setMargin(b1, new Insets(15));//设置b1的外边距,会使得当前行的所有控件水平排列
stkp.setAlignment(Pos.BOTTOM_CENTER);//向下居中
//循环获取所有的组件名字----方法1
stkp.getChildren().forEach(new Consumer<Node>() {
@Override
public void accept(Node node) {
Button bu = (Button) node;
System.out.println(bu.getText());
}
});
// //循环获取所有的组件名字----方法2--lambda表达式()I改成啥都可,不会可以用上面那种
// stkp.getChildren().forEach((I)->{
// Button bu = (Button) I;
// System.out.println(I);
// });
Scene scene = new Scene(stkp);
primaryStage.setScene(scene);
primaryStage.setTitle("Java FX - StackPane ");
primaryStage.setWidth(800);
primaryStage.setHeight(800);
primaryStage.show();
}
}