模块 java.desktop

类 SwingUtilities

java.lang.Object
javax.swing.SwingUtilities
所有已实现的接口:
SwingConstants

public class SwingUtilities extends Object implements SwingConstants
Swing 实用方法的集合。
自从:
1.2
  • 方法详情

    • isRectangleContainingRectangle

      public static final boolean isRectangleContainingRectangle(Rectangle  a, Rectangle  b)
      如果 a 包含 b,则返回 true
      参数:
      a - 第一个矩形
      b - 第二个矩形
      返回:
      true 如果 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)
      返回 c 的第一个 Window 祖先,如果 c 不包含在 Window 中,则返回 null
      参数:
      c - Component 得到 Window 的祖先。
      返回:
      c 的第一个 Window 祖先,或者 null 如果 c 不包含在 Window 中。
      自从:
      1.3
    • convertPoint

      public static Point  convertPoint(Component  source, Point  aPoint, Component  destination)
      source坐标系中的aPoint转换为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 的根组件坐标系中。如果 destinationnull ,则 aRectangle 将转换为 source 的根组件坐标系。如果 sourcedestination 都是 null ,则返回 aRectangle 而不进行任何转换。
      参数:
      source - 源组件
      aRectangle - 一个矩形
      destination - 目标组件
      返回:
      转换后的矩形
    • getAncestorOfClass

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

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

      public static Component  getDeepestComponentAt(Component  parent, int x, int y)
      返回包含位置 xyparent 的最深可见后代组件。如果 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 的坐标系。如果 sourcenull ,则假定 sourceEvent x 和 y 成员位于 destination 的根组件坐标系中。如果 destinationnull ,则返回的 MouseEvent 将在 source 的坐标系中。 sourceEvent 不会更改。返回一个新事件。如果目标不是 null,则返回事件的 source 字段将设置为 destination 使用 translateMouseEvent() 方法将鼠标事件从一个组件转换为另一个组件,而不更改源。
      参数:
      source - 源组件
      sourceEvent - 源鼠标事件
      destination - 目标组件
      返回:
      新鼠标事件
    • convertPointToScreen

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

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

      public static Window  windowForComponent(Component  c)
      返回 c 的第一个 Window 祖先,如果 c 不包含在 Window 中,则返回 null

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

      参数:
      c - Component 得到 Window 的祖先。
      返回:
      c 的第一个 Window 祖先,或者 null 如果 c 不包含在 Window 中。
    • isDescendingFrom

      public static boolean isDescendingFrom(Component  a, Component  b)
      如果组件 a 是组件 b 的后代,则返回 true
      参数:
      a - 第一个组件
      b - 第二个组件
      返回:
      true 如果组件 a 来自组件 b
    • computeIntersection

      public static Rectangle  computeIntersection(int x, int y, int width, int height, Rectangle  dest)
      无需分配新矩形即可方便地计算两个矩形的交集。如果两个矩形不相交,则返回的矩形从 (0,0) 开始,宽度和高度为零。
      参数:
      x - 第一个矩形左上角的 X 坐标
      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 不重叠,则返回一个空数组
      参数:
      rectA - 第一个矩形
      rectB - 第二个矩形
      返回:
      代表 rectA 内不与 rectB 重叠的区域的矩形数组。
    • isLeftMouseButton

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

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

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

      public static int computeStringWidth(FontMetrics  fm, String  str)
      使用具有指定“度量”(大小)的字体计算字符串的宽度。
      参数:
      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(它们将默认为 RIGHT)和 horizontalAlignment(它们将默认为 CENTER)中的 LEADING/TRAILING 值。请改用其他版本的 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 s,它们不作为可见包含层次结构的一部分存在,但用于渲染。例如,如果您正在进行自己的渲染并想要渲染一些文本(甚至是 HTML),您可以使用 JLabel 的文本渲染支持并通过此方法直接绘制,而无需将标签添加到可见的包含层次结构。

      此方法使用 CellRendererPane 来处理实际绘画,仅在使用一个组件进行渲染时才推荐使用。如果像JTable那样使用多个组件来处理渲染,请直接使用CellRendererPane。否则,如下所述,您最终可能会得到一个 CellRendererPane per Component

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

      该组件应该是 JComponent 的后代,或者是另一种轻量级组件。轻量级组件是指其“轻量级”属性(由 Component isLightweight 方法返回)为真的组件。如果组件不是轻量级的,就会发生坏事:崩溃、异常、绘画问题……

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

      public static void paintComponent(Graphics  g, Component  c, Container  p, Rectangle  r)
      将组件绘制到指定的 Graphics 。这是 paintComponent(Graphics,Component,Container,int,int,int,int) 的覆盖方法。请参阅它以获取更多信息。
      参数:
      g - 要绘制的 Graphics 对象
      c - Component 绘制
      p - 中间体 Container
      r - 要绘制的 Rectangle
      参见:
    • 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()抛出一个未捕获的异常,事件调度线程将展开(不是当前线程)。

      可以在 Swing 中的并发 中找到此方法的其他文档和示例。

      从 1.3 开始,此方法只是 java.awt.EventQueue.invokeLater() 的掩护。

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

      参数:
      doRun - Runnable 的实例
      参见:
    • invokeAndWait

      public static void invokeAndWait(Runnable  doRun) throws InterruptedException , InvocationTargetException
      导致 doRun.run() 在 AWT 事件调度线程上同步执行。此调用会阻塞,直到处理完所有未决的 AWT 事件并且(然后)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 ,在调用者的线程上。

      可以在 Swing 中的并发 中找到此方法的其他文档和示例。

      从 1.3 开始,此方法只是 java.awt.EventQueue.invokeAndWait() 的掩护。

      参数:
      doRun - Runnable 的实例
      抛出:
      InterruptedException - 如果我们在等待事件分派线程完成执行时被打断 doRun.run()
      InvocationTargetException - 如果在运行时抛出异常 doRun
      参见:
    • 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)
      返回包含在局部坐标 PointAccessible 子节点(如果存在)。否则返回 null
      参数:
      c - 组件
      p - 本地坐标
      返回:
      指定位置的 Accessible(如果存在);否则null
    • getAccessibleStateSet

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

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

      参数:
      c - 组件
      返回:
      包含对象当前状态集的 AccessibleStateSet 实例
      参见:
    • 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 个可访问子对象。

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

      参数:
      c - 组件
      i - 从零开始的子索引
      返回:
      对象的第 n 个可访问子对象
    • findFocusOwner

      @Deprecated public static Component  findFocusOwner(Component  c)
      已弃用。
      从 1.4 开始,由 KeyboardFocusManager.getFocusOwner() 取代。
      返回作为焦点所有者的指定 Component 的子节点 Component(如果有)。
      参数:
      c - Component 层次结构的根,用于搜索焦点所有者
      返回:
      焦点所有者,或 null 如果没有焦点所有者,或者如果焦点所有者不是 compcomp 的后代
      参见:
    • getRootPane

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

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

      public static boolean processKeyBindings(KeyEvent  event)
      处理与 event 关联的 Component 的键绑定。此方法仅在 event.getComponent() 不是从 JComponent 继承,或者您没有从 JComponent 子类中调用 super.processKeyEvent 时才有用。 JComponent 自动处理其 processKeyEvent 方法中的绑定,因此您很少需要直接调用此方法。
      参数:
      event - KeyEvent 用于识别要处理的绑定,以及哪个组件具有焦点。
      返回:
      如果已找到并处理绑定,则为真
      自从:
      1.4
    • notifyAction

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

      public static void replaceUIInputMap(JComponent  component, int type, InputMap  uiInputMap)
      component 的 UI InputMap 更改为 uiInputMap 的便捷方法。如果 uiInputMapnull ,这将删除任何以前安装的 UI InputMap。
      参数:
      component - 一个组件
      type - 一种类型
      uiInputMap - 一个 InputMap
      自从:
      1.3
    • replaceUIActionMap

      public static void replaceUIActionMap(JComponent  component, ActionMap  uiActionMap)
      component 的 UI ActionMap 更改为 uiActionMap 的便捷方法。如果 uiActionMapnull,这将删除任何以前安装的 UI ActionMap。
      参数:
      component - 一个组件
      uiActionMap - 一个 ActionMap
      自从:
      1.3
    • getUIInputMap

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

      如果 UI 没有安装指定类型的 InputMap,这将返回 null

      参数:
      component - 一个组件
      condition - 一个条件
      返回:
      组件中 UI 为 condition 提供的 ActionMap,如果 UI 未安装指定类型的 InputMap,则为 null
      自从:
      1.3
    • getUIActionMap

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

      如果 UI 没有安装 ActionMap,这将返回 null

      参数:
      component - 一个组件
      返回:
      组件中 UI 提供的 ActionMap,如果 UI 未安装 ActionMap,则为 null
      自从:
      1.3
    • calculateInnerArea

      public static Rectangle  calculateInnerArea(JComponent  c, Rectangle  r)
      将指定组件的内部绘制区域的位置和大小存储在 r 中并返回 r 。位置和大小指定组件的边界,调整后不包括边界区域(insets)。此方法对于实现绘画代码的类很有用。
      参数:
      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
      参见:
    • 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
      参见: