目录

BorderPane

如果我们使用BorderPane,则节点将排列在Top,Left,Right,Bottom和Center位置。

javafx.scene.layout名为BorderPane的类表示BorderPane。

该类包含五个属性,包括 -

  • bottom - 此属性属于Node类型,它表示放置在BorderPane底部的节点。 您可以使用setter方法setBottom()为此属性设置值。

  • center - 此属性是Node类型,它表示放置在BorderPane中心的节点。 您可以使用setter方法setCenter()为此属性设置值。

  • left - 此属性是Node类型,它表示放置在BorderPane左侧的节点。 您可以使用setter方法setLeft()为此属性设置值。

  • right - 此属性属于Node类型,它表示位于BorderPane右侧的节点。 您可以使用setter方法setRight()为此属性设置值。

  • top - 此属性是Node类型,它表示放置在BorderPane顶部的节点。 您可以使用setter方法setTop()为此属性设置值。

除此之外,本课程还提供以下方法 -

  • setAlignment() - 此方法用于设置属于此窗格的节点的对齐方式。 此方法接受节点和优先级值。

例子 (Example)

以下程序是BorderPane布局的示例。 在此,我们在顶部,底部,右侧,左侧和中心位置插入五个文本字段。

将此代码保存在名为BorderPaneExample.java的文件中。

import javafx.application.Application; 
import javafx.collections.ObservableList; 
import javafx.scene.Scene; 
import javafx.scene.control.TextField; 
import javafx.scene.layout.BorderPane; 
import javafx.stage.Stage; 
public class BorderPaneExample extends Application { 
   @Override 
   public void start(Stage stage) {      
      //Instantiating the BorderPane class  
      BorderPane bPane = new BorderPane();   
      //Setting the top, bottom, center, right and left nodes to the pane 
      bPane.setTop(new TextField("Top")); 
      bPane.setBottom(new TextField("Bottom")); 
      bPane.setLeft(new TextField("Left")); 
      bPane.setRight(new TextField("Right")); 
      bPane.setCenter(new TextField("Center")); 
      //Creating a scene object 
      Scene scene = new Scene(bPane);  
      //Setting title to the Stage
      stage.setTitle("BorderPane Example"); 
      //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文件。

javac BorderPaneExample.java 
java BorderPaneExample

执行时,上面的程序生成一个JavaFX窗口,如下所示。

BorderPane
↑回到顶部↑
WIKI教程 @2018