Java GUI之位置控制與尺寸控制

鍾超發表於2011-11-20

以下四個類都包含在java.awt包內。


1. Toolkit類:

Toolkit類是一個包含了本機系統屬性和引數的抽象類,比如Clipboard內容、游標、桌面屬性、字型族、顏色型別、螢幕引數和系統事件。

2. Dimension類:

Dimension類通常用來獲取或設定元件的尺寸。與Toolkit類配合使用,則可以獲取螢幕尺寸。

3. GraphicsEnvironment類:

GraphicsEnvironment類是一個包含本級系統影像環境的類。

4. Rectangle類:

Rectangle類是矩形類。


package com.sinosuperman.driver;

import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Toolkit;

public class MainBench {
	public static void main(String[] args) {
		// Get a Toolkit object containing system properties and parameters.
		Toolkit tk = Toolkit.getDefaultToolkit();
		// Get a Dimension object containing screen size.
		Dimension d = tk.getScreenSize();
		// Get the screen width.
		System.out.println(d.getWidth());
		// Get the screen height.
		System.out.println(d.getHeight());
		
		// Get a GraphicsEnvironment object containing system graphics environment.
		GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
		// Get a Rectangle object containing the maximum window bounds of current window.
		Rectangle rec = environment.getMaximumWindowBounds();
		// Get the x-coordinate of the center point of rec.
		System.out.println(rec.getCenterX());
		// Get the y-coordinate of the center point of rec.
		System.out.println(rec.getCenterY());
		System.out.println(rec);
		
		// Get the center point of system graphics environment.
		Point point = environment.getCenterPoint();
		System.out.println(point);
		
		// Get the location of rec.
		point = rec.getLocation();
		System.out.println(point);
	}
}


相關文章