java中borderpane_BorderPane

本文介绍BorderPane布局在JavaFX中的应用,演示如何通过setTop、setBottom等方法将节点放置于窗格的不同区域,并提供了一个具体示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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窗口,如下所示。

1495a737e6c496d6bbd0dff9598e8bc0.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值