Quick Contact

    Hibernate

    JavaFXAnchorPane

    The JavaFX Anchor Pane is used for anchoring to an offset from an anchor pane edges by its child node edges. If the anchor pane has a border or padding set, the off sets is measuring from the inside corner edges of the insets. Anchor pane lay out managing child of the child’s makes visible-property value and non-managing child are not considered for calculating layouts. We have facility to add CSS styles with Anchor pane of JavaFX. Anchor Pane is a JavaFX class, which is available in scene.layout.AnchorPane package. We can apply JavaFX Anchor pane with label, cylinder structure, rectangle structure, buttons etc.

    Constructors

    • AnchorPane():

      Creates an anchor pane object with new keyword without any argument in the constructor.

    • AnchorPane(Node n1, Node n2, Node n3,….):

      Creates an anchor pane object with new keyword with required number of arguments in the constructor.

    Frequently Used Methods

    Below are the used methods:

    • setBottomAnchor(Node b, Double value):

      setBottomAnchor() method is used to set Bottom anchor.

    • getBottomAnchor(Node b):

      getBottomAnchor() method returns bottom anchor.

    • setLeftAnchor(Node l, Double value):

      setLeftAnchor() method is used to set left anchor.

    • getLeftAnchor(Node l):

      getLeftAnchor() method returns left anchor.

    • setRightAnchor(Node r, Double value):

      setRightAnchor() method is used to set right anchor.

    • getRightAnchor(Node r):

      getRightAnchor() method returns right anchor.

    • setTopAnchor(Node t, Double value):

      setRightAnchor() method is used to set top anchor.

    • getTopAnchor(Node t):

      getTopAnchor() method returns top anchor.

    How does AnchorPane work in JavaFX?

    Accessing JavaFX features user defined class must extends Application class.

    1. In JavaFX creating AnchorPane is first step. AnchorPane can instantiate by using new keyword.
    2. Syntax:

      AnchorPaneanchorPaneRef=new AnchorPane();

    3. Creating Label or Button or any JavaFX element is second step.
    4. Syntax:

      Label lableRef=new Label(“Any Message”);

      anchorPaneRef.setTopAnchor(labelRef, value, value);

    5. Create VBox or any other display(like TilePane or HBox as per requirement) class to add the items is third step.
    6. Syntax:

      VBoxvBox=new VBox(anchorPaneRef); //Gives vertical box

    7. Fourth for apply show method on to it.
    8. Syntax:

      Step is creating scene.

      Scene screen = new Scene(vBox, length, width);

    9. Adding Scene reference screen to the Stage object reference is fifth step. Adding output screen to Stage. We will get this stage object reference from start predefined JavaFX method.
    10. Syntax:

      stage.setScene(screen);

    11. Sixth step is showing output to the end user by applying show() method on the scene object.
    12. Syntax:

      stage.show();

    Examples of JavaFXAnchorPane

    Given below are the examples:

    Example #1

    Anchor pane with Label.

    Code:

    package com.anchorpanae;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Label;
    //importing AnchorPane class
    import javafx.scene.layout.AnchorPane;
    import javafx.stage.Stage;
    public class AnchorPaneLabel extends Application {
    // launch method invoked this start method
    public void start(Stage outStage) {
    try {
    // setting the tile for application
    outStage.setTitle("AnchorPane Label");
    // create the label for adding the text to the application
    Label lableAnchor = new Label("I am anchor pane lable to show the demo.");
    // creating a AnchorPane object
    AnchorPaneanchorPaneObject = new AnchorPane(lableAnchor);
    //set the label to top anchor
    AnchorPane.setTopAnchor(lableAnchor, 20.0);
    //set the label to left anchor
    AnchorPane.setLeftAnchor(lableAnchor, 100.0);
    //set the label to right anchor
    AnchorPane.setRightAnchor(lableAnchor, 20.0);
    //set the label to bottom anchor
    AnchorPane.setBottomAnchor(lableAnchor, 20.0);
    // creating the scene to display output
    Scene screen = new Scene(anchorPaneObject, 410, 310);
    outStage.setScene(screen);
    // display the output
    outStage.show();
    }
    catch (Exception e) {
    System.out.println(e.getMessage());
    }
    }
    // Main Method
    public static void main(String args[]) {
    // invoking main method from JVM
    launch(args);
    }
    }
    

    Example #2

    Anchor pane with Buttons.

    Code:

    package com.anchorpanae;
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.ScrollPane;
    //importing AnchorPane class
    import javafx.scene.layout.AnchorPane;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;
    public class AnchorPaneButton extends Application {
    // launch method invoked this start method
    public void start(Stage outStage) {
    try {
    // setting the tile for application
    outStage.setTitle("AnchorPane Buttons");
    // creating the buttons for adding the text to the application
    Button button1 = new Button("First Button");
    Button button2 = new Button("Second Button");
    // creating a AnchorPane object
    AnchorPane anchorPaneObject1 = new AnchorPane(button1);
    //set the label to top anchor
    anchorPaneObject1.setTopAnchor(button1, 125.0);
    //set the label to left anchor
    anchorPaneObject1.setLeftAnchor(button1, 200.0);
    //set the label to right anchor
    anchorPaneObject1.setRightAnchor(button1, 100.0);
    //set the label to bottom anchor
    anchorPaneObject1.setBottomAnchor(button1, 500.0);
    //anchorPaneObject.getChildren().add(button1);
    anchorPaneObject1.setMinHeight(500);
    anchorPaneObject1.setMinWidth(500);
    // creating a AnchorPane object
    AnchorPane anchorPaneObject2 = new AnchorPane(button2);
    //set the label to top anchor
    anchorPaneObject2.setTopAnchor(button2, 150.0);
    //set the label to left anchor
    anchorPaneObject2.setLeftAnchor(button2, 200.0);
    //set the label to right anchor
    anchorPaneObject2.setRightAnchor(button2, 100.0);
    //set the label to bottom anchor
    anchorPaneObject2.setBottomAnchor(button2, 500.0);
    //anchorPaneObject.getChildren().add(button1);
    anchorPaneObject2.setMinHeight(500);
    anchorPaneObject2.setMinWidth(500)
    //creating VBox for adding anchor panes
    VBoxvBox=new VBox();
    vBox.getChildren().add(anchorPaneObject1);
    vBox.getChildren().add(anchorPaneObject2);
    //scrollpane for scrolling
    ScrollPanescrollPnae=new ScrollPane();
    scrollPnae.setContent(vBox);
    // creating the scene to display output
    Scene screen = new Scene(scrollPnae, 400, 300);
    outStage.setScene(screen);
    // display the output
    outStage.show();
    }
    catch (Exception e) {
    System.out.println(e.getMessage());
    }
    }
    // Main Method
    public static void main(String args[]) {
    // invoking main method from JVM
    launch(args);
    }
    }
    

    Example #3

    Anchor pane with Rectangle shape.

    Code:

    package com.anchorpanae;
    import javafx.application.Application;
    import javafx.scene.Scene;
    //importing AnchorPane class
    import javafx.scene.layout.AnchorPane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Rectangle;
    import javafx.stage.Stage;
    public class AnchorPaneImage extends Application {
    // launch method invoked this start method
    public void start(Stage outStage) {
    try {
    // setting the tile for application
    outStage.setTitle("AnchorPane Rectangle Shape");
    //creating rectangle shape
    Rectangle rectangleShape = new Rectangle(300,200,Color.GREEN);
    rectangleShape.relocate(70,70);
    // creating a AnchorPane object
    AnchorPaneanchorPaneObject = new AnchorPane(rectangleShape);
    //set the label to top anchor
    AnchorPane.setTopAnchor(rectangleShape, 20.0);
    //set the label to left anchor
    AnchorPane.setLeftAnchor(rectangleShape, 100.0);
    //set the label to right anchor
    AnchorPane.setRightAnchor(rectangleShape, 20.0);
    //set the label to bottom anchor
    AnchorPane.setBottomAnchor(rectangleShape, 20.0);
    // creating the scene to display output
    Scene screen = new Scene(anchorPaneObject, 500, 400);
    outStage.setScene(screen);
    // display the output
    outStage.show();
    }
    catch (Exception e) {
    System.out.println(e.getMessage());
    }
    }
    // Main Method
    public static void main(String args[]) {
    // invoking main method from JVM
    launch(args);
    }
    }
    

    Apply now for Advanced Java Training Course

    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.