public class SwingUtilities extends Object implements SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST| Modifier and Type | Method and Description |
|---|---|
static Rectangle |
calculateInnerArea(JComponent c, Rectangle r)
在r中
r指定组件的内部绘图区域的位置和大小,并返回
r 。
|
static Rectangle[] |
computeDifference(Rectangle rectA, Rectangle rectB)
方便返回RECT的表示内的区域的阵列
rectA不与重叠
rectB 。
|
static Rectangle |
computeIntersection(int x, int y, int width, int height, Rectangle dest)
方便计算两个矩形的交点,而不分配一个新的矩形。
|
static int |
computeStringWidth(FontMetrics fm, String str)
使用指定的“metrics”(大小)的字体计算字符串的宽度。
|
static Rectangle |
computeUnion(int x, int y, int width, int height, Rectangle dest)
计算两个矩形的并集而不分配新矩形的便捷方法。
|
static MouseEvent |
convertMouseEvent(Component source, MouseEvent sourceEvent, Component destination)
返回类似于
sourceEvent但其x和y成员已转换为
destination的坐标系。
|
static Point |
convertPoint(Component source, int x, int y, Component destination)
将
source坐标系中的点
(x,y)转换为
destination坐标系。
|
static Point |
convertPoint(Component source, Point aPoint, Component destination)
转换一个
aPoint在
source坐标系
destination坐标系。
|
static void |
convertPointFromScreen(Point p, Component c)
将一个点从屏幕坐标转换为组件的坐标系
|
static void |
convertPointToScreen(Point p, Component c)
将点从组件的坐标系转换为屏幕坐标。
|
static Rectangle |
convertRectangle(Component source, Rectangle aRectangle, Component destination)
将source坐标
aRectangle中的
source aRectangle转换为
destination坐标系。
|
static Component |
findFocusOwner(Component c)
已弃用
截至1.4,由
KeyboardFocusManager.getFocusOwner() 。
|
static Accessible |
getAccessibleAt(Component c, Point p)
返回包含在本地坐标
Point中的
Accessible子项(如果存在)。
|
static Accessible |
getAccessibleChild(Component c, int i)
返回对象的第n个可访问子对象。
|
static int |
getAccessibleChildrenCount(Component c)
返回对象中可访问的子对象的数量。
|
static int |
getAccessibleIndexInParent(Component c)
在其可访问的父项中获取此对象的索引。
|
static AccessibleStateSet |
getAccessibleStateSet(Component c)
获取此对象的状态。
|
static Container |
getAncestorNamed(String name, Component comp)
以上搜索的便捷方法
comp组件层次结构中,并返回的第一对象
name它找到。
|
static Container |
getAncestorOfClass(类<?> c, Component comp)
以上搜索的便捷方法
comp组件层次结构中,并返回类的第一个目的
c它找到。
|
static Component |
getDeepestComponentAt(Component parent, int x, int y)
返回的最深可见后代组件
parent包含位置
x ,
y 。
|
static Rectangle |
getLocalBounds(Component aComponent)
返回矩形(0,0,bounds.width,bounds.height)为组件
aComponent
|
static Component |
getRoot(Component c)
返回当前组件树的根组件。
|
static JRootPane |
getRootPane(Component c)
如果c是JRootPane后代,则返回其JRootPane祖先。
|
static ActionMap |
getUIActionMap(JComponent component)
返回UI在组件
component提供的
component 。
|
static InputMap |
getUIInputMap(JComponent component, int condition)
返回由UI为条件提供的InputMap
condition在组件
component 。
|
static Container |
getUnwrappedParent(Component component)
返回的始祖 component这不是一个实例JLayer 。
|
static Component |
getUnwrappedView(JViewport viewport)
返回第
JViewport的后代,而不是
JLayer一个实例。
|
static Window |
getWindowAncestor(Component c)
返回第
Window祖先
c ,或
null如果
c未包含在里面
Window 。
|
static void |
invokeAndWait(Runnable doRun)
导致在AWT事件调度线程上
doRun.run()执行doRun.run()。
|
static void |
invokeLater(Runnable doRun)
导致
doRun.run()在AWT事件分派线程上异步执行。
|
static boolean |
isDescendingFrom(Component a, Component b)
返回
true如果组件
a从组件下降
b
|
static boolean |
isEventDispatchThread()
如果当前线程是AWT事件调度线程,则返回true。
|
static boolean |
isLeftMouseButton(MouseEvent anEvent)
如果鼠标事件指定鼠标左键,则返回true。
|
static boolean |
isMiddleMouseButton(MouseEvent anEvent)
如果鼠标事件指定鼠标中键,则返回true。
|
static boolean |
isRectangleContainingRectangle(Rectangle a, Rectangle b)
如果
a包含
b则返回true
|
static boolean |
isRightMouseButton(MouseEvent anEvent)
如果鼠标事件指定了鼠标右键,则返回true。
|
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)
计算并返回图标原点的位置,文本基线的原点位置以及复合标签字符串的可能剪切版本。
|
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)
计算并返回图标原点的位置,文本基线的原点位置以及复合标签字符串的可能剪切版本。
|
static boolean |
notifyAction(Action action, KeyStroke ks, KeyEvent event, Object sender, int modifiers)
如果启用了
action (和非
null ),则调用
actionPerformed上的
action 。
|
static void |
paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h)
绘制一个组件到指定的
Graphics 。
|
static void |
paintComponent(Graphics g, Component c, Container p, Rectangle r)
绘制一个组件到指定的
Graphics 。
|
static boolean |
processKeyBindings(KeyEvent event)
处理与
Component相关联的
event的键绑定。
|
static void |
replaceUIActionMap(JComponent component, ActionMap uiActionMap)
方便方法更改UI ActionMap for
component至
uiActionMap 。
|
static void |
replaceUIInputMap(JComponent component, int type, InputMap uiInputMap)
方便的方法来改变UI InputMap为
component到
uiInputMap 。
|
static void |
updateComponentTreeUI(Component c)
一个简单的头脑的感觉变化:请求树中的每个节点为
updateUI() - 即使用当前的外观来初始化其UI属性。
|
static Window |
windowForComponent(Component c)
返回第
Window祖先
c ,或
null如果
c未包含在里面
Window 。
|
public static final boolean isRectangleContainingRectangle(Rectangle a, Rectangle b)
a包含
b则返回true
public static Rectangle getLocalBounds(Component aComponent)
aComponent
public static Window getWindowAncestor(Component c)
Window祖先
c ,或
null如果
c未包含在里面
Window 。
c -
Component得到
Window祖先。
Window祖先
c ,或
null如果
c不包含在一个
Window 。
public static Point convertPoint(Component source, Point aPoint, Component destination)
aPoint在source坐标系destination坐标系。
如果source为null ,则假设aPoint为destination的根组件坐标系。
如果destination为null ,则aPoint将转换为source的根组件坐标系。
如果source和destination都是null ,返回aPoint没有任何转换。
public static Point convertPoint(Component source, int x, int y, Component destination)
source坐标系中的点(x,y)转换为destination坐标系。
如果source是null , (x,y)被假定为在destination的根组件坐标系统。
如果destination是null , (x,y)将被转换为source的根组件坐标系统。
如果source和destination都是null ,返回(x,y)没有任何转换。
public static Rectangle convertRectangle(Component source, Rectangle aRectangle, Component destination)
aRectangle中的source aRectangle转换为destination坐标系。
如果source是null , aRectangle被假定为在destination的根组件坐标系统。
如果destination是null , aRectangle将被转换为source的根组件坐标系统。
如果source和destination都是null ,返回aRectangle没有任何转换。
public static Container getAncestorOfClass(类<?> c, Component comp)
comp组件层次结构中,并返回类的第一个目的c它找到。
可以返回null ,如果一个类c无法找到。
public static Container getAncestorNamed(String name, Component comp)
comp组件层次结构中,并返回的第一对象name它找到。
可以返回null ,如果name 。
public static Component getDeepestComponentAt(Component parent, int x, int y)
parent包含位置x , y 。
如果parent不包含指定的位置,则返回null 。
如果parent不是容器,或者parent的可见后代中没有包含指定的位置,则返回parent 。
parent - 开始搜索的根组件
x - x目标位置
y - y目标位置
public static MouseEvent convertMouseEvent(Component source, MouseEvent sourceEvent, Component destination)
sourceEvent但其x和y成员已转换为destination的坐标系。
如果source是null , sourceEvent x和y的成员被认为是成destination的根组件坐标系统。
如果destination是null ,则返回的MouseEvent将在source的坐标系中。
sourceEvent不会更改。
返回一个新的事件。
所述source返回的事件的字段将被设置为destination如果目的地是非null使用translateMouseEvent()方法来在不改变源代码翻译从一个组件到另一个鼠标事件。
public static void convertPointToScreen(Point p, Component c)
p - 点对象(转换为新坐标系)
c - 一个Component对象
public static void convertPointFromScreen(Point p, Component c)
p - 点对象(转换为新的坐标系)
c - 一个Component对象
public static Window windowForComponent(Component c)
Window祖先c ,或null如果c不包含在一个Window 。
注意:此方法提供与getWindowAncestor相同的getWindowAncestor 。
c -
Component得到
Window祖先。
Window祖先
c或
null如果
c不包含在一个
Window 。
public static boolean isDescendingFrom(Component a, Component b)
true如果组件
a从组件下降
b
public static Rectangle computeIntersection(int x, int y, int width, int height, Rectangle dest)
x - 第一个矩形左上角的X坐标
y - 第一个矩形左上角的Y坐标
width - 第一个矩形的宽度
height - 第一个矩形的高度
dest - 第二个矩形
dest ,修改为指定交集
public static Rectangle computeUnion(int x, int y, int width, int height, Rectangle dest)
x - 第一个矩形的x坐标
y - 第一个矩形的y坐标
width - 第一个矩形的宽度
height - 第一个矩形的高度
dest - 第二个矩形的坐标;
在这个矩形中返回两个矩形的并集
dest
Rectangle
public static Rectangle[] computeDifference(Rectangle rectA, Rectangle rectB)
rectA中与rectB不重叠的rectB 。
如果两个Rect不重叠,返回一个空数组
public static boolean isLeftMouseButton(MouseEvent anEvent)
anEvent - 一个MouseEvent对象
public static boolean isMiddleMouseButton(MouseEvent anEvent)
anEvent - 一个MouseEvent对象
public static boolean isRightMouseButton(MouseEvent anEvent)
anEvent - 一个MouseEvent对象
public static int computeStringWidth(FontMetrics fm, String str)
fm - 要计算的FontMetrics对象
str - 要计算的字符串
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)
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)
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每Component 。
如果c的父母是不是CellRendererPane ,新CellRendererPane创建, c被添加到它,并且CellRendererPane添加到p 。 如果c的父母是CellRendererPane ,而CellRendererPane的父母不是p ,则被添加到p 。
组件应该从JComponent或者是另一种轻量级组件。 轻量级组件是“轻量级”属性(由Component isLightweight方法返回)为真。 如果组件不轻便,则会发生错误的事情:崩溃,异常,绘画问题...
g - 要绘制的
Graphics对象
c -
Component画
p - 中间
Container
x - 一个int指定绘制的区域的左侧,以像素为单位,从图形上下文的左边缘测量
y - 一个int指定要绘制的区域的顶部,以从图形上下文的上边缘测量的像素
w - 一个int指定绘制区域的宽度,以像素为单位
h - 指定绘制区域的高度的int(以像素为单位)
CellRendererPane , Component.isLightweight()
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画
paintComponent(Graphics,Component,Container,int,int,int,int) , CellRendererPane
public static void updateComponentTreeUI(Component c)
updateUI() - 也就是用当前的外观来初始化其UI属性。
public static void invokeLater(Runnable doRun)
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不同,该方法可以从任何线程调用。
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 。
有关此方法的其他文档和示例,请参见Concurrency in Swing 。
从1.3这个方法只是一个封面为java.awt.EventQueue.invokeAndWait() 。
InterruptedException - 如果我们在等待事件调度线程完成执行时
doRun.run()
InvocationTargetException - 如果在运行
doRun时抛出异常
invokeLater(java.lang.Runnable)
public static boolean isEventDispatchThread()
截至1.3这个方法只是java.awt.EventQueue.isDispatchThread() 。
public static int getAccessibleIndexInParent(Component c)
注意:从Java 2平台v1.3开始,建议开发人员调用Component.AccessibleAWTComponent.getAccessibleIndexInParent()而不是使用此方法。
public static Accessible getAccessibleAt(Component c, Point p)
Point (如果存在)的Accessible子节点。
否则返回null 。
Accessible在指定位置,如果存在;
否则null
public static AccessibleStateSet getAccessibleStateSet(Component c)
注意:从Java 2平台v1.3开始,建议开发人员调用Component.AccessibleAWTComponent.getAccessibleIndexInParent()而不是使用此方法。
AccessibleState
public static int getAccessibleChildrenCount(Component c)
注意:从Java 2平台v1.3开始,建议开发人员调用Component.AccessibleAWTComponent.getAccessibleIndexInParent()而不是使用此方法。
public static Accessible getAccessibleChild(Component c, int i)
注意:从Java 2平台v1.3开始,建议开发人员调用Component.AccessibleAWTComponent.getAccessibleIndexInParent()而不是使用此方法。
i - 基于零的儿童索引
@Deprecated public static Component findFocusOwner(Component c)
KeyboardFocusManager.getFocusOwner()取代。
Component指定的
Component是焦点所有者,如果有的话。
c - 搜索焦点所有者的
Component层次结构的根
null如果没有焦点所有者,或者如果焦点所有者不是
comp ,或后代
comp
KeyboardFocusManager.getFocusOwner()
public static JRootPane getRootPane(Component c)
null 。
public static boolean processKeyBindings(KeyEvent event)
Component相关联的event的键绑定。
此方法仅在event.getComponent()未从JComponent下降或您未从JComponent子类中调用super.processKeyEvent时JComponent 。
JComponent自动处理其processKeyEvent方法中的绑定,因此很少需要直接调用此方法。
event - KeyEvent用于标识要处理的绑定,以及哪个组件具有焦点。
public static boolean notifyAction(Action action, KeyStroke ks, KeyEvent event, Object sender, int modifiers)
action (而非null ),则可以调用actionPerformed上的action 。
ActionEvent的命令由以下确定:
registerKeyboardAction了该操作,则传递的命令字符串( null将被使用,如果null被传入)。 null 。 getKeyChar返回KeyEvent.CHAR_UNDEFINED .. action不是null ,并且在其上调用null ,则返回true。
public static void replaceUIInputMap(JComponent component, int type, InputMap uiInputMap)
component为uiInputMap 。
如果uiInputMap是null ,这将删除任何以前安装的UI InputMap。
public static void replaceUIActionMap(JComponent component, ActionMap uiActionMap)
component至uiActionMap 。
如果uiActionMap是null ,这将删除任何以前安装的UI ActionMap。
public static InputMap getUIInputMap(JComponent component, int condition)
condition中的条件component的UI提供的InputMap。
这将返回null如果该UI未安装指定类型的InputMap中。
public static ActionMap getUIActionMap(JComponent component)
component 。
如果UI还没有安装null ,这将返回null。
public static Rectangle calculateInnerArea(JComponent c, Rectangle r)
r的内部绘图区域的位置和大小,并返回r 。
位置和大小指定组件的边界,调整为不包括边框区域(插图)。
此方法对于实现绘画代码的类很有用。
c - 有关JComponent的问题;
如果null ,此方法返回null
r - 要修改的Rectangle实例;
可能是null
null如果组件是null ;
否则,返回传入矩形(如果null )或指定位置和大小信息的新矩形
public static Container getUnwrappedParent(Component component)
component这不是一个实例JLayer 。
component - Component获得第一个祖先,这不是一个JLayer实例。
component的第一个祖先不是JLayer的一个例子。
如果找不到这样的祖先,则返回null 。
NullPointerException - 如果
component是
null
JLayer
public static Component getUnwrappedView(JViewport viewport)
JViewport的第一个JViewport的JLayer 。
如果找不到这样的后代,则返回null 。
如果viewport的视图组件不是JLayer ,则此方法相当于JViewport.getView()否则JLayer.getView()将递归调用所有降序JLayer 。
viewport -
JViewport获得第一个后裔,其中不是一个
JLayer实例。
JViewport的后代,不是JLayer一个例子。
如果找不到这样的后代,则返回null 。
NullPointerException - 如果
viewport是
null
JViewport.getView(),
JLayer
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.