JavaFX | AnchorPane类

AnchorPane类是JavaFX的一部分。AnchorPane允许将子节点的边缘定位到与定位窗格边缘的偏移处。如果锚定窗格设置了边框和/或填充,则将从这些插图的内侧边缘测量偏移量。锚烷遗传 窗玻璃

null

类的构造函数:

  1. 锚烷() :创建一个新的锚烷。
  2. 锚烷(节点…c) :创建具有指定节点的锚链。

常用方法:

方法 解释
getBottomAnchor(节点c) 返回子对象的底部锚点。
getLeftAnchor(节点c) 返回孩子的左锚。
getRightAnchor(节点c) 返回孩子的右锚点。
getTopAnchor(节点c) 返回子对象的顶部锚点。
立根顶点(节点c,双v) 设置孩子的下锚。
setLeftAnchor(节点c,双v) 设置孩子的左锚。
setRightAnchor(节点c,双v) 设置孩子的右锚。
setTopAnchor(节点c,双v) 设置孩子的上锚。

以下程序说明了锚烷类的使用:

  1. Java程序,用于创建一个主播台并向其添加标签,以及向舞台添加标签: 在这个程序中,我们将创建一个名为 锚板 .添加一个名为 标签 到“锚定”窗格,并使用 setTopAnchor() , setBottomAnchor() , setLeftAnchor() , setRightAnchor() 分别起作用。现在将“锚定”窗格添加到场景中。最后将场景添加到舞台上,并调用 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);
    }
    }

    
    

    输出:

    图片[1]-JavaFX | AnchorPane类-yiteyi-C++库

  2. Java程序创建一个锚网,向其添加标签和按钮,并设置锚网的最小高度和宽度,然后将其添加到舞台上: 在这个程序中,我们将创建一个名为 锚板 .添加一个名为 标签 锚板 。同时添加一个名为 按钮 并使用 setTopAnchor() , setBottomAnchor() , setLeftAnchor() , setRightAnchor() 分别起作用。使用 setMinHeight() setMinWidth() 作用添加 锚板 去现场。最后,将场景添加到舞台,并调用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);
    }
    }

    
    

    输出:

    图片[2]-JavaFX | AnchorPane类-yiteyi-C++库

注: 以上程序可能无法在联机IDE中运行,请使用脱机编译器。

参考: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/AnchorPane.html

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