当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


JavaFX 类 Insets用法及代码示例


Insets 类是 JavaFX 的一部分。 Insets 类存储矩形区域四个边的内部偏移量。 Insets 类继承 java.lang.Object 类。

类的构造函数:

  1. Insets(double a):为所有四个偏移量构造一个具有相同值的新 Insets 实例。
  2. Insets(double top, double right, double bottom, double left):构造一个具有四个不同偏移量的新 Insets 实例。

常用方法:

方法解释
equals(java.lang.Object obj)指示其他某个对象是否是 “equal to” 这个。
getBottom()返回底部的插图
getLeft()返回左侧的插图
getRight()返回右侧的插图
getTop()返回顶部的插图
hashCode()返回类的哈希码

下面的程序将说明 Insets 类的使用:

  1. Java 程序创建两个 insets 对象并显示内容:该程序创建了两个名为 insets_1 和 insets_2 的插图。调用构造函数时,插入作为参数传递。我们将使用 getTop()、getBottom()、getLeft()、getRight() 函数获取 insets 对象的四个边并显示它们。
    
    // Java program to create two insets 
    // object and display the contents
    import javafx.geometry.Insets;
      
    public class Insets_1 {
      
        // Main Method
        public static void main(String args[])
        {
              
            // create two insets object
            Insets insets_1 = new Insets(20.0f);
            Insets insets_2 = new Insets(20.0f, 40.0f, 60.0f, 70.0f);
      
            // display the insets
            display(insets_1);
            display(insets_2);
        }
          
        // display method
        public static void display(Insets insets)
        {
            double left, right, bottom, top;
      
            // get the insets
            left = insets.getLeft();
            right = insets.getRight();
            bottom = insets.getBottom();
            top = insets.getTop();
      
            // display the insets
            System.out.println("Insets of the object");
            System.out.println("Left= " + left + ", Right = " 
                               + right + ", Bottom = " + bottom 
                               + ", Top = " + top);
        }
    }

    输出:



    Insets of the object
    Left = 20.0, Right = 20.0, Bottom = 20.0, Top = 20.0
    Insets of the object
    Left = 70.0, Right = 40.0, Bottom = 60.0, Top = 20.0
    
  2. Java 程序创建三个 insets 对象并显示其内容并检查它们是否彼此相等:该程序创建了三个名为 insets_1、insets_2 和 insets_3 的插图。调用构造函数时,插入作为参数传递。我们将使用 getTop()、getBottom()、getLeft()、getRight() 函数获取 insets 对象的四个边并显示它们。我们还将使用 equals 函数检查插入是否彼此相等。
    
    // Java program to create three objects of insets
    // and display its contents and check whether 
    // they are equal to each other or not
    import javafx.geometry.Insets;
      
    public class Insets_2 {
      
        // Main Method
        public static void main(String args[])
        {
              
            // create three insets objects
            Insets insets_1 = new Insets(120.0f, 150.0f,
                                          40.0f, 60.0f);
                                            
            Insets insets_2 = new Insets(120.0f, 150.0f, 
                                           40.0f, 60.0f);
                                             
            Insets insets_3 = new Insets(200.0f, 120.0f, 
                                          60.0f, 40.0f);
      
            // display the 3 insets
            display(insets_1);
            display(insets_2);
            display(insets_3);
      
            // check whether any insets is equal to other or not
            System.out.println("Insets 1 equals Insets 2 = " 
                               + insets_1.equals(insets_2));
                                 
            System.out.println("Insets 2 equals Insets 3 = " 
                                + insets_2.equals(insets_3));
                                  
            System.out.println("Insets 3 equals Insets 1 = " 
                                + insets_3.equals(insets_1));
        }
      
        // display Method
        public static void display(Insets insets)
        {
            double left, right, bottom, top;
      
            // get the insets
            left = insets.getLeft();
            right = insets.getRight();
            bottom = insets.getBottom();
            top = insets.getTop();
      
            // display the insets
            System.out.println("Insets of the object");
            System.out.println("Left= " + left + ", Right= "
                               + right + ", Bottom= " + bottom 
                               + ", Top = " + top);
        }
    }

    输出:

    Insets of the object
    Left= 60.0, Right= 150.0, Bottom= 40.0, Top = 120.0
    Insets of the object
    Left= 60.0, Right= 150.0, Bottom= 40.0, Top = 120.0
    Insets of the object
    Left= 40.0, Right= 120.0, Bottom= 60.0, Top = 200.0
    Insets 1 equals Insets 2 = true
    Insets 2 equals Insets 3 = false
    Insets 3 equals Insets 1 = false
    

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

参考:https://docs.oracle.com/javafx/2/api/javafx/geometry/Insets.html




相关用法


注:本文由纯净天空筛选整理自andrew1234大神的英文原创作品 JavaFX | Insets Class。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。