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


JavaFX 類 Dimension2D用法及代碼示例


Dimension2D類是JavaFX的一部分。 Dimension2D類是一個存儲高度和寬度的對象。它繼承了java.lang.Object類。

該類的構造函數:

  1. Dimension2D(double width, double height):創建具有指定寬度和高度的尺寸對象。

常用方法:


方法 說明
equals(Object obj) 返回兩個Dimension2D對象是否相等
getHeight() 返回Dimension2d對象的高度
getWidth() 返回Dimension2d對象的寬度
hashCode() 返回Dimension2D對象的哈希碼值。

下麵的程序將說明Dimension2D類的用法:

  1. Java程序,用於創建Dimension2D對象並顯示其內容:該程序創建一個名為Dimension的Dimension2D對象。尺寸的高度和寬度作為參數傳遞。然後使用getHeight()和getWidth()函數從對象中提取高度和寬度,並顯示相應的值。
    // Java program to create a Dimension2D  
    // object and display its contents 
    import javafx.geometry.*; 
      
    public class Dimension_1 { 
      
        // Main Method 
        public static void main(String args[]) 
        { 
      
            // create a dimension object 
            Dimension2D dimension = new Dimension2D(20.0f, 50.0f); 
      
            double height, width; 
      
            // get the height and width of the dimension2D 
            height = dimension.getHeight(); 
            width = dimension.getWidth(); 
      
            // display the height and width of dimension2D 
            System.out.println("Height = " + height + " Width = " + width); 
        } 
    }

    輸出:

    Height = 50.0 Width = 20.0
    
  2. 創建3個Dimension2D對象的Java程序將顯示其內容以及一個對象與另一個對象如何相等:此程序將創建三個名為Dimension_1,dimension_2,dimension_3的Dimension2D對象。尺寸的高度和寬度作為參數傳遞。然後使用getHeight()和getWidth()函數從對象中提取高度和寬度,並顯示相應的值。然後使用equals()函數比較這三個對象,並顯示結果。
    // Java program to create 3 Dimension2D objects display 
    // its contents and how whether one object is equal to  
    // the other or not 
    import javafx.geometry.*; 
      
    public class Dimension_2 { 
      
        // Main Method 
        public static void main(String args[]) 
        { 
      
            // create three dimension objects 
            Dimension2D dimension_1 = new Dimension2D(20.0f, 50.0f); 
            Dimension2D dimension_2 = new Dimension2D(120.0f, 150.0f); 
            Dimension2D dimension_3 = new Dimension2D(20.0f, 50.0f); 
      
            // display the coordinates of the 3 dimensions 
            display(dimension_1); 
            display(dimension_2); 
            display(dimension_3); 
      
            // check whether any dimension is equal to other or not 
            System.out.println("Dimension 1 equals Dimension 2 = " 
                               + dimension_1.equals(dimension_2)); 
      
            System.out.println("Dimension 2 equals Dimension 3 = " 
                                + dimension_2.equals(dimension_3)); 
      
            System.out.println("Dimension 3 equals Dimension 1 = " 
                                + dimension_3.equals(dimension_1)); 
        } 
      
        // Display Method 
        public static void display(Dimension2D dimension) 
        { 
      
            double height, width; 
      
            // get the cheight and width of the dimension 
            height = dimension.getHeight(); 
            width = dimension.getWidth(); 
      
            // display the height and width of dimension2D 
            System.out.println("Height = " + height + " Width = " + width); 
        } 
    }

    輸出:

    Height = 50.0 Width = 20.0
    Height = 150.0 Width = 120.0
    Height = 50.0 Width = 20.0
    Dimension 1 equals Dimension 2 = false
    Dimension 2 equals Dimension 3 = false
    Dimension 3 equals Dimension 1 = true
    

注意:以上程序可能無法在在線IDE中運行。請使用離線編譯器。

參考:https://docs.oracle.com/javase/8/javafx/api/javafx/geometry/Dimension2D.html



相關用法


注:本文由純淨天空篩選整理自andrew1234大神的英文原創作品 JavaFX | Dimension2D Class。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。