當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。