JavaFX | Класс HBox
HBox является частью JavaFX. HBox размещает своих дочерних элементов в виде горизонтальных столбцов. Если для HBox установлены границы и / или отступы, то содержимое будет размещено внутри этих вставок. Класс HBox расширяет класс Pane .
Конструкторы класса:
- HBox () : создает объект HBox без узлов.
- HBox (double s) : создает HBox с интервалом между узлами.
Часто используемые методы:
| Метод | Объяснение |
|---|---|
| getAlignment () | Возвращает значение выравнивания свойства. |
| getSpacing () | Возвращает интервал между дочерними элементами. |
| setAlignment (значение Pos) | Устанавливает выравнивание HBox. |
| getChildren () | Возвращает узлы в HBox. |
Below programs illustrate the use of HBox class:
- Java Program to create a HBox and add it to the stage: In this program we will create a HBox named hbox. Now create a label and add it to the hbox. We will also create some buttons and add them to the HBox using the getChildren().add() function. Now create a scene and add the hbox to the scene and add the scene to the stage and call show() function to display the final results.
// Java Program to create a HBox// and add it to the stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.canvas.*;importjavafx.scene.web.*;importjavafx.scene.Group;publicclassHBOX_1extendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("HBox");// create a HBoxHBox hbox =newHBox();// create a labelLabel label =newLabel("this is HBox example");// add label to hboxhbox.getChildren().add(label);// add buttons to HBoxfor(inti =0; i <10; i++){hbox.getChildren().add(newButton("Button "+ (int)(i +1)));}// create a sceneScene scene =newScene(hbox,800,300);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:

- Java Program to create a HBox, add spaces between its elements and add it to the stage: In this program we will create a HBox named hbox. Set the spacing by passing a double value of space as an argument to the constructor. Now create a label and add it to the hbox. To add some buttons to the HBox use the getChildren().add() function. Finally, create a scene and add the hbox to the scene and add the scene to the stage and call show() function to display the final results.
// Java Program to create a HBox, add// spaces between its elements and add// it to the stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.canvas.*;importjavafx.scene.web.*;importjavafx.scene.Group;publicclassHBOX_2extendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("HBox");// create a HBoxHBox hbox =newHBox(10);// create a labelLabel label =newLabel("this is HBox example");// add label to hboxhbox.getChildren().add(label);// add buttons to HBoxfor(inti =0; i <5; i++){hbox.getChildren().add(newButton("Button "+ (int)(i +1)));}// create a sceneScene scene =newScene(hbox,800,300);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:

- Java Program to create a HBox, add spaces between its elements, set an alignment and add it to the stage: In this program we will create a HBox named hbox. Set the spacing by passing a double value of space as an argument to the constructor. Set the alignment of the HBox using the setAlignment() function. Then create a label and add it to the hbox. Add some buttons to the HBox using the getChildren().add() function. Finally, create a scene and add the hbox to the scene and add the scene to the stage and call show() function to display the final results.
// Java Program to create a HBox, add spaces// between its elements, set an alignment// and add it to the stageimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.canvas.*;importjavafx.scene.web.*;importjavafx.scene.Group;importjavafx.geometry.*;publicclassHBOX_3extendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("HBox");// create a HBoxHBox hbox =newHBox(10);// setAlignmenthbox.setAlignment(Pos.CENTER);// create a labelLabel label =newLabel("this is HBox example");// add label to hboxhbox.getChildren().add(label);// add buttons to HBoxfor(inti =0; i <5; i++){hbox.getChildren().add(newButton("Button "+ (int)(i +1)));}// create a sceneScene scene =newScene(hbox,800,300);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:

Note: The above programs might not run in an online IDE please use an offline compiler.
Reference: https://docs.oracle.com/javafx/2/api/javafx/scene/layout/HBox.html
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.