模块  java.desktop
软件包  javax.swing

Class SwingUtilities

  • 实现的所有接口
    SwingConstants

    public class SwingUtilities
    extends Object
    implements SwingConstants
    Swing实用程序方法的集合。
    从以下版本开始:
    1.2
    • 方法详细信息

      • isRectangleContainingRectangle

        public static final boolean isRectangleContainingRectangle​(Rectangle a,
                                                                   Rectangle b)
        返回 true如果@ {代码}包含 b
        参数
        a - 第一个矩形
        b - 第二个矩形
        结果
        true如果@ {code a}包含 b
      • getLocalBounds

        public static Rectangle getLocalBounds​(Component aComponent)
        返回组件 aComponent的矩形(0,0,bounds.width,bounds.height)
        参数
        aComponent - 一个组件
        结果
        组件 aComponent的本地边界
      • getWindowAncestor

        public static Window getWindowAncestor​(Component c)
        返回第 Window祖先 c ,或 null如果 c未包含在里面 Window
        参数
        c - Component获得 Window祖先。
        结果
        第一个 Window祖先 c ,或 null如果 c未包含在 Window
        从以下版本开始:
        1.3
      • convertPoint

        public static Point convertPoint​(Component source,
                                         Point aPoint,
                                         Component destination)
        转换aPointsource坐标系destination坐标系。 如果sourcenull ,则假定aPointdestination的根组件坐标系中。 如果destinationnull ,则aPoint将转换为source的根组件坐标系。 如果sourcedestination均为null ,则返回aPoint而不进行任何转换。
        参数
        source - 源组件
        aPoint - 重点
        destination - 目标组件
        结果
        转换后的坐标
      • convertPoint

        public static Point convertPoint​(Component source,
                                         int x,
                                         int y,
                                         Component destination)
        source坐标系中的点(x,y)转换为destination坐标系。 如果sourcenull ,则假定(x,y)位于destination的根组件坐标系中。 如果destinationnull ,则(x,y)将转换为source的根组件坐标系。 如果sourcedestination均为null ,则返回(x,y)而不进行任何转换。
        参数
        source - 源组件
        x - 该点的x坐标
        y - 该点的y坐标
        destination - 目标组件
        结果
        转换后的坐标
      • convertRectangle

        public static Rectangle convertRectangle​(Component source,
                                                 Rectangle aRectangle,
                                                 Component destination)
        source坐标系中的矩形aRectangle转换为destination坐标系。 如果sourcenull ,则假定aRectangledestination的根组件坐标系中。 如果destinationnullaRectangle将被转换为source的根组件坐标系统。 如果sourcedestination均为null ,则返回aRectangle而不进行任何转换。
        参数
        source - 源组件
        aRectangle - 一个矩形
        destination - 目标组件
        结果
        转换后的矩形
      • getAncestorOfClass

        public static Container getAncestorOfClass​(<?> c,
                                                   Component comp)
        在组件层次结构中搜索上述comp便捷方法,并返回它找到的类c的第一个对象。 可以返回null ,如果找不到类c
        参数
        c - 组件的类
        comp - 组件
        结果
        comp的祖先,或 null如果 c
      • getAncestorNamed

        public static Container getAncestorNamed​(String name,
                                                 Component comp)
        在组件层次结构中搜索上述comp便捷方法,并返回它找到的第一个对象name 可以返回null ,如果name
        参数
        name - 组件的名称
        comp - 组件
        结果
        comp的祖先,或 null如果 name
      • getDeepestComponentAt

        public static Component getDeepestComponentAt​(Component parent,
                                                      int x,
                                                      int y)
        返回的最深可见后代组件parent包含位置xy 如果parent不包含指定的位置,则返回null 如果parent不是容器,或者parent的可见后代都不包含指定位置,则返回parent
        参数
        parent - 开始搜索的根组件
        x - x目标位置
        y - y目标位置
        结果
        最深的部分
      • convertMouseEvent

        public static MouseEvent convertMouseEvent​(Component source,
                                                   MouseEvent sourceEvent,
                                                   Component destination)
        返回类似于sourceEvent的MouseEvent,除了它的x和y成员已转换为destination的坐标系。 如果sourcenullsourceEvent x和y的成员被认为是成destination的根组件坐标系统。 如果destinationnull ,则返回的MouseEvent将位于source的坐标系中。 sourceEvent不会被更改。 返回一个新事件。 如果destination为非null ,则返回事件的source字段将设置为destination使用translateMouseEvent()方法将鼠标事件从一个组件转换为另一个组件而不更改源。
        参数
        source - 源组件
        sourceEvent - 源鼠标事件
        destination - 目标组件
        结果
        新的鼠标事件
      • convertPointToScreen

        public static void convertPointToScreen​(Point p,
                                                Component c)
        将点从组件的坐标系转换为屏幕坐标。
        参数
        p - Point对象(转换为新坐标系)
        c - 一个Component对象
      • convertPointFromScreen

        public static void convertPointFromScreen​(Point p,
                                                  Component c)
        将点从屏幕坐标转换为组件的坐标系
        参数
        p - 一个Point对象(转换为新的坐标系)
        c - 组件对象
      • windowForComponent

        public static Window windowForComponent​(Component c)
        返回第Window祖先c ,或null如果c未包含在里面Window

        注意:此方法提供与getWindowAncestor相同的功能。

        参数
        c - Component获得 Window祖先。
        结果
        第一个 Window祖先是 c ,或者 null如果 c不包含在 Window
      • isDescendingFrom

        public static boolean isDescendingFrom​(Component a,
                                               Component b)
        返回 true如果组件 a从组件下降 b
        参数
        a - 第一个组件
        b - 第二个组件
        结果
        true如果组件 a来自组件 b
      • computeIntersection

        public static Rectangle computeIntersection​(int x,
                                                    int y,
                                                    int width,
                                                    int height,
                                                    Rectangle dest)
        便于计算两个矩形的交集,而无需分配新的矩形。 如果两个矩形不相交,则返回的矩形从(0,0)开始,宽度和高度为零。
        参数
        x - 第一个矩形 x的X坐标
        y - 第一个矩形 y的Y坐标
        width - 第一个矩形的宽度
        height - 第一个矩形的高度
        dest - 第二个矩形
        结果
        dest ,修改为指定交集
      • computeUnion

        public static Rectangle computeUnion​(int x,
                                             int y,
                                             int width,
                                             int height,
                                             Rectangle dest)
        在不分配新矩形的情况下计算两个矩形的并集的便捷方法。
        参数
        x - 第一个矩形的x坐标
        y - 第一个矩形的y坐标
        width - 第一个矩形的宽度
        height - 第一个矩形的高度
        dest - 第二个矩形的坐标; 在这个矩形中返回两个矩形的并集
        结果
        dest Rectangle
      • computeDifference

        public static Rectangle[] computeDifference​(Rectangle rectA,
                                                    Rectangle rectB)
        方便返回表示rectA内不与rectB重叠的区域的rect数组。 如果两个Rects不重叠,则返回一个空数组
        参数
        rectA - 第一个矩形
        rectB - 第二个矩形
        结果
        表示 rectA内不与 rectB重叠的区域的矩形数组。
      • isLeftMouseButton

        public static boolean isLeftMouseButton​(MouseEvent anEvent)
        如果鼠标事件指定鼠标左键,则返回true。
        参数
        anEvent - MouseEvent对象
        结果
        如果鼠标左键处于活动状态,则为true
      • isMiddleMouseButton

        public static boolean isMiddleMouseButton​(MouseEvent anEvent)
        如果鼠标事件指定鼠标中键,则返回true。
        参数
        anEvent - MouseEvent对象
        结果
        如果鼠标中键处于活动状态,则为true
      • isRightMouseButton

        public static boolean isRightMouseButton​(MouseEvent anEvent)
        如果鼠标事件指定鼠标右键,则返回true。
        参数
        anEvent - MouseEvent对象
        结果
        如果鼠标右键处于活动状态,则为true
      • computeStringWidth

        public static int computeStringWidth​(FontMetrics fm,
                                             String str)
        使用具有指定“metrics”(大小)的字体计算字符串的宽度。
        参数
        fm - 要计算的FontMetrics对象
        str - 要计算的字符串
        结果
        包含字符串宽度的int
      • layoutCompoundLabel

        public static String layoutCompoundLabel​(JComponent c,
                                                 FontMetrics fm,
                                                 String text,
                                                 Icon icon,
                                                 int verticalAlignment,
                                                 int horizontalAlignment,
                                                 int verticalTextPosition,
                                                 int horizontalTextPosition,
                                                 Rectangle viewR,
                                                 Rectangle iconR,
                                                 Rectangle textR,
                                                 int textIconGap)
        计算并返回图标原点的位置,文本基线的原点位置以及复合标签字符串的可能剪裁版本。 相对于viewR矩形计算位置。 JComponents方向(LEADING / TRAILING)也将被考虑在内并相应地转换为LEFT / RIGHT值。
        参数
        c - 组件
        fm -实例 FontMetrics
        text - 原文
        icon - 图标
        verticalAlignment - 垂直对齐
        horizontalAlignment - 水平对齐
        verticalTextPosition - 垂直文本位置
        horizontalTextPosition - 水平文本位置
        viewR - 可用的矩形
        iconR - 图标的矩形
        textR - 文本的矩形
        textIconGap - 文本和图标之间的差距
        结果
        复合标签字符串的可能剪裁版本
      • layoutCompoundLabel

        public static String layoutCompoundLabel​(FontMetrics fm,
                                                 String text,
                                                 Icon icon,
                                                 int verticalAlignment,
                                                 int horizontalAlignment,
                                                 int verticalTextPosition,
                                                 int horizontalTextPosition,
                                                 Rectangle viewR,
                                                 Rectangle iconR,
                                                 Rectangle textR,
                                                 int textIconGap)
        计算并返回图标原点的位置,文本基线的原点位置以及复合标签字符串的可能剪裁版本。 相对于viewR矩形计算位置。 此layoutCompoundLabel()不知道如何处理horizontalTextPosition中的LEADING / TRAILING值(它们将默认为RIGHT)和horizontalAlignment(它们将默认为CENTER)。 请改用另一个版本的layoutCompoundLabel()。
        参数
        fm -实例 FontMetrics
        text - 文字
        icon - 图标
        verticalAlignment - 垂直对齐
        horizontalAlignment - 水平对齐
        verticalTextPosition - 垂直文本位置
        horizontalTextPosition - 水平文本位置
        viewR - 可用的矩形
        iconR - 图标的矩形
        textR - 文本的矩形
        textIconGap - 文本和图标之间的差距
        结果
        复合标签字符串的可能剪裁版本
      • paintComponent

        public static void paintComponent​(Graphics g,
                                          Component c,
                                          Container p,
                                          int x,
                                          int y,
                                          int w,
                                          int h)
        将组件绘制到指定的Graphics 此方法主要用于渲染Component ,它们不作为可见包含层次结构的一部分存在,但用于渲染。 例如,如果您正在进行自己的渲染并想要渲染一些文本(甚至是HTML),您可以使用JLabel的文本渲染支持并通过此方法直接绘制,而无需将标签添加到可见的包含层次结构。

        此方法使用CellRendererPane处理实际绘制,仅在使用一个组件进行渲染时才建议使用此方法。 如果您使用多个组件来处理渲染(如JTableCellRendererPane直接使用CellRendererPane 否则,如下所述,您最终可以获得CellRendererPaneComponent

        如果c的父级不是CellRendererPane ,则创建新的CellRendererPane ,添加c ,并将CellRendererPane添加到p 如果c的父级是CellRendererPaneCellRendererPane的父级不是p ,则会将其添加到p

        该组件应该从JComponent下降或者是另一种轻量级组件。 轻量级组件是“轻量级”属性(由Component isLightweight方法返回)为真。 如果Component不是轻量级的,那就会发生糟糕的事情:崩溃,异常,绘画问题......

        参数
        g - 要绘制的 Graphics对象
        c - 绘制 Component
        p - 中间体 Container
        x - 指定区域左侧的int,以像素为单位,从图形上下文的左边缘开始测量
        y - 指定要绘制的区域顶部的int,以图形上下文的上边缘向下测量的像素为单位
        w - 指定绘制区域宽度的int,以像素为单位
        h - 一个int,指定绘制区域的高度,以像素为单位
        另请参见:
        CellRendererPaneComponent.isLightweight()
      • updateComponentTreeUI

        public static void updateComponentTreeUI​(Component c)
        一个简单的外观和感觉变化:请求树中的每个节点 updateUI() - 即,使用当前外观初始化其UI属性。
        参数
        c - 组件
      • invokeLater

        public static void invokeLater​(Runnable doRun)
        导致doRun.run()在AWT事件派发线程上异步执行。 这将在处理完所有挂起的AWT事件后发生。 当应用程序线程需要更新GUI时,应该使用此方法。 在以下示例中, invokeLater调用在事件调度线程上对Runnable对象doHelloWorld进行排队,然后打印一条消息。
          Runnable doHelloWorld = new Runnable() {
             public void run() {
                 System.out.println("Hello World on " + Thread.currentThread());
             }
         };
        
         SwingUtilities.invokeLater(doHelloWorld);
         System.out.println("This might well be displayed before the other message."); 
        如果从事件调度线程调用invokeLater - 例如,从JButton的ActionListener 调用 - 仍然会延迟doRun.run(),直到处理所有挂起事件。 请注意,如果doRun.run()抛出未捕获的异常,则事件调度线程将展开(而不是当前线程)。

        有关此方法的其他文档和示例,请参见Concurrency in Swing

        从1.3开始,这种方法只是java.awt.EventQueue.invokeLater()的封面。

        与Swing的其余部分不同,可以从任何线程调用此方法。

        参数
        doRun -实例 Runnable
        另请参见:
        invokeAndWait(java.lang.Runnable)
      • invokeAndWait

        public static void invokeAndWait​(Runnable doRun)
                                  throws InterruptedException,
                                         InvocationTargetException
        导致doRun.run()在AWT事件派发线程上同步执行。 此调用将阻塞,直到所有待处理的AWT事件都已处理doRun.run()并且(然后) doRun.run()返回。 当应用程序线程需要更新GUI时,应该使用此方法。 不应该从事件调度线程调用它。 下面是一个创建新应用程序线程的示例,该线程使用invokeAndWait从事件调度线程中打印字符串,然后在完成后,从应用程序线程中打印一个字符串。
          final Runnable doHelloWorld = new Runnable() {
             public void run() {
                 System.out.println("Hello World on " + Thread.currentThread());
             }
         };
        
         Thread appThread = new Thread() {
             public void run() {
                 try {
                     SwingUtilities.invokeAndWait(doHelloWorld);
                 }
                 catch (Exception e) {
                     e.printStackTrace();
                 }
                 System.out.println("Finished on " + Thread.currentThread());
             }
         };
         appThread.start(); 
        请注意,如果Runnable.run方法抛出未捕获的异常(在事件调度线程上),它将在调用者的线程上以InvocationTargetException的形式捕获并InvocationTargetException

        有关此方法的其他文档和示例,请参见Concurrency in Swing

        从1.3开始,这种方法只是java.awt.EventQueue.invokeAndWait()的封面。

        参数
        doRun -实例 Runnable
        异常
        InterruptedException - 如果我们在等待事件调度线程完成执行时被中断 doRun.run()
        InvocationTargetException - 如果在运行 doRun抛出异常
        另请参见:
        invokeLater(java.lang.Runnable)
      • isEventDispatchThread

        public static boolean isEventDispatchThread()
        如果当前线程是AWT事件调度线程,则返回true。

        从1.3开始,这种方法只是java.awt.EventQueue.isDispatchThread()的封面。

        结果
        如果当前线程是AWT事件调度线程,则返回true
      • getAccessibleIndexInParent

        public static int getAccessibleIndexInParent​(Component c)
        获取此对象的索引在其可访问的父级中。

        注意:从Java 2平台v1.3开始,建议开发人员调用Component.AccessibleAWTComponent.getAccessibleIndexInParent()而不是使用此方法。

        参数
        c - 该组件
        结果
        此对象的-1没有可访问的父对象。 否则,可访问父级中的子级索引。
      • getAccessibleAt

        public static Accessible getAccessibleAt​(Component c,
                                                 Point p)
        返回包含在本地坐标Point中的AccessiblePoint (如果存在)。 否则返回null
        参数
        c - 组件
        p - 本地坐标
        结果
        指定位置的Accessible (如果存在); 否则null
      • getAccessibleStateSet

        public static AccessibleStateSet getAccessibleStateSet​(Component c)
        获取此对象的状态。

        注意:从Java 2平台v1.3开始,建议开发人员调用Component.AccessibleAWTComponent.getAccessibleIndexInParent()而不是使用此方法。

        参数
        c - 组件
        结果
        AccessibleStateSet的实例,包含对象的当前状态集
        另请参见:
        AccessibleState
      • getAccessibleChildrenCount

        public static int getAccessibleChildrenCount​(Component c)
        返回对象中可访问的子级数。 如果此对象的所有子项都实现了Accessible,则此方法应返回此对象的子项数。

        注意:从Java 2平台v1.3开始,建议开发人员调用Component.AccessibleAWTComponent.getAccessibleIndexInParent()而不是使用此方法。

        参数
        c - 该组件
        结果
        对象中可访问的子项数。
      • getAccessibleChild

        public static Accessible getAccessibleChild​(Component c,
                                                    int i)
        返回对象的第n个Accessible子级。

        注意:从Java 2平台v1.3开始,建议开发人员调用Component.AccessibleAWTComponent.getAccessibleIndexInParent()而不是使用此方法。

        参数
        c - 组件
        i - 从零开始的儿童指数
        结果
        对象的第n个Accessible子对象
      • findFocusOwner

        @Deprecated
        public static Component findFocusOwner​(Component c)
        Deprecated.
        As of 1.4, replaced by KeyboardFocusManager.getFocusOwner().
        返回作为焦点所有者的指定 Component的子 Component (如果有)。
        参数
        c - 要搜索焦点所有者的 Component层次结构的根
        结果
        焦点所有者,或 null如果没有焦点所有者,或者如果焦点所有者不是 comp ,或后裔 comp
        另请参见:
        KeyboardFocusManager.getFocusOwner()
      • getRootPane

        public static JRootPane getRootPane​(Component c)
        如果c是JRootPane后代,则返回其JRootPane祖先。 如果c是RootPaneContainer,则返回其JRootPane。
        参数
        c - 组件
        结果
        组件c的JRootPane或 null
      • getRoot

        public static Component getRoot​(Component c)
        返回当前组件树的根组件。
        参数
        c - 组件
        结果
        c的第一个祖先是一个窗口或最后一个Applet祖先
      • processKeyBindings

        public static boolean processKeyBindings​(KeyEvent event)
        处理用于在所述键绑定Component关联event 此方法仅在event.getComponent()不从JComponent下降,或者您未在JComponent子类中调用super.processKeyEvent时才有用。 JComponent自动处理其processKeyEvent方法中的绑定,因此您很少需要直接调用此方法。
        参数
        event - 用于标识要处理的绑定以及哪个Component具有焦点的KeyEvent。
        结果
        如果已找到并处理了绑定,则为true
        从以下版本开始:
        1.4
      • notifyAction

        public static boolean notifyAction​(Action action,
                                           KeyStroke ks,
                                           KeyEvent event,
                                           Object sender,
                                           int modifiers)
        调用actionPerformedaction如果action是非null并接受发送对象。 ActionEvent的命令由以下因素确定:
        1. 如果操作是通过registerKeyboardAction注册的,则传入的命令字符串(如果null null将使用null )。
        2. 名称为Action.ACTION_COMMAND_KEY的操作值,除非null
        3. KeyEvent的字符串值,除非getKeyChar返回KeyEvent.CHAR_UNDEFINED ..
        如果action为非null并且在其上调用了actionPerformed,则返回true。
        参数
        action - 动作
        ks - 关键的一击
        event - 一个关键事件
        sender - 发件人
        modifiers - 动作修饰符
        结果
        true如果 actionnull并且在其上调用了actionPerformed。
        从以下版本开始:
        1.3
        另请参见:
        Action.accept(java.lang.Object)
      • replaceUIInputMap

        public static void replaceUIInputMap​(JComponent component,
                                             int type,
                                             InputMap uiInputMap)
        方便的方法来UI InputMap更改为componentuiInputMap 如果uiInputMapnull ,则会删除以前安装的所有UI InputMap。
        参数
        component - 一个组件
        type - 一种类型
        uiInputMap - InputMap
        从以下版本开始:
        1.3
      • replaceUIActionMap

        public static void replaceUIActionMap​(JComponent component,
                                              ActionMap uiActionMap)
        方便的方法来改变UI ActionMap中为componentuiActionMap 如果uiActionMapnull ,则会删除以前安装的所有UI ActionMap。
        参数
        component - 一个组件
        uiActionMap - ActionMap
        从以下版本开始:
        1.3
      • getUIInputMap

        public static InputMap getUIInputMap​(JComponent component,
                                             int condition)
        返回组件component条件condition的UI提供的InputMap。

        如果UI尚未安装指定类型的InputMap,则返回null

        参数
        component - 一个组件
        condition - 一个条件
        结果
        由组件中的 ActionMap的UI提供的 condition ,或者如果UI尚未安装指定类型的InputMap, null
        从以下版本开始:
        1.3
      • getUIActionMap

        public static ActionMap getUIActionMap​(JComponent component)
        返回组件component UI提供的ActionMap。

        如果UI尚未安装ActionMap,则返回null

        参数
        component - 一个组件
        结果
        ActionMap由组件中的UI提供,如果UI尚未安装ActionMap, null
        从以下版本开始:
        1.3
      • calculateInnerArea

        public static Rectangle calculateInnerArea​(JComponent c,
                                                   Rectangle r)
        rr指定组件的内部绘制区域的位置和大小,并返回r 位置和大小指定组件的边界,调整为不包括边框区域(插图)。 此方法对于实现绘制代码的类很有用。
        参数
        c - 有问题的JComponent; 如果是null ,则此方法返回null
        r - 要修改的Rectangle实例; 可能是null
        结果
        null如果组件是null ; 否则,返回传入的矩形(如果null )或指定位置和大小信息的新矩形
        从以下版本开始:
        1.4
      • getUnwrappedParent

        public static Container getUnwrappedParent​(Component component)
        返回component的第一个祖先,它不是JLayer的实例。
        参数
        component - Component获取第一个祖先,这不是JLayer实例。
        结果
        所述的始祖component这不是的一个实例JLayer 如果找不到这样的祖先,则返回null
        异常
        NullPointerException - 如果 componentnull
        从以下版本开始:
        1.7
        另请参见:
        JLayer
      • getUnwrappedView

        public static Component getUnwrappedView​(JViewport viewport)
        返回第一个JViewport的后代,它不是JLayer的实例。 如果找不到这样的后代,则返回null 如果viewport的视图分量不是JLayer ,该方法等效于JViewport.getView()否则JLayer.getView()将被递归调用的所有降序JLayer秒。
        参数
        viewport - JViewport获取第一个后代,而不是 JLayer实例。
        结果
        第一个JViewport的后代,不是JLayer的实例。 如果找不到这样的后代,则返回null
        异常
        NullPointerException - 如果 viewportnull
        从以下版本开始:
        1.7
        另请参见:
        JViewport.getView()JLayer