Javafx 简明教程
JavaFX - HBox Layout
HBox Layout in JavaFX
HBox ,也称为水平框,是一种布局窗格,它按单个水平行排列 JavaFX 应用程序的所有节点。HBox 布局窗格由 javafx.scene.layout 包中名为 HBox 的类表示。实例化此类以创建 HBox 布局。HBox 类的构造函数如下 −
HBox, also referred to as Horizontal Box, is a layout pane that arranges all the nodes of a JavaFX application in a single horizontal row. The HBox layout pane is represented by a class named HBox of the package javafx.scene.layout. Instantiate this class to create an HBox layout. Constructors of HBox class is as follows −
-
HBox() − It is the default constructor that constructs an HBox layout with 0 spacing.
-
HBox(double spacingVal) − It constructs a new HBox layout with the specified spacing between nodes.
-
HBox(double spacingVal, Node nodes) − This parameterized constructor of HBox class accepts children nodes as well as spacing between them and creates a new HBox layout with the specified components.
-
HBox(Node nodes) − It creates an HBox layout with specified children nodes and 0 spacing.
在下面的快照中,我们可以看到红色框内的 UI 组件被放置在水平布局内 −
In the below snapshot, we can see the UI components inside the red box are placed within a horizontal layout −

HBox 类具有以下属性 −
The HBox class has the following properties −
-
alignment − This property represents the alignment of the nodes in the bounds of the HBox. We can set value to this property using the setter method setAlignment().
-
fillHeight − This property is of Boolean type and on setting this to true, the resizable nodes in the HBox are resized to the height of the HBox. We can set value to this property using the setter method setFillHeight().
-
spacing − This property is of double type and it represents the space between the children of the HBox. We can set value to this property using the setter method setSpacing().
-
padding − It represents the space between the border of HBox and its child nodes. We can set value to this property using the setter method setPadding() which accepts Insets constructor as a parameter value.
除这些外, HBox 类还提供一些方法,如下 −
In addition to these, HBox class also provides a couple of methods, which are as follows −
S.No |
methods & Description |
1 |
setHgrow()*Sets the horizontal grow priority for the child when contained by an HBox. This method accepts a node and a priority value. The possible priority value could be *Policy.ALWAYS, Policy.SOMETIMES and Policy.NEVER. |
2 |
*setMargin()*Using this method, you can set margins to the HBox. This method accepts a node and an object of the Insets class (A set of inside offsets for the 4 side of a rectangular area). |
Example
以下程序是 HBox 布局的示例。在此处,我们插入一个文本字段和两个名为 play 和 stop 的按钮。将此 JavaFX 代码保存在一个名为 HBoxExample.java 的文件中。
The following program is an example of the HBox layout. Here, we are inserting a text field and two buttons named as play and stop. Save this JavaFX code in a file with the name HBoxExample.java.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.scene.layout.HBox;
public class HBoxExample extends Application {
@Override
public void start(Stage stage) {
//creating a text field
TextField textField = new TextField();
//Creating the play button
Button playButton = new Button("Play");
//Creating the stop button
Button stopButton = new Button("stop");
//Instantiating the HBox class
HBox hbox = new HBox();
//Setting the space between the nodes of a HBox pane
hbox.setSpacing(10);
//Adding all the nodes to the HBox
hbox.getChildren().addAll(textField, playButton, stopButton);
//Setting the margin to the nodes
hbox.setMargin(textField, new Insets(20, 20, 20, 20));
hbox.setMargin(playButton, new Insets(20, 20, 20, 20));
hbox.setMargin(stopButton, new Insets(20, 20, 20, 20));
//Creating a scene object
Scene scene = new Scene(hbox, 400, 300);
//Setting title to the Stage
stage.setTitle("Hbox Example in JavaFX");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
使用以下命令,从命令提示符编译并执行已保存的 java 文件。
Compile and execute the saved java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls HBoxExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls HBoxExample
执行后,上述程序会生成如下所示的 JavaFX 窗口。
On executing, the above program generates a JavaFX window as shown below.

Example
在以下示例中,我们将使用 HBox 类的参数化构造函数创建水平布局。将此 JavaFX 代码保存在一个名为 HBoxExample.java 的文件中。
In the following example, we are going to use the parameterized constructor of the HBox class to create a horizontal layout. Save this JavaFX code in a file with the name HBoxExample.java.
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.scene.layout.HBox;
public class HBoxExample extends Application {
@Override
public void start(Stage stage) {
//creating a text field
TextField textField = new TextField();
//Creating the play button
Button playButton = new Button("Play");
//Creating the stop button
Button stopButton = new Button("stop");
//Instantiating the HBox class
HBox box = new HBox(10, textField, playButton, stopButton);
box.setAlignment( Pos.CENTER);
//Creating a scene object
Scene scene = new Scene(box, 400, 300);
//Setting title to the Stage
stage.setTitle("Hbox Example in JavaFX");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
使用以下命令,从命令提示符编译并执行已保存的 java 文件。
Compile and execute the saved java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls HBoxExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls HBoxExample
在执行上述程序时,将生成以下输出。
On executing, the above program generates the following output.
