程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Swing的實用工具:Swing Utilities

Swing的實用工具:Swing Utilities

編輯:關於JAVA

Swing提供了許多的在Swing組件內部使用的實用工具,SwingUtilities類就是其中一個。它提供了許多的涉及計算、轉換、訪問控制、布局等方面的方法,這些方法已廣泛的應用在各種Swing組件當中。當然,我們也可以把它拿來應用到我們自己的程序當中。

下面我們就來了解一個這些實用的功能.

(1)轉換方法

MouseEvent convertMouseEvent(Component source, MouseEvent ourceEvent, Component destination)

將一個鼠標事件從一個組件轉換到另一個組件上。

Point convertPoint(Component source, int x, int y, Component estination)

Point convertPoint(Component source, Point aPoint, Component destination)

將一個組件上的點坐標轉換成另一個組件上的坐標點。

void convertPointFromScreen(Point p, Component c)

將一個屏暮坐標點轉換成一個組件的坐標點.

void convertPointToScreen(Point p, omponent c)

將一個組件上的坐標點轉換成屏暮坐標。

Rectangle convertRectangle(Component source, Rectangle aRectangle, Component destination)

將一個組件上的矩形坐標轉換成另一個組件上的矩形坐標.

(2)計算方法

Rectangle[] computeDifference(Rectangle r1,Rectangle r2)

Rectangle computeIntersection(int x, int y, int width, int height, Rectangle dest)

Rectangle computeUnion(int x, int y, int width, int height, Rectangle dest)

int computeStringWidth(FontMetrics fm, String str)

boolean isRectangleContainingRectangle(Rectangle a, Rectangle b)

上面這些方法計算兩個矩形之間的差集、交集、並集,還可確定一個矩形是否包括了另一個矩形,以及計算一個字符串的像素寬度.

(3)鼠標按鈕信息

boolean isLeftMouseButton(MouseEvent anEvent)

boolean isMiddleMouseButton(MouseEvent anEvent)

boolean isRightMouseButton(MouseEvent anEvent)

以給定的鼠標事件來判定按下了哪個鼠標按鈕。

(4)訪問控制方法

Component findFocusOwner(Component c)

取得給定組件的焦點組件.

Component getLocalBounds(Component aComponent)

取得給定組件的范圍(Bounds)信息.

Component getRoot(Component c)

JRootPane getRootPane(Component c)

取得給定組件的根窗格.

Window windowForComponent(Component aComponent)

返回一個組件的視窗Window.

(5)組件繪制

String layoutCompoundLabel(FontMetrics fm, String text, Icon icon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, Rectangle viewR, Rectangle iconR, Rectangle textR, int textIconGap)

String layoutCompoundLabel(JComponent c, FontMetrics fm, String text, Icon icon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int rizontalTextPosition, Rectangle viewR, Rectangle iconR, Rectangle textR, int textIconGap)void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h)

這兩個方法用於重新布局swing按鈕和標簽上的文本和圖標.

void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h)

void paintComponent(Graphics g, Component c, Container p, Rectangle r)

在任一個圖形上繪制一個組件。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved