Java FX - StackPane

 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();


    }


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值