JavaFX Pane

本文介绍了JavaFX中的Pane类,它是一个布局容器,用于包含其他组件并根据子组件的layoutX和layoutY进行布局。创建Pane实例,添加组件(如Label),并将Pane作为SceneGraph的根节点是本文的重点。

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

The JavaFX Pane class is a layout container which can contain other JavaFX components internally, and lay them out. Actually, the JavaFX Pane class does not actually provide any layout algorithm. The Pane class simply displays the components it contains at the locations the components themselves want to be located. In other words, the Pane class uses the layoutX and layoutY specified by its child components to determine where to display them.

The JavaFX Pane class, javafx.scene.layout.Pane, is a subclass of the JavaFX Region class, so it inherits all of the Region class functionality. That includes functionality like borders, padding, background settings etc.

Create a JavaFX Pane

You create a JavaFX Pane simply via its standard no-arg constructor. Here is an example of creating a JavaFX Pane instance:

Pane pane = new Pane();

Add Items to a JavaFX Pane

You add items JavaFX Pane by obtaining its list of children via getChildren(), and then add the items to that list. Here is an example of adding a JavaFX Label to a JavaFX Pane:

Pane  pane  = new Pane();
pane.getChildren().add(new Label("Hello Pane"));

If you repeat the last line multiple times, you will add multiple Label instances to the Pane. Just keep in mind, that unless you change the layoutX and / or layoutY properties of the added Labels, all the Label instances will be displayed in the same X and Y position - meaning on top of each other.

Adding a JavaFX Pane to the Scene Graph

Here is an example of adding a JavaFX Pane to the JavaFX scene graph - by setting the Pane as the root node of a JavaFX Scene:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class PaneExample extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    public void start(Stage primaryStage) {

        Pane  pane  = new Pane();

        pane.getChildren().add(new Label("Hello Pane"));

        Scene scene = new Scene(pane);

        primaryStage.setScene(scene);

        primaryStage.show();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值