📜  JavaFX | AnchorPane 类

📅  最后修改于: 2022-05-13 01:55:09.730000             🧑  作者: Mango

JavaFX | AnchorPane 类

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

类的构造函数:

  1. AnchorPane() :创建一个新的 AnchorPane。
  2. AnchorPane(Node... c) : 创建一个具有指定节点的 AnchorPane。

常用方法:

MethodExplanation
getBottomAnchor(Node c)Returns the child’s bottom anchor.
getLeftAnchor(Node c)Returns the child’s left anchor.
getRightAnchor(Node c)Returns the child’s right anchor.
getTopAnchor(Node c)Returns the child’s top anchor.
setBottomAnchor(Node c, Double v)Sets the child’s bottom anchor.
setLeftAnchor(Node c, Double v)Sets the child’s left anchor.
setRightAnchor(Node c, Double v)Sets the child’s right anchor.
setTopAnchor(Node c, Double v)Sets the child’s top anchor.

下面的程序说明了 AnchorPane 类的使用:

  1. 创建 AnchorPane 并为其添加标签并向舞台添加标签的Java程序:在此程序中,我们将创建一个名为 anchor_pane 的AnchorPane 。将一个名为label的标签添加到 anchor_pane 并分别使用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);
        }
    }
    

    输出:

  2. 创建 AnchorPane 的Java程序,为其添加标签和按钮,并设置 AnchorPane 的最小高度和宽度,然后将其添加到舞台:在这个程序中,我们将创建一个名为 anchor_pane 的AnchorPane 。将一个名为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);
        }
    }
    

    输出:

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

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