HIDE NAV

JavaFX Horizontal and Vertical Layout (HBox and VBox)

This program shows some basic use of HBox and VBox layout containers.

Main.java


package ceccs;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class JFXHorizAndVertLayout extends Application {

    public static void main(String[] args){
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        double boxSize = 30;
        Rectangle box1 = new Rectangle(boxSize,boxSize*2, Color.RED);
        Rectangle box2 = new Rectangle(boxSize,boxSize, Color.GREEN);
        Rectangle box3 = new Rectangle(boxSize,boxSize*3, Color.BLUE);
        Rectangle box4 = new Rectangle(boxSize*2,boxSize*2, Color.RED);
        Rectangle box5 = new Rectangle(boxSize,boxSize, Color.GREEN);
        Rectangle box6 = new Rectangle(boxSize*3,boxSize, Color.BLUE);

        HBox rowOne = new HBox();
        rowOne.setAlignment(Pos.CENTER);
        rowOne.setSpacing(boxSize/2);
        rowOne.getChildren().add(box1);
        rowOne.getChildren().add(box2);
        rowOne.getChildren().add(box3);

        VBox colOne = new VBox();
        colOne.setAlignment(Pos.BASELINE_CENTER);
        colOne.setSpacing(boxSize*2);
        colOne.setPadding(new Insets(boxSize/4));
        colOne.getChildren().add(box4);
        colOne.getChildren().add(box5);
        colOne.getChildren().add(box6);
        colOne.getChildren().add(rowOne);

        Scene scn = new Scene(colOne, 400,400);
        primaryStage.setScene(scn);
        primaryStage.show();

    }
}