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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。