JavaFX | BorderPane 类
BorderPane 类是 JavaFX 的一部分。 BorderPane 类将其子级放置在顶部、底部、中心、左侧和右侧位置。 BorderPane 将每个子集布置在五个位置,而不管子元素的可见属性值如何,非托管子元素都将被忽略。 BorderPane 类继承 Pane 类。
类的构造函数:
- BorderPane() :创建一个新的边框窗格。
- BorderPane(Node c) : 创建一个以指定节点为中心的新边框窗格。
- BorderPane(Node center, Node top, Node right, Node bottom, Node left) :使用给定的节点创建一个 BorderPane 布局,用于 Border Pane 的每个主要布局区域。
常用方法:Methods Explanation getAlignment(Node c) Returns the alignment of the node. getBottom() Returns the bottom node of the border pane. getCenter() Returns the center node of the border pane. getLeft() Returns the left node of the border pane. getRight() Returns the right node of the border pane. getTop() Returns the top node of the border pane. setAlignment(Node c, Pos v) Sets the alignment of node c to pos v. setBottom(Node v) Sets the bottom node of the border pane. setCenter(Node v) Sets the center node of the border pane. setLeft(Node v) Sets the left node of the border pane. setRight(Node v) Sets the right node of the border pane. setTop(Node v) Sets the top node of the border pane.
下面的程序说明了 BorderPane 类的使用:
- Java程序创建一个 BorderPane 并将其添加到舞台:在这个程序中,我们创建一个名为label的标签。现在创建一个名为borderpane的BorderPane。我们将此标签添加到其中心的 BorderPane 布局中。将边框窗格添加到场景中,并将此场景添加到舞台并显示舞台以显示最终结果。
Java
// Java Program to create a BorderPane
// 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.layout.BorderPane;
import javafx.scene.shape.*;
public class BorderPane_1 extends Application {
// launch the application
public void start(Stage stage)
{
try {
// set title for the stage
stage.setTitle("BorderPane");
// create a label
Label label = new Label("this is BorderPane example");
// create a BorderPane
BorderPane border_pane = new BorderPane(label);
// create a scene
Scene scene = new Scene(border_pane, 400, 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);
}
}
Java
// Java Program to create a BorderPane and
// add Center, top, bottom, left, right
// components 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.layout.BorderPane;
import javafx.scene.shape.*;
import javafx.geometry.*;
public class BorderPane_2 extends Application {
// launch the application
public void start(Stage stage)
{
try {
// set title for the stage
stage.setTitle("BorderPane");
// create a label
Label label_center = new Label("this is BorderPane center");
Label label_top = new Label("this is BorderPane top");
Label label_bottom = new Label("this is BorderPane bottom");
Label label_left = new Label("this is BorderPane left");
Label label_right = new Label("this is BorderPane right");
// create a BorderPane
BorderPane border_pane = new BorderPane(label_center,
label_top, label_right, label_bottom, label_left);
// set alignment
border_pane.setAlignment(label_top, Pos.CENTER);
border_pane.setAlignment(label_bottom, Pos.CENTER);
border_pane.setAlignment(label_left, Pos.CENTER);
border_pane.setAlignment(label_right, Pos.CENTER);
// create a scene
Scene scene = new Scene(border_pane, 400, 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);
}
}
输出:
- Java程序创建一个 BorderPane 并添加 Center、top、bottom、left、right 组件并将其添加到舞台:在这个程序中,我们创建名为label_center 、 label_top 、 label_bottom 、 label_right 、 label_left的标签。现在创建一个名为borderpane的BorderPane。我们将此标签添加到 BorderPane 布局的中心、顶部、底部、右侧、左侧。使用setAlignment()将标签的对齐方式设置为居中。我们将向场景添加边框窗格并将此场景添加到舞台并显示舞台以显示最终结果。
Java
// Java Program to create a BorderPane and
// add Center, top, bottom, left, right
// components 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.layout.BorderPane;
import javafx.scene.shape.*;
import javafx.geometry.*;
public class BorderPane_2 extends Application {
// launch the application
public void start(Stage stage)
{
try {
// set title for the stage
stage.setTitle("BorderPane");
// create a label
Label label_center = new Label("this is BorderPane center");
Label label_top = new Label("this is BorderPane top");
Label label_bottom = new Label("this is BorderPane bottom");
Label label_left = new Label("this is BorderPane left");
Label label_right = new Label("this is BorderPane right");
// create a BorderPane
BorderPane border_pane = new BorderPane(label_center,
label_top, label_right, label_bottom, label_left);
// set alignment
border_pane.setAlignment(label_top, Pos.CENTER);
border_pane.setAlignment(label_bottom, Pos.CENTER);
border_pane.setAlignment(label_left, Pos.CENTER);
border_pane.setAlignment(label_right, Pos.CENTER);
// create a scene
Scene scene = new Scene(border_pane, 400, 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);
}
}
输出:
注意:以上程序可能无法在在线 IDE 中运行,请使用离线编译器。
参考: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/BorderPane.html