快速业务通道

Swing的实用工具:Swing Utilities - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-24

Swing的实用工具:Swing Utilities

时间:2010-05-21

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)

以给定的鼠标事件来判定按下了哪个鼠标按钮。

Swing的实用工具:Swing Utilities(2)

时间:2010-05-21

(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)

在任一个图形上绘制一个组件。

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号