实用工具SwingUtilities
创始人
2024-04-14 10:20:48
0

Java的秘密:Swing的实用工具

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)计算方法

  1. Rectangle[] computeDifference(Rectangle r1,Rectangle r2)  
  2. Rectangle computeIntersection(int x, int y, int width, int height, Rectangle dest)  
  3. Rectangle computeUnion(int x, int y, int width, int height, Rectangle dest)  
  4. int computeStringWidth(FontMetrics fm, String str)  
  5. 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)组件绘制

  1. String layoutCompoundLabel(FontMetrics fm, String text, Icon icon, 
    int verticalAlignment, int horizontalAlignment, int verticalTextPosition, 
    int horizontalTextPosition, Rectangle viewR, Rectangle iconR, Rectangle textR, 
    int textIconGap)  
  2. 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. 浅析运行Swing应用程序
  2. 了解AWT和Swing组件
  3. Swing组件相关的操作
  4. 详解MVC设计模式与Swing
  5. 浅谈Swing和AWT构件

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...