In JavaFX, the HBox
class is a layout container that arranges its children horizontally in a single row. The HBox
layout is useful for arranging buttons, text fields, and other controls in a horizontal row.
Here's an example of how you can use the HBox
layout in a JavaFX application:
import javafx.application.Application; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.layout.HBox; import javafx.stage.Stage; public class Main extends Application { public void start(Stage stage) { // create three buttons Button button1 = new Button("Button 1"); Button button2 = new Button("Button 2"); Button button3 = new Button("Button 3"); // create an hbox layout HBox hbox = new HBox(); // add the buttons to the hbox layout hbox.getChildren().addAll(button1, button2, button3); // set the spacing and padding of the hbox layout hbox.setSpacing(10); hbox.setPadding(new Insets(10, 10, 10, 10)); // create a scene and add the hbox layout to it Scene scene = new Scene(hbox, 300, 100); // set the title of the stage and show it stage.setTitle("HBox Example"); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }Source:www.lautturi.com
This code creates three buttons and adds them to an HBox
layout. It then sets the spacing and padding of the HBox
layout and adds it to a Scene
. Finally, it sets the title of a Stage
and displays the scene.
The HBox
layout arranges the buttons in a horizontal row, with the specified spacing and padding between them. You can also use the setAlignment()
method to set the alignment of the children within the HBox
, and the setFillHeight()
method to specify whether the children should be resized to fill the vertical space of the HBox
.
For more information about layouts and other UI components in JavaFX, you can refer to the JavaFX documentation (https://openjfx.io/javadoc/).