JavaFX HBox create

Introduction

An HBox lays out its children in a single horizontal row.

A VBox lays out its children in a single vertical column.

FlowPane can lay out its children in multiple rows or multiple columns, but an HBox or a VBox can lay out children only in one row or one column.

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class Main extends Application {
  @Override/*from  ww  w.  j  a  v a  2 s.  c  o m*/
  public void start(Stage primaryStage) {
    // Create a border pane
    BorderPane pane = new BorderPane();

    // Place nodes in the pane
    pane.setTop(getHBox());

    Scene scene = new Scene(pane);
    primaryStage.setTitle("demo2s");
    primaryStage.setScene(scene);
    primaryStage.show();
  }

  private HBox getHBox() {
    HBox hBox = new HBox(15);
    hBox.setPadding(new Insets(15, 15, 15, 15));
    hBox.getChildren().add(new Button("HTML"));
    hBox.getChildren().add(new Button("CSS"));
    return hBox;
  }

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



PreviousNext

Related