📜  JavaFX-布局窗格(容器)

📅  最后修改于: 2020-11-14 07:18:20             🧑  作者: Mango


在场景中构造完所有必需的节点后,我们通常会按顺序排列它们。

容器内组件的这种排列方式称为容器布局。我们也可以说我们遵循一种布局,因为它包括将所有组件放置在容器内的特定位置。

JavaFX提供了几种预定义的布局,例如HBox,VBox,Border Pane,Stack Pane,Text Flow,Anchor Pane,Title Pane,Grid Pane,Flow Panel等。

上面提到的每个布局都由一个类表示,所有这些类都属于包javafx.layout 。名为Pane的类是JavaFX中所有布局的基类。

创建布局

要创建布局,您需要-

  • 创建节点。
  • 实例化所需布局的相应类。
  • 设置布局的属性。
  • 将所有创建的节点添加到布局。

创建节点

首先,通过实例化它们各自的类来创建JavaFX应用程序的必需节点。

例如,如果您要有一个文本字段和两个按钮,即在HBox布局中播放和停止-您将必须首先创建这些节点,如以下代码块所示-

//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");

实例化相应的类

创建节点(并完成对它们的所有操作)之后,实例化所需布局的类。

例如,如果要创建Hbox布局,则需要实例化此类,如下所示。

HBox hbox = new HBox();

设置布局的属性

实例化类后,您需要使用各自的setter方法设置布局的属性。

例如-如果要在HBox布局中创建的节点之间设置间距,则需要将值设置为名为interval的属性。这可以通过使用setter方法setSpacing()来完成,如下所示:

hbox.setSpacing(10);

将形状对象添加到组

最后,您需要通过将形状的对象作为构造函数的参数传递到组中,如下所示。

//Creating a Group object  
Group root = new Group(line);

布局窗格

以下是JavaFX提供的各种布局窗格(类)。这些类存在于包javafx.scene.layout中

S.No Shape & Description
1 HBox

The HBox layout arranges all the nodes in our application in a single horizontal row.

The class named HBox of the package javafx.scene.layout represents the text horizontal box layout.

2 VBox

The VBox layout arranges all the nodes in our application in a single vertical column.

The class named VBox of the package javafx.scene.layout represents the text Vertical box layout.

3 BorderPane

The Border Pane layout arranges the nodes in our application in top, left, right, bottom and center positions.

The class named BorderPane of the package javafx.scene.layout represents the border pane layout.

4 StackPane

The stack pane layout arranges the nodes in our application on top of another just like in a stack. The node added first is placed at the bottom of the stack and the next node is placed on top of it.

The class named StackPane of the package javafx.scene.layout represents the stack pane layout.

5 TextFlow

The Text Flow layout arranges multiple text nodes in a single flow.

The class named TextFlow of the package javafx.scene.layout represents the text flow layout.

6 AnchorPane

The Anchor pane layout anchors the nodes in our application at a particular distance from the pane.

The class named AnchorPane of the package javafx.scene.layout represents the Anchor Pane layout.

7 TilePane

The Tile Pane layout adds all the nodes of our application in the form of uniformly sized tiles.

The class named TilePane of the package javafx.scene.layout represents the TilePane layout.

8 GridPane

The Grid Pane layout arranges the nodes in our application as a grid of rows and columns. This layout comes handy while creating forms using JavaFX.

The class named GridPane of the package javafx.scene.layout represents the GridPane layout.

9 FlowPane

The flow pane layout wraps all the nodes in a flow. A horizontal flow pane wraps the elements of the pane at its height, while a vertical flow pane wraps the elements at its width.

The class named FlowPane of the package javafx.scene.layout represents the Flow Pane layout.