欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

javafx_流式布局_网格布局

程序员文章站 2022-06-19 08:46:27
package Lesson16;//流式布局//网格布局import javafx.application.Application;import javafx.geometry.Orientation;import javafx.geometry.Pos;import javafx.scene.Scene;import javafx.scene.control.Button;import javafx.scene.layout.FlowPane;import javafx.scene.l...

jdk1.8

//流式布局
//网格布局
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class Test extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Button b1 = new Button("Button1");
        Button b2 = new Button("Button2");
        Button b3 = new Button("Button3");
        Button b4 = new Button("Button4");
        Button b5 = new Button("Button5");
        Button b6 = new Button("Button6");
        Button b7 = new Button("Button7");
        Button b8 = new Button("Button8");
        
        //流式布局
/*
        FlowPane flow = new FlowPane();
        flow.setStyle("-fx-background-color:#EE6AA7");

        flow.getChildren().addAll(b1,b2,b3,b4,b5,b6,b7,b8);
        flow.setAlignment(Pos.CENTER);//对齐方式
//        flow.setOrientation(Orientation.VERTICAL);//排列方式
        flow.setHgap(10);//水平间隔
        flow.setVgap(10);//垂直间隔
        Scene scene = new Scene(flow);
 */

        //网格布局
        GridPane grid = new GridPane();
        grid.setStyle("-fx-background-color:#EE6AA7");

        grid.add(b1, 0, 0);
        grid.add(b2, 1, 0);
        grid.add(b3, 2, 0);
        grid.add(b4, 3, 0);
        grid.add(b5, 0, 1);
        grid.add(b6, 1, 1);
        grid.add(b7, 2, 1);
        grid.add(b8, 3, 1);

        grid.setAlignment(Pos.CENTER);//对齐方式
//        grid.setOrientation(Orientation.VERTICAL);//排列方式
        grid.setHgap(10);//水平间隔
        grid.setVgap(10);//垂直间隔

		//单独设置间距
//		grid.getColumnConstraints().add(new ColumnConstraints(100));
//      grid.getRowConstraints().add(new RowConstraints(100));

		//遍历组件,可以用lambda表达式,可以用toArray
        grid.getChildren().forEach(new Consumer<Node>() {
            @Override
            public void accept(Node node) {
                Button bu = (Button)node;
                System.out.println(bu.getText());
            }
        });
        /*
        grid.getChildren().forEach((item) -> {
            Button bu = (Button)item;
            System.out.println(bu.getText());
        });
         */

        Scene scene = new Scene(grid);

        primaryStage.setScene(scene);
        primaryStage.setTitle("JavaFX");
        primaryStage.setHeight(800);
        primaryStage.setWidth(800);
        primaryStage.show();
    }
}

javafx_流式布局_网格布局
参考:
JavaF视频教程第18课 GridPane布局类 https://www.bilibili.com/video/BV1wW411276n

本文地址:https://blog.csdn.net/qq_43750882/article/details/109960719

相关标签: 笔记