首页 / 软件开发 / JAVA / Swing的实用工具:Swing Utilities
Swing的实用工具:Swing Utilities2010-05-21Swing提供了许多的在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)以给定的鼠标事件来判定按下了哪个鼠标按钮。