新聞中心
Java的秘密:Swing的實(shí)用工具

成都創(chuàng)新互聯(lián)成立與2013年,先為南澗等服務(wù)建站,南澗等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為南澗企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
Swing提供了許多的在Swing組件內(nèi)部使用的實(shí)用工具,SwingUtilities類就是其中一個(gè)。它提供了許多的涉及計(jì)算、轉(zhuǎn)換、訪問控制、布局等方面的方法,這些方法已廣泛的應(yīng)用在各種Swing組件當(dāng)中。當(dāng)然,我們也可以把它拿來應(yīng)用到我們自己的程序當(dāng)中。
下面我們就來了解一個(gè)這些實(shí)用的功能.
(1)轉(zhuǎn)換方法
◆MouseEvent convertMouseEvent(Component source, MouseEvent ourceEvent, Component destination)
將一個(gè)鼠標(biāo)事件從一個(gè)組件轉(zhuǎn)換到另一個(gè)組件上。
◆Point convertPoint(Component source, int x, int y, Component estination)
◆Point convertPoint(Component source, Point aPoint, Component destination)
將一個(gè)組件上的點(diǎn)坐標(biāo)轉(zhuǎn)換成另一個(gè)組件上的坐標(biāo)點(diǎn)。
◆void convertPointFromScreen(Point p, Component c)
將一個(gè)屏暮坐標(biāo)點(diǎn)轉(zhuǎn)換成一個(gè)組件的坐標(biāo)點(diǎn).
◆void convertPointToScreen(Point p, omponent c)
將一個(gè)組件上的坐標(biāo)點(diǎn)轉(zhuǎn)換成屏暮坐標(biāo)。
◆Rectangle convertRectangle(Component source, Rectangle aRectangle, Component destination)
將一個(gè)組件上的矩形坐標(biāo)轉(zhuǎn)換成另一個(gè)組件上的矩形坐標(biāo).
(2)計(jì)算方法
- 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)
上面這些方法計(jì)算兩個(gè)矩形之間的差集、交集、并集,還可確定一個(gè)矩形是否包括了另一個(gè)矩形,以及計(jì)算一個(gè)字符串的像素寬度.
(3)鼠標(biāo)按鈕信息
◆boolean isLeftMouseButton(MouseEvent anEvent)
◆boolean isMiddleMouseButton(MouseEvent anEvent)
◆boolean isRightMouseButton(MouseEvent anEvent)
以給定的鼠標(biāo)事件來判定按下了哪個(gè)鼠標(biāo)按鈕。
(4)訪問控制方法
◆Component findFocusOwner(Component c)
取得給定組件的焦點(diǎn)組件.
◆Component getLocalBounds(Component aComponent)
取得給定組件的范圍(Bounds)信息.
◆Component getRoot(Component c)
◆JRootPane getRootPane(Component c)
取得給定組件的根窗格.
◆Window windowForComponent(Component aComponent)
返回一個(gè)組件的視窗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)
這兩個(gè)方法用于重新布局swing按鈕和標(biāo)簽上的文本和圖標(biāo).
◆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)
在任一個(gè)圖形上繪制一個(gè)組件。
【編輯推薦】
- 淺析運(yùn)行Swing應(yīng)用程序
- 了解AWT和Swing組件
- Swing組件相關(guān)的操作
- 詳解MVC設(shè)計(jì)模式與Swing
- 淺談Swing和AWT構(gòu)件
當(dāng)前文章:實(shí)用工具SwingUtilities
文章源于:http://m.5511xx.com/article/cdgspoe.html


咨詢
建站咨詢
