java anchorpane_JavaFX 类 AnchorPane用法及代码示例

AnchorPane类是JavaFX的一部分。 AnchorPane允许将子节点的边锚定到与锚定窗格的边偏移的位置。如果锚定窗格具有边框和/或填充设置,则偏移将从这些插图的内部边开始测量。 AnchorPane继承了Pane类。

该类的构造函数:

AnchorPane():创建一个新的AnchorPane。

AnchorPane(Node… c):创建具有指定节点的AnchorPane。

常用方法:

方法

说明

getBottomAnchor(Node c)

返回孩子的底部锚。

getLeftAnchor(Node c)

返回孩子的左锚。

getRightAnchor(Node c)

返回孩子的右锚。

getTopAnchor(Node c)

返回孩子的最高锚。

setBottomAnchor(Node c, Double v)

设置孩子的底部锚点。

setLeftAnchor(Node c, Double v)

设置孩子的左锚。

setRightAnchor(Node c, Double v)

设置孩子的右锚。

setTopAnchor(Node c, Double v)

设置孩子的最高锚。

以下示例程序旨在说明AnchorPane类的用法:

Java程序来创建AnchorPane并向其中添加标签,并向舞台添加标签:在此程序中,我们将创建一个名为anchor_pane的AnchorPane。向anchor_pane添加名为Label的Label,并分别使用setTopAnchor(),setBottomAnchor(),setLeftAnchor(),setRightAnchor()函数设置顶部,底部,左侧,右侧。现在将anchor_pane添加到场景中。然后最后将场景添加到舞台上,并调用show()函数以显示结果。

// Java Program to create a AnchorPane and

// add label to it and add label 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.AnchorPane;

import javafx.scene.shape.*;

public class AnchorPane_1 extends Application {

// launch the application

public void start(Stage stage)

{

try {

// set title for the stage

stage.setTitle("AnchorPane");

// create a label

Label label = new Label("this is AnchorPane example");

// create a AnchorPane

AnchorPane anchor_pane = new AnchorPane(label);

// anchor to the label

AnchorPane.setTopAnchor(label, 10.0);

AnchorPane.setLeftAnchor(label, 10.0);

AnchorPane.setRightAnchor(label, 10.0);

AnchorPane.setBottomAnchor(label, 10.0);

// create a scene

Scene scene = new Scene(anchor_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);

}

}

输出:

a9e0a6fb9244ce32197238ca87f973df.png

Java程序创建一个AnchorPane,向其添加标签和按钮,并设置AnchorPane的最小高度和最小宽度,然后将其添加到舞台上:在此程序中,我们将创建一个名为anchor_pane的AnchorPane。将名为label的Label添加到anchor_pane。还要添加一个名为Button的Button,并分别使用setTopAnchor(),setBottomAnchor(),setLeftAnchor(),setRightAnchor()函数设置顶部,底部,左侧,右侧锚点。使用setMinHeight()和setMinWidth()功能设置最小高度和宽度。将anchor_pane添加到场景中。最后,将场景添加到舞台上并调用show()函数以显示结果。

// Java Program to create a AnchorPane, adding

// label and button to it and also setting the

// min height and width of AnchorPane then 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.AnchorPane;

import javafx.scene.shape.*;

public class AnchorPane_2 extends Application {

// launch the application

public void start(Stage stage)

{

try {

// set title for the stage

stage.setTitle("AnchorPane");

// create a label

Label label = new Label("this is AnchorPane example");

// create a AnchorPane

AnchorPane anchor_pane = new AnchorPane(label);

// anchor to the label

AnchorPane.setTopAnchor(label, 120.0);

AnchorPane.setLeftAnchor(label, 10.0);

AnchorPane.setRightAnchor(label, 230.0);

AnchorPane.setBottomAnchor(label, 120.0);

Button button = new Button("button ");

// anchor to the button

AnchorPane.setTopAnchor(button, 125.0);

AnchorPane.setLeftAnchor(button, 220.0);

AnchorPane.setRightAnchor(button, 110.0);

AnchorPane.setBottomAnchor(button, 125.0);

anchor_pane.getChildren().add(button);

anchor_pane.setMinHeight(400);

anchor_pane.setMinWidth(400);

// create a scene

Scene scene = new Scene(anchor_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);

}

}

输出:

d373c338ff4468af4ca60c7e2d99d291.png

注意:以上程序可能无法在在线IDE中运行,请使用离线编译器。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值