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


Java AWT Dimension用法及代碼示例


Dimension 類是 Java AWT 的一部分。它包含整數和雙精度的組件的高度和寬度。 Dimension 類的用途是 Java AWT 和 Swing 的許多函數都返回維度對象。

Dimension 類的構造函數

  1. Dimension():它將創建一個高度和寬度設置為零的新對象。
  2. 尺寸(尺寸d):它將創建一個與指定對象具有相同高度和寬度的新對象。
  3. 尺寸(int w,int h):它將創建一個具有指定高度和寬度的新對象。

Methods of Dimension Class

方法 解釋
equals(Object o) 檢查二維對象是否相等。
getHeight() 返回尺寸對象的高度
getWidth() 返回尺寸對象的寬度
getSize() 返回維度對象的大小。
hashCode() 返回維度的哈希碼。
setSize(Dimension d) 將對象的大小設置為指定尺寸
setSize(double width, double height) 將高度和寬度設置為指定的雙精度值
setSize(int width, int height) 將高度和寬度設置為指定的整數值

以下示例程序旨在說明 Dimension 類:

  • 程序顯示尺寸類(整數精度)的函數。

Java


// Java Program to show the functions
// of dimension class(Integer precision)
import java.awt.*;
class dimen {
    // Main Method
    public static void main(String args[])
    {
        // create dimension
        Dimension d = new Dimension();
        Dimension d1 = new Dimension(20, 30);
        Dimension d2 = new Dimension(d1);
        // set height and width of dimension d
        d.setSize(30, 30);
        // equating dimensions
        System.out.println("Dimension d and d1 " + 
                    "are equal = " + d.equals(d1));
                     
        System.out.println("Dimension d and d1 " + 
                "are equal = " + d1.equals(d2));
        // print hashcode
        System.out.println("Hashcode of Dimension " + 
                            "d = " + d.hashCode());
        // display dimension
        display(d, "Dimension d");
        display(d1, "Dimension d1");
        display(d2, "Dimension d2");
    }
    // display dimension
    public static void display(Dimension d, String s)
    {
        System.out.println(s +" Height = " + d.getHeight() + 
                                " Width= " + d.getWidth());
    }
}
輸出
Dimension d and d1 are equal = false
Dimension d and d1 are equal = true
Hashcode of Dimension d = 1860
Dimension d Height = 30.0 Width= 30.0
Dimension d1 Height = 30.0 Width= 20.0
Dimension d2 Height = 30.0 Width= 20.0
輸出:
Dimension d and d1 are equal = false
Dimension d and d1 are equal = true
Hashcode of Dimension d = 1860
Dimension d Height = 30.0 Width= 30.0
Dimension d1 Height = 30.0 Width= 20.0
Dimension d2 Height = 30.0 Width= 20.0
  • 顯示尺寸類(雙精度)函數的程序。

Java


// Java Program to show the functions
// of dimension class(Double precision)
import java.awt.*;
class dimen {
     
    // Main Method
    public static void main(String args[])
    {
         
        // create dimension
        Dimension d = new Dimension();
        Dimension d1 = new Dimension(20, 30);
        Dimension d2 = new Dimension(d1);
        // set height and width of dimension d
        d.setSize(30.3, 30.45);
        // equating dimensions
        System.out.println("Dimension d and d1" + 
                "are equal = " + d.equals(d1));
                     
        System.out.println("Dimension d and d1" + 
                "are equal = " + d1.equals(d2));
        // print hashcode
        System.out.println("Hashcode of Dimension d = "
                                    + d.hashCode());
        // display dimension
        System.out.println("See the values are rounded" + 
                        "off to ceiling in dimension d");
        display(d, "Dimension d");
        display(d1, "Dimension d1");
        display(d2, "Dimension d2");
    }
    // display dimension
    public static void display(Dimension d, String s)
    {
        System.out.println(s + " Height = " + d.getHeight() 
                            + " Width = " + d.getWidth());
    }
}
輸出
Dimension d and d1are equal = false
Dimension d and d1are equal = true
Hashcode of Dimension d = 1984
See the values are roundedoff to ceiling in dimension d
Dimension d Height = 31.0 Width = 31.0
Dimension d1 Height = 30.0 Width = 20.0
Dimension d2 Height = 30.0 Width = 20.0
輸出:
Dimension d and d1are equal = false
Dimension d and d1are equal = true
Hashcode of Dimension d = 1984
See the values are roundedoff to ceiling in dimension d
Dimension d Height = 31.0 Width = 31.0
Dimension d1 Height = 30.0 Width = 20.0
Dimension d2 Height = 30.0 Width = 20.0

參考:https://docs.oracle.com/javase/7/docs/api/java/awt/Dimension.html



相關用法


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