JavaFX | Класс VBox
VBox является частью JavaFX. VBox размещает своих дочерних элементов в виде вертикальных столбцов. Если для vbox установлены границы и / или отступы, то содержимое будет размещено внутри этих вставок. Класс VBox расширяет класс Pane .
Конструктор класса:
- VBox () : создает макет VBox с интервалом = 0 и выравниванием по TOP_LEFT.
- VBox (double s) : создает новый VBox с указанным интервалом между дочерними элементами.
- VBox (double s, Node… c) : создает новый VBox с указанными узлами и промежутками между ними.
- VBox (Узел… c) : создает макет VBox с интервалом = 0.
Часто используемые методы:
Метод | Объяснение |
---|---|
getAlignment () | Возвращает значение выравнивания свойства. |
getSpacing () | Возвращает интервал между дочерними элементами. |
setAlignment (значение Pos) | Устанавливает выравнивание VBox. |
getChildren () | Возвращает узлы в VBox. |
Below programs illustrate the use of VBox class:
- Java Program to create a VBox and add it to the stage: In this program we will create a VBox named vbox. We will create a label and add it to the vbox. We will also create some buttons and add them to the VBox using the getChildren().add() function. Now create a scene and add the vbox to the scene and add the scene to the stage and call show() function to display the final results.
// Java Program to create a VBox
// and add it to the stage
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.canvas.*;
import
javafx.scene.web.*;
import
javafx.scene.Group;
public
class
VBOX_1
extends
Application {
// launch the application
public
void
start(Stage stage)
{
try
{
// set title for the stage
stage.setTitle(
"VBox"
);
// create a VBox
VBox vbox =
new
VBox();
// create a label
Label label =
new
Label(
"this is VBox example"
);
// add label to vbox
vbox.getChildren().add(label);
// add buttons to VBox
for
(
int
i =
0
; i <
10
; i++)
{
vbox.getChildren().add(
new
Button(
"Button "
+ (
int
)(i +
1
)));
}
// create a scene
Scene scene =
new
Scene(vbox,
300
,
300
);
// set the scene
stage.setScene(scene);
stage.show();
}
catch
(Exception e) {
System.out.println(e.getMessage());
}
}
// Main Method
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
Output:
- Java Program to create a VBox, add spaces between its elements and add it to the stage: In this program we will create a VBox named vbox. We will 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 vbox. To add some buttons to the VBox use the getChildren().add() function. Finally, create a scene and add the vbox to the scene and add the scene to the stage and call show() function to display the final results.
// Java Program to create a VBox, add
// spaces between its elements and add
// it to the stage
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.canvas.*;
import
javafx.scene.web.*;
import
javafx.scene.Group;
public
class
VBOX_2
extends
Application {
// launch the application
public
void
start(Stage stage)
{
try
{
// set title for the stage
stage.setTitle(
"VBox"
);
// create a VBox
VBox vbox =
new
VBox(
10
);
// create a label
Label label =
new
Label(
"this is VBox example"
);
// add label to vbox
vbox.getChildren().add(label);
// add buttons to VBox
for
(
int
i =
0
; i <
5
; i++)
{
vbox.getChildren().add(
new
Button(
"Button "
+ (
int
)(i +
1
)));
}
// create a scene
Scene scene =
new
Scene(vbox,
300
,
300
);
// set the scene
stage.setScene(scene);
stage.show();
}
catch
(Exception e) {
System.out.println(e.getMessage());
}
}
// Main Method
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
Output:
- Java Program to create a VBox, add spaces between its elements, set an alignment and add it to the stage: In this program we will create a VBox named vbox. We will set the spacing by passing a double value of space as an argument to the constructor. Set the alignment of the VBox using the setAlignment() function. Then create a label and add it to the vbox. Add some buttons to the VBox using the getChildren().add() function. Finally, create a scene and add the vbox to the scene and add the scene to the stage and call show() function to display the final results.
// Java Program to create a VBox, add spaces
// between its elements, set an alignment
// and add it to the stage
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.scene.Group;
import
javafx.geometry.Pos;
public
class
VBOX_3
extends
Application {
// launch the application
public
void
start(Stage stage)
{
try
{
// set title for the stage
stage.setTitle(
"VBox"
);
// create a VBox
VBox vbox =
new
VBox(
10
);
// create a label
Label label =
new
Label(
"this is VBox example"
);
// add label to vbox
vbox.getChildren().add(label);
// set alignment
vbox.setAlignment(Pos.CENTER);
// add buttons to VBox
for
(
int
i =
0
; i <
5
; i++)
{
vbox.getChildren().add(
new
Button(
"Button "
+ (
int
)(i +
1
)));
}
// create a scene
Scene scene =
new
Scene(vbox,
300
,
300
);
// set the scene
stage.setScene(scene);
stage.show();
}
catch
(Exception e) {
System.out.println(e.getMessage());
}
}
// Main Method
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
Output:
Note: The above programs might not run in an online IDE please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/VBox.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.