JavaFX - HBOX、VBOX(水平、垂直布局)

 

 javafx慢慢学习吧建议使用idea敲代码,可以选择颜色方便点

下面代码注释时候看看改变来学习 

package sample;


import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
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("button1");
        Button b2 = new Button("button2");
        Button b3 = new Button("button3");
        Button b4 = new Button("button4");

        AnchorPane anchorPane = new AnchorPane();


        anchorPane.setStyle("-fx-background-color: #783e78");

        HBox box = new HBox();水平排列,子组件多了的话会都显示,但是缩小了。字都会看不见。
//        VBox box = new VBox();垂直排列,子组件多了的话会都显示,并且显示正常,因为会把anp撑大!使用方法与Hbox一致。

        box.setStyle("-fx-background-color: #03e8a0");

        box.getChildren().addAll(b1,b2,b3,b4);
        box.setPrefHeight(500);
        box.setPrefWidth(300);
        box.setPadding(new Insets(20));//会使得按钮距离父控件的边缘为20,上下左右都会,再小的父控件都会撑大
        box.setSpacing(10);///设置子组件的相互之间距离
        box.setMargin(b1,new Insets(10));//b1外边距加10
        box.setAlignment(Pos.BOTTOM_CENTER);按钮下居中,之前设置的B1外边距还保留
        anchorPane.getChildren().add(box);
        Scene scene = new Scene(anchorPane);


        primaryStage.setScene(scene);
        primaryStage.setTitle("Java FX Lesson.HBOX.VBOX");
        primaryStage.setWidth(800);
        primaryStage.setHeight(800);


        primaryStage.show();


    }


}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值