JavaFX |边界窗格类

BorderPane类是JavaFX的一部分。BorderPane类将其子类放置在顶部、底部、中心、左侧和右侧位置。BorderPane在五个位置布置每个子集合,而不考虑子集合的可见属性值。未托管的子集合将被忽略。BorderPane类继承Pane类。 类的构造函数:

null
  1. 边界窗格() :创建一个新的边框窗格。
  2. 边界窗格(节点c) :创建中心有指定节点的新边框窗格。
  3. 边框窗格(节点中心、节点顶部、节点右侧、节点底部、节点左侧) :使用给定节点创建边框窗格布局,以用于边框窗格的每个主布局区域。

常用方法:

方法 解释
getAlignment(节点c) 返回节点的对齐方式。
getBottom() 返回边框窗格的底部节点。
getCenter() 返回边框窗格的中心节点。
getLeft() 返回边框窗格的左侧节点。
getRight() 返回边框窗格的右侧节点。
getTop() 返回边框窗格的顶部节点。
设置对齐(节点c,位置v) 设置节点c到位置v的对齐方式。
setBottom(节点v) 设置边框窗格的底部节点。
设置中心(节点v) 设置边框窗格的中心节点。
setLeft(节点v) 设置边框窗格的左侧节点。
setRight(节点v) 设置边框窗格的右侧节点。
机顶盒(节点v) 设置边框窗格的顶部节点。

下面的程序演示了BorderPane类的使用:

  1. 创建边框窗格并将其添加到舞台的Java程序: 在这个程序中,我们创建一个名为 标签 .现在创建一个名为 边界窗格 。我们会将此标签添加到其中心的边框窗格布局中。将边框窗格添加到场景,并将此场景添加到舞台,然后显示舞台以显示最终结果。

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);
}
}


输出:

图片[1]-JavaFX |边界窗格类-yiteyi-C++库

  1. Java程序创建边框窗格,添加中间、顶部、底部、左侧和右侧组件,并将其添加到舞台: 在这个程序中,我们创建名为 标签中心 , 贴上标签 , 在底部贴标签 , 给你贴上正确的标签 , 左标签 .现在创建一个名为 边界窗格 。我们将在边框窗格的中心、顶部、底部、右侧和左侧添加此标签。使用将标签的对齐设置为居中 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);
}
}


输出:

图片[2]-JavaFX |边界窗格类-yiteyi-C++库

注: 以上程序可能无法在联机IDE中运行,请使用脱机编译器。 参考: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/BorderPane.html

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享